我想找到 SQL 中表的计数总和
I want to find the sum of the counts of tables in SQL
请在下面找到我使用的查询
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
的单个总和。
如果我没理解错的话,您只需要一个摘要行。您可以使用 with rollup
:
select . . .
group by cm.city_name with rollup;
你似乎想要 :
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;
请在下面找到我使用的查询
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
的单个总和。
如果我没理解错的话,您只需要一个摘要行。您可以使用 with rollup
:
select . . .
group by cm.city_name with rollup;
你似乎想要 :
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;