我有以下查询,但我需要能够为列出的每个列提供“总数”。
有人可以帮忙吗?
这是我的sql:
select PartnerAbbrev,
count(TrafficPreApplications.Id) as Hits,
SUM(Case WHEN TrafficPreApplications.laststep = 1 THEN 1 ELSE 0 END) as Hit1_1,
SUM(Case WHEN TrafficPreApplications.laststep = 2 THEN 1 ELSE 0 END) as Hit1_2,
SUM(Case WHEN TrafficPreApplications.laststep = 3 THEN 1 ELSE 0 END) as Hit1_3,
SUM(Case WHEN TrafficPreApplications.laststep = 4 THEN 1 ELSE 0 END) as Hit1_4,
SUM(Case WHEN TrafficPreApplications.laststep = 5 THEN 1 ELSE 0 END) as Hit1_5,
SUM(Case WHEN TrafficPreApplications.laststep = 6 THEN 1 ELSE 0 END) as Hit1_6,
SUM(Case WHEN TrafficPreApplications.laststep = 7 THEN 1 ELSE 0 END) as Hit1_7,
SUM(Case WHEN TrafficPreApplications.laststep = 9 THEN 1 ELSE 0 END) as Hit1_9,
SUM(Case WHEN TrafficPreApplications.laststep = 999 THEN 1 ELSE 0 END) as Hit1_999,
SUM(Case WHen TrafficHitId is not null THEN 1 ELSE 0 END) as Today_PreApp
from TrafficPreApplications full join TrafficHits on TrafficPreApplications.TrafficHitId = traffichits.Id
join TrafficPartners on TrafficPartners.Id = TrafficHits.TrafficPartnerId
join Partners on Partners.Id = TrafficPartners.PartnerId
group by PartnerAbbrev
答案 0 :(得分:2)
您不需要完全加入,无论如何都会从TrafficHits
移除TrafficPreApplications
行。
select Partners.Id,
PartnerAbbrev,
count(TrafficPreApplications.Id) as Hits,
SUM(Case WHEN TrafficPreApplications.laststep = 1 THEN 1 ELSE 0 END) as Hit1_1,
...
from TrafficPreApplications
right join TrafficHits on TrafficPreApplications.TrafficHitId = traffichits.Id
join TrafficPartners on TrafficPartners.Id = TrafficHits.TrafficPartnerId
join Partners on Partners.Id = TrafficPartners.PartnerId
group by Partners.Id, PartnerAbbrev
union all
select max(Partners.Id)+1,
'total',
count(TrafficPreApplications.Id) as Hits,
SUM(Case WHEN TrafficPreApplications.laststep = 1 THEN 1 ELSE 0 END) as Hit1_1,
...
from TrafficPreApplications
right join TrafficHits on TrafficPreApplications.TrafficHitId = traffichits.Id
join TrafficPartners on TrafficPartners.Id = TrafficHits.TrafficPartnerId
join Partners on Partners.Id = TrafficPartners.PartnerId
order by 1,2