Я пытаюсь понять важность подсчета переменной внутри подзапроса против внешнего.
Query1: использование count () внутри, получение неправильных результатов, но я не знаю почему:
sel
monthday_key,
mfr_mdl_code,
--svc_time_Zone_Code,
cntc_rqst_subarea_desc,
CALL_COUNT
from
(
sel
monthday_key,
--svc_time_Zone_Code,
mfr_mdl_code,
cntc_rqst_subarea_desc
,mfr_Ser_id,
count(cntc_rqst_id) call_count
from cust_Acct_equip_fact A join monthday_dim b on monthday_key between bgn_monthday_key and end_monthday_key
and equip_stat_code = 'ACTV' and monthday_key = current_Date-1
join equip_type_dim C on a.equip_type_key=c.equip_type_key and mfr_mdl_code like any ('C21)
join cntc_rqst_tech_fact D on a.cust_acct_key=cntc_key and cntc_Rqst_bgn_Date = monthday_key and cntc_rqst_bgn_date = current_Date-1
join cust_acct_dim E on a.cust_acct_key=e.cust_Acct_key
AND D.CNTC_RQST_SUBAREA_DESC in ('NO SS')
--and svc_time_Zone_Code in ('P','E','M','C')
group by 1,2,3,4
) T1
group by 1,2,3,4
Использование Count () вне подзапроса, получение правильных результатов:
sel
monthday_key,
mfr_mdl_code,
--svc_time_Zone_Code,
cntc_rqst_subarea_desc,
count(cntc_Rqst_id) as CALL_COUNT
from
(
sel
monthday_key,
--svc_time_Zone_Code,
mfr_mdl_code,
cntc_rqst_subarea_desc
,mfr_Ser_id,
cntc_rqst_id
from cust_Acct_equip_fact A join monthday_dim b on monthday_key between bgn_monthday_key and end_monthday_key
and equip_stat_code = 'ACTV' and monthday_key = current_Date-1
join equip_type_dim C on a.equip_type_key=c.equip_type_key and mfr_mdl_code like any ('C21')
join cntc_rqst_tech_fact D on a.cust_acct_key=cntc_key and cntc_Rqst_bgn_Date = monthday_key and cntc_rqst_bgn_date = current_date-1
join cust_acct_dim E on a.cust_acct_key=e.cust_Acct_key
AND D.CNTC_RQST_SUBAREA_DESC in ('NO SS')
--and svc_time_Zone_Code in ('P','E','M','C')
group by 1,2,3,4,5
) T1
group by 1,2,3