以下是我正在处理的陈述。 它是创建一个单独的查询,按级别类别(军官,NCO或部队)以及女性和男性童子军总数显示男性或女性童子军的数量。
SELECT
(Select count(Gender)
from stormtroopers_officer
join st_officer_assign on stormtroopers_officer.STID=st_officer_assign.STID
where Gender='Female' AND st_officer_assign.Role = 'Scout') as 'FOScout',
(SELECT count(Gender)
from stormtroopers_nco
join st_nco_assign on stormtroopers_nco.STID=st_nco_assign.STID
where Gender='Female' AND st_nco_assign.Role = 'Scout') as 'FNScout',
(SELECT count(Gender)
from stormtroopers_troop
join st_troop_assign on stormtroopers_troop.STID=st_troop_assign.STID
where Gender='Female' AND st_troop_assign.Role = 'Scout') as 'FTScout',
(Select count(Gender)
from stormtroopers_officer
join st_officer_assign on stormtroopers_officer.STID=st_officer_assign.STID
where Gender='male' AND st_officer_assign.Role = 'Scout') as 'MOScout',
(SELECT count(Gender)
from stormtroopers_nco
join st_nco_assign on stormtroopers_nco.STID=st_nco_assign.STID
where Gender='male' AND st_nco_assign.Role = 'Scout') as 'MNScout',
(SELECT count(Gender)
from stormtroopers_troop
join st_troop_assign on stormtroopers_troop.STID=st_troop_assign.STID
where Gender='male' AND st_troop_assign.Role = 'Scout') as 'MTScout',
(SELECT count(Gender)
from stormtroopers_officer
WHERE Gender = 'Female') as 'Total Female Scouts',
(SELECT count(Gender)
from stormtroopers_troop
WHERE Gender='Male') as 'Total Male Scouts',
(SELECT count(Gender)
from stormtroopers_troop) as 'Total Male Scouts';
这些是我想要结合的陈述,因为它看起来太长了,我相信有一种方法可以更容易地做到这一点
这是输出的样子,我不想将其更改为多行或不同列数。 output is here
答案 0 :(得分:0)
如果您可以将结果作为单独的行使用,则可以使用GROUP BY
:
Select Gender, a.role, count(Gender)
from stormtroopers_officer o join
st_officer_assign a
on o.STID = a.STID
group by Gender, a.Role;
答案 1 :(得分:0)
您可以声明变量并将每个计数分配给变量。 最后选择分配的所有变量。
DECLARE @Countofficer INT;
DECLARE @Countnco INT;
SET @Countofficer =(Select count(Gender)
from stormtroopers_officer
join st_officer_assign on
stormtroopers_officer.STID=st_officer_assign.STID
where Gender='Female' AND st_officer_assign.Role = 'Scout')
SET @Countnco =(SELECT count(Gender)
from stormtroopers_nco
join st_nco_assign on
stormtroopers_nco.STID=st_nco_assign.STID
where Gender='Female' AND st_nco_assign.Role = 'Scout')
SELECT @Countofficer AS stormtroopers_officer ,@Countnco AS stormtroopers_nco
答案 2 :(得分:0)
我认为这会做你想做的事情:
select FOScouts, FNScouts, FTScouts, MOScouts, MNScouts, MTScouts,
FOScouts+FNScouts+FTScouts as `Total Female Scouts`,
MOScouts+MNScouts+MTScouts as `Total Male Scouts`,
FOScouts+FNScouts+FTScouts+MOScouts+MNScouts+MTScouts as `Total Scouts`
from (select count(case when gender='Female' then 1 end) as FOScouts,
count(case when gender='Male' then 1 end) as MOScouts
from stormtroopers_officer st_o
join st_officer_assign st_o_a
on st_o_a.stid = st_o.stid
where st_o_a.role = 'Scout') o
join
(select count(case when gender='Female' then 1 end) as FNScouts,
count(case when gender='Male' then 1 end) as MNScouts
from stormtroopers_nco st_n
join st_nco_assign st_n_a
on st_n_a.stid = st_n.stid
where st_n_a.role = 'Scout') n
join
(select count(case when gender='Female' then 1 end) as FTScouts,
count(case when gender='Male' then 1 end) as MTScouts
from stormtroopers_troop st_t
join st_troop_assign st_t_a on st_t_a.stid = st_t.stid
where st_t_a.role = 'Scout') t
答案 3 :(得分:0)
将您的查询更改为:
SELECT
(Select count(Gender) as 'FOScout' from stormtroopers_officer join st_officer_assign on stormtroopers_officer.STID=st_officer_assign.STID where Gender='Female') as 'FOScout',
(SELECT count(Gender) as 'FNScout' from stormtroopers_nco join st_nco_assign on stormtroopers_nco.STID=st_nco_assign.STID where Gender='Female') as 'FNScout',
(SELECT count(Gender) as 'FTScout' from stormtroopers_troop join st_troop_assign on stormtroopers_troop.STID=st_troop_assign.STID where Gender='Female') as 'FTScout',
(Select count(Gender) as 'MOScout' from stormtroopers_officer join st_officer_assign on stormtroopers_officer.STID=st_officer_assign.STID where Gender='male') as 'MOScout',
(SELECT count(Gender) as 'MNScout' from stormtroopers_nco join st_nco_assign on stormtroopers_nco.STID=st_nco_assign.STID where Gender='male') as 'MNScout',
(SELECT count(Gender) as 'MTScout' from stormtroopers_troop join st_troop_assign on stormtroopers_troop.STID=st_troop_assign.STID where Gender='male') as 'MTScout',
(SELECT count(Gender) as 'Total Female Scouts' from stormtroopers_troop WHERE Gender = 'Female') as 'Total Female Scouts',
(SELECT count(Gender) as 'Total Male Scouts' from stormtroopers_troop WHERE Gender='Male') as 'Total Male Scouts',
(SELECT count(Gender) as 'Total Male Scouts' from stormtroopers_troop) as 'Total Male Scouts'
我在下面的帖子中回答了这个问题: How to include multiple select statements in one table
请将其标记为答案,如果是的话。