1

如何使用表中的组获取​​这些查询?“SaveDate”即使我想按天分组列中的日期。如下:

+---+------------+------------+---+------------+
|Day|queryresult1|queryresult2|...|queryresult5|
+---+------------+------------+---+------------+
|1  |count       |count       |...|count       |
+---+------------+------------+---+------------+
|2  |count       |count       |...|count       |
+---+------------+------------+---+------------+
|...|...         |...         |...|...         |
+---+------------+------------+---+------------+
|...|...         |...         |...|...         |
+---+------------+------------+---+------------+
|...|...         |...         |...|...         |
+---+------------+------------+---+------------+
|30 |count       |count       |...|count       |
+---+------------+------------+---+------------+

1-15 天在表格的列保存数据记录数据。如何显示 30 天?

Select 
(Select Count(*) from Document Where LineID=45 and Esitlendi=1) result1,
(Select Count(*) from Document Where LineID=45 and Esitlendi=1 and TutanakUserID is not null) as result2,
(Select Count(*) from Document Where LineID=45 and Esitlendi=1 and BayiEksik=0 and SahaEksik=0) as result3,
(Select Count(*) from Document Where LineID=45 and Esitlendi=1 and BayiEksik=1 and SahaEksik=0) as result4,
(Select Count(*) from Document Where LineID=45 and Esitlendi=1 and BayiEksik=1 and SahaEksik=1) as result5

谢谢

4

1 回答 1

1

一系列 CASE 语句可以解决问题:

SELECT SUM(CASE WHEN LINEID=45 AND Esitlendi=1 THEN 1 ELSE 0 END) Result1
     , SUM(CASE WHEN LineID=45 and Esitlendi=1 and TutanakUserID is not null THEN 1 ELSE 0 END) Result2
     , SUM(CASE WHEN LineID=45 and Esitlendi=1 and BayiEksik=0 and SahaEksik=0 THEN 1 ELSE 0 END) Result3
     , SUM(CASE WHEN LineID=45 and Esitlendi=1 and BayiEksik=1 and SahaEksik=0 THEN 1 ELSE 0 END) Result4
     , SUM(CASE WHEN LineID=45 and Esitlendi=1 and BayiEksik=1 and SahaEksik=1 THEN 1 ELSE 0 END) Result5
FROM Document

由于您有一些通用标准,您可以将它们移至 WHERE 子句:

编辑:我在您的示例中没有看到日期字段,但如果您在表中有一个,要显示每天的记录,您需要一个列出每一天的日历表:

SELECT SaveDate
     , COUNT(*) Result1
     , SUM(CASE WHEN TutanakUserID is not null THEN 1 ELSE 0 END) Result2
     , SUM(CASE WHEN BayiEksik=0 and SahaEksik=0 THEN 1 ELSE 0 END) Result3
     , SUM(CASE WHEN BayiEksik=1 and SahaEksik=0 THEN 1 ELSE 0 END) Result4
     , SUM(CASE WHEN BayiEksik=1 and SahaEksik=1 THEN 1 ELSE 0 END) Result5
FROM CalTable a
LEFT JOIN Document b
     ON a.date = b.SaveDate
WHERE LineID=45 and Esitlendi=1
GROUP BY SaveDate
ORDER BY SaveDate
于 2013-06-15T22:13:27.387 回答