将同一列的日期分为多行,直到指定下一个日期值-SQL Server-英雄云拓展知识分享
338
2024-01-22
我有桌子 Mark_Attendance
它包括:
Class | [Status] | [DateTime] | S_Adm_No | Gender------+----------+-------------+-----------+--------

NUR | P | 2017-04⑴9 | 1101 | Male
NUR | A | 2017-04⑴9 | 1102 | Male
NUR | P | 2017-04⑴9 | 1103 | Female
NUR | A | 2017-04⑴9 | 1104 | Female
KG | P | 2017-04⑴9 | 1105 | Male
KG | A | 2017-04⑴9 | 1106 | Male
KG | P | 2017-04⑴9 | 1107 | Female
KG | A | 2017-04⑴9 | 1108 | Female
现在我想显示我的结果
Class|ttl|total_male|ttl_FeMale|Ttl_Pr|Pr_Male|Pr_Female|Ttl_Ab|Ab_Female|Ab_Mal2NUR |4 | 2 | 2 | 2 |2 | 2 |2 |2 |2
KG |4 | 2 | 2 | 2 |2 | 2 |2 |2 |2
为了取得此结果,我在SQL Server中应使用甚么查询。
固然您的输出是毛病的
适当的查询和输出应为:
select Class, Count(Class) as ttl,Sum(Case when Gender = 'Male' Then 1 Else 0 End) as Total_Male,
Sum(Case when Gender = 'Female' Then 1 Else 0 End) as Total_Female,
Sum(Case when Status = 'P' Then 1 Else 0 End) as Total_Present,
Sum(Case when Status = 'P' and Gender = 'Male' Then 1 Else 0 End) as Total_Male_Present,
Sum(Case when Status = 'P' and Gender = 'Female' Then 1 Else 0 End) as Total_Female_Present,
Sum(Case when Status = 'A' Then 1 Else 0 End) as Total_Absent,
Sum(Case when Status = 'A' and Gender = 'Male' Then 1 Else 0 End) as Total_Male_Absent,
Sum(Case when Status = 'A' and Gender = 'Female' Then 1 Else 0 End) as Total_Female_Absent
from Mark_Attendance
group by Class;
和输出应为:
Class ttl Total_Male Total_Female Total_Present Total_Male_Present Total_Female_Present Total_Absent Total_Male_Absent Total_Female_Absent------ ----------- ----------- ------------ ------------- ------------------ -------------------- ------------ ----------------- -------------------
KG 4 2 2 2 1 1 2 1 1
NUR 4 2 2 2 1 1 2 1 1
如果您想了解我如何实现此结果,请浏览
小组按子句
总和案例声明
我强烈禁止人们在没有表现出努力的情形下发布问题的人。
免责声明:
本网址(www.yingxiongyun.com)发布的材料主要源于独立创作和网友匿名投稿。此处提供的所有信息仅供参考之用。我们致力于提供准确且可信的信息,但不对材料的完整性或真实性作出任何保证。用户应自行验证相关信息的正确性,并对其决策承担全部责任。对于由于信息的错误、不准确或遗漏所造成的任何损失,本网址不承担任何法律责任。本网站所展示的所有内容,如文字、图像、标志、音频、视频、软件和程序等的版权均属于原创作者。如果任何组织或个人认为网站内容可能侵犯其知识产权,或包含不准确之处,请即刻联系我们进行相应处理。
发表评论
暂时没有评论,来抢沙发吧~