请在下面使用的查询中查找
select cm.city_name,
count(case when k.listing_status_uid=1 then 1 end ) as 'Active',
count(case when k.listing_status_uid=3 then 1 end ) as 'Bad_listing',
count(case when k.listing_status_uid=4 then 1 end ) as 'proj_scrapped',
count(case when k.listing_status_uid=5 then 1 end ) as 'proj_on_hold',
count(case when k.listing_status_uid=6 then 1 end ) as 'sold_out',
count(case when k.listing_status_uid=7 then 1 end ) as 'others'
from ksl_listing_master k
join ksl_locality_master lm on lm.locality_uid=k.listing_locality
join ksl_city_master cm on cm.city_uid=lm.city_uid
join ksl_listing_status ls on ls.listing_status_uid=k.listing_status_uid
group by cm.city_name
这给出了类似下面的输出
现在,我想要每列的总和。
城市名称下方应为Sum
我希望在同一查询中分别计算Active
,Bad_listing
,proj_scrapped
,proj_on_hold
,sold_out
和others
的总和。
答案 0 :(得分:2)
如果我理解正确,则只需要一个摘要行。您可以使用with rollup
:
select . . .
group by cm.city_name with rollup;
答案 1 :(得分:0)
您似乎想要:
select cm.city_name,
sum(k.listing_status_uid = 1) as 'Active',
sum(k.listing_status_uid = 3) as 'Bad_listing',
sum(k.listing_status_uid = 4) as 'proj_scrapped',
. . .
sum(k.listing_status_uid in (1,3,4)) as 'total'
from ksl_listing_master k join
ksl_locality_master lm
on lm.locality_uid = k.listing_locality join
ksl_city_master cm
on cm.city_uid=lm.city_uid join
ksl_listing_status ls
on ls.listing_status_uid = k.listing_status_uid
group by cm.city_name;