powered by simpleCommunicator - 2.0.51     © 2025 Programmizd 02
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Обозначение результата в качестве значения.
7 сообщений из 7, страница 1 из 1
Обозначение результата в качестве значения.
    #40053380
elay13
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Пытаюсь взять среднее значение с двух запросов, обозначены как Roi1 и Roi2 ниже. Проблема заключается в том , что один запрос идёт по 10му месяцу , а второй по 11. Не получается взять avg от Roi1 + Roi2, видимо, из-за значения NULL. Подскажите как сделать это подзапросом или обозначить результаты roi1 и roi2 качестве значения ?

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
select  appl_month as month
    , (sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then app_PS_2mob end) 
 / sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then cash_to_client end))
 / cast((sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then app_PS_1mob end) 
 / sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then cash_to_client end))as float) as Roi1

    , (sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then app_PS_2mob end) 
 / sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then cash_to_client end))
 / cast((sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then app_PS_1mob end) 
 / sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then cash_to_client end)) as float) as Roi2
  from DWH.RMT
			where		1=1						                                         
		and loyal_client = 0						
		and request_id_PDL_step1 is not null						
		and isnull(refinancing, 0) <> 1						
	    and appl_registr_method not in ('МП Android', 'МП iOS', 'Онлайн', 'Сайт ДС 2.0')						
		and office_group not in ('401' , '402',' 403', '404')
	    and date_time >= '10-01-2020'
		and date_time <='11-30-2020'
group by appl_month
order by appl_month



Результат:
month Roi1 Roi2
2020-10 1,4 NULL
2020-11 NULL 1,5
...
Рейтинг: 0 / 0
Обозначение результата в качестве значения.
    #40053384
Владислав Колосов
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
elay13,

Код: sql
1.
2.
3.
4.
5.
6.
7.
select avg(tbl1.f1)
from 
(
select roi1 as f1...
union all
select roi2 as f1 ...
) tbl1
...
Рейтинг: 0 / 0
Обозначение результата в качестве значения.
    #40053391
elay13
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Владислав Колосов, извините , я правильно понял что после f1, вместо трёх точек следуют мои запросы roi1, roi2?
...
Рейтинг: 0 / 0
Обозначение результата в качестве значения.
    #40053446
Владислав Колосов
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
elay13,

Да, запросы, которые формируют указанные значения. При вычислении среднего по колонке null-значения будут пропущены.
...
Рейтинг: 0 / 0
Обозначение результата в качестве значения.
    #40053459
invm
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
with t as
(
select  appl_month as month
    , (sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then app_PS_2mob end) 
 / sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then cash_to_client end))
 / cast((sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then app_PS_1mob end) 
 / sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then cash_to_client end))as float) as Roi1

    , (sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then app_PS_2mob end) 
 / sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then cash_to_client end))
 / cast((sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then app_PS_1mob end) 
 / sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then cash_to_client end)) as float) as Roi2
  from DWH.RMT
			where		1=1						                                         
		and loyal_client = 0						
		and request_id_PDL_step1 is not null						
		and isnull(refinancing, 0) <> 1						
	    and appl_registr_method not in ('МП Android', 'МП iOS', 'Онлайн', 'Сайт ДС 2.0')						
		and office_group not in ('401' , '402',' 403', '404')
	    and date_time >= '10-01-2020'
		and date_time <='11-30-2020'
group by appl_month
order by appl_month
)
select
 t.*, avg(isnull(t.Roi1, 0) + isnull(t.Roi2, 0)) over ()
from
 t;
...
Рейтинг: 0 / 0
Обозначение результата в качестве значения.
    #40053472
elay13
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Владислав Колосов, Спасибо огромное , чуть-чуть доработал ваш вариант.

select avg(tbl1.f1)
from
(
select

(sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then app_PS_2mob end)
/ sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then cash_to_client end))
/ cast((sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then app_PS_1mob end)
/ sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-10' then cash_to_client end))as float) as f1
from DWH.RMT
union all
select (sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then app_PS_2mob end)
/ sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then cash_to_client end))
/ cast((sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then app_PS_1mob end)
/ sum(case when financed = 1 and term_day <= 35 and appl_month = '2020-11' then cash_to_client end)) as float)
as f1
from DWH.RMT

) tbl1
...
Рейтинг: 0 / 0
Обозначение результата в качестве значения.
    #40053475
elay13
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
invm, Спасибо , отличный вариант.
...
Рейтинг: 0 / 0
7 сообщений из 7, страница 1 из 1
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Обозначение результата в качестве значения.
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]