Мастерская > Помощь с компьютером

Есть ли спецы в вопросах SQL?

<< < (2/3) > >>

void:

--- Цитата: Костян от 04.11.2016 14:41:47 ---Как видно, она состоит из январской части аренды1, полной аренды 2 и январской части аренды3               

--- Конец цитаты ---
Аренда1 и аренда3 рассчитать по минутам, перемножить на кол-во минут в пересечении с периодом выборки + аренда2. Если я правильно понял задачу.

Kostas:

--- Цитата: void от 04.11.2016 15:38:18 ---Аренда1 и аренда3 рассчитать по минутам, перемножить на кол-во минут в пересечении с периодом выборки + аренда2. Если я правильно понял задачу
--- Конец цитаты ---

В excell примерно так и делаю.  А как сделать для MySQL или MS SQL , не знаю. 

 

Taiwaz:
перепроверь результат только, на коленке написано

declare @rent table(
id int,
rentBegin datetime,
rentEnd datetime,
total decimal(15,2)
)


insert into @rent (id, rentBegin, rentEnd, total) values (1, '2014-12-25 13:30', '2015-01-03 18:00', 5200)
insert into @rent (id, rentBegin, rentEnd, total) values (2, '2015-01-03 18:00', '2015-01-26 12:20', 17340)
insert into @rent (id, rentBegin, rentEnd, total) values (3, '2015-01-26 12:20', '2015-02-07 00:35', 4700)
insert into @rent (id, rentBegin, rentEnd, total) values (4, '2015-12-24 14:45', '2016-01-04 13:00', 6500)

Declare @begin datetime,
      @end datetime

Set @begin = '2015-01-01 00:00'
Set @end = '2015-01-31 23:59'

select cast(sum(tariff * perionTime) as decimal(15,2)) from
   (
   select id, DATEdiff(ss, rentBegin, rentEnd) as fullrentTime,
   case
      when (rentBegin < @begin and rentEnd > @end) then DATEdiff(ss, @begin, @end)
      when (rentBegin >= @begin and rentEnd <= @end) then DATEdiff(ss, rentBegin, rentEnd)
      when (rentBegin < @begin and rentEnd <= @end) then DATEdiff(ss, @begin, rentEnd)
      when (rentBegin >= @begin and rentEnd > @end) then DATEdiff(ss, rentBegin, @end)
   end as perionTime
   ,  total, total / DATEdiff(ss, rentBegin, rentEnd) as tariff  from @rent
   where rentBegin between @begin and @end or rentEnd  between @begin and @end
   ) as r

Kostas:
Спасибо. Буду разбираться

Taiwaz:

--- Цитата: Костян от 04.11.2016 19:11:46 ---Буду разбираться
--- Конец цитаты ---
будут вопросы - пиши в личку. тебе нужна только эта часть

--- Цитата: Taiwaz от 04.11.2016 16:32:53 ---Declare @begin datetime,
      @end datetime

Set @begin = '2015-01-01 00:00'
Set @end = '2015-01-31 23:59'

select cast(sum(tariff * perionTime) as decimal(15,2)) from
   (
   select id, DATEdiff(ss, rentBegin, rentEnd) as fullrentTime,
   case
      when (rentBegin < @begin and rentEnd > @end) then DATEdiff(ss, @begin, @end)
      when (rentBegin >= @begin and rentEnd <= @end) then DATEdiff(ss, rentBegin, rentEnd)
      when (rentBegin < @begin and rentEnd <= @end) then DATEdiff(ss, @begin, rentEnd)
      when (rentBegin >= @begin and rentEnd > @end) then DATEdiff(ss, rentBegin, @end)
   end as perionTime
   ,  total, total / DATEdiff(ss, rentBegin, rentEnd) as tariff  from @rent
   where rentBegin between @begin and @end or rentEnd  between @begin and @end
   ) as r
--- Конец цитаты ---

@rent - таблица в твоей базе
id - аренда1.. аренда48
rentBegin  - [начало]
rentEnd - [конец]
total - [полученная аренда]

Set @begin = '2015-01-01 00:00' - начало твоего периода
Set @end = '2015-01-31 23:59' - конец периода.

тариф расчитан по секундам, если хочешь поменять на минуты или дни, то нужно менять значение 1го параметав DATEdiff, вместо ss - mm, dd и так далее
этот пример на MS SQL

Навигация

[0] Главная страница сообщений

[#] Следующая страница

[*] Предыдущая страница

Перейти к полной версии