我在SQL Server中有一个表,其中包含名为“surname”和“Date”(DateTime)的列。该表用于度假请求。如何构建SQL语句以查看每天将丢失多少人?关键是查询不会告诉我实际上8人将在9月2日失踪。例如,给出以下数据:
surname Date Duration
------- ---------- ---------
Bertram 2011-09-01 3
Coulois 2011-09-01 5
LeBlanc 2011-09-01 6
Fosters 2011-09-01 3
Blanche 2011-09-01 2
Bertram 2011-09-02 6
Gillian 2011-09-02 4
Pikklar 2011-09-02 7
Thierry 2011-09-03 6
Selanne 2011-09-03 6
或者
CREATE TABLE #VRs(surname NVARCHAR(32), [Date] DATETIME);
INSERT #VRs SELECT 'Bertram','2011-09-01'
UNION ALL SELECT 'Coulois','2011-09-01'
UNION ALL SELECT 'LeBlanc','2011-09-01'
UNION ALL SELECT 'Fosters','2011-09-01'
UNION ALL SELECT 'Blanche','2011-09-01'
UNION ALL SELECT 'Bertram','2011-09-02'
UNION ALL SELECT 'Gillian','2011-09-02'
UNION ALL SELECT 'Pikklar','2011-09-02'
UNION ALL SELECT 'Thierry','2011-09-03'
UNION ALL SELECT 'Selanne','2011-09-03';
我想要以下结果:
Date Count
----- -----
1 Sep 5
2 Sep 3
3 Sep 2
任何想法如何处理它?</ p>
答案 0 :(得分:0)
SELECT CONCAT(DAYOFMONTH(`date`), ' ', LEFT(MONTHNAME(`date`), 3)) AS Date,
COUNT(surname) AS Count FROM `#VRs` GROUP BY Date
此处,CONCAT
用于组合月份名称的日期和前三个字母。
答案 1 :(得分:0)
好吧,您可以按日期对假期请求进行分组,并返回每个日期的行数,如果我没有遗漏任何内容:
SELECT
[Date],
COUNT(*) AS [Count]
FROM #VRs