我正在尝试获取具有增量日期的行数。
我的表格如下:
ID name status create_date
1 John AC 2016-01-01 00:00:26.513
2 Jane AC 2016-01-02 00:00:26.513
3 Kane AC 2016-01-02 00:00:26.513
4 Carl AC 2016-01-03 00:00:26.513
5 Dave AC 2016-01-04 00:00:26.513
6 Gina AC 2016-01-04 00:00:26.513
现在我想从SQL返回的内容是这样的:
Date Count
2016-01-01 1
2016-01-02 3
2016-01-03 4
2016-01-04 6
答案 0 :(得分:3)
使用COUNT() OVER ()
,您可以在PARTITION BY
之后使用ORDER BY
。它将为您提供累积总和。使用DISTINCT
过滤掉重复的值。
SELECT DISTINCT CAST(create_date AS DATE) [Date],
COUNT(create_date) OVER (ORDER BY CAST(create_date AS DATE)) as [COUNT]
FROM [YourTable]
答案 1 :(得分:0)
select r.date,count(r.date) count
from
(
select id,name,substring(convert(nvarchar(50),create_date),1,10) date
from tblName
) r
group by r.date
在此代码中,在子查询部分中, 我选择从dateTime转换为nvarchar的前10个日期字母,所以我做的就像'2016-01-01'。 (这也不是必需的,但为了使代码更具可读性,我更喜欢以这种方式实现)。 然后用一个简单的组我有日期和日期的计数。
答案 2 :(得分:0)
SELECT create_date, COUNT(create_date) as [COUNT]
FROM (
SELECT CAST(create_date AS DATE) create_date
FROM [YourTable]
) T
GROUP BY create_date
答案 3 :(得分:0)
根据您的描述,您需要一个连续的日期列表,它是否有意义? 此示例仅生成一个月的数据。
CREATE TABLE #tt(ID INT, name VARCHAR(10), status VARCHAR(10), create_date DATETIME)
INSERT INTO #tt
SELECT 1,'John','AC','2016-01-01 00:00:26.513' UNION
SELECT 2,'Jane','AC','2016-01-02 00:00:26.513' UNION
SELECT 3,'Kane','AC','2016-01-02 00:00:26.513' UNION
SELECT 4,'Carl','AC','2016-01-03 00:00:26.513' UNION
SELECT 5,'Dave','AC','2016-01-04 00:00:26.513' UNION
SELECT 6,'Gina','AC','2016-01-04 00:00:26.513' UNION
SELECT 7,'Tina','AC','2016-01-08 00:00:26.513'
SELECT * FROM #tt
SELECT CONVERT(DATE,DATEADD(d,sv.number,n.FirstDate)) AS [Date],COUNT(n.num) AS [Count]
FROM master.dbo.spt_values AS sv
LEFT JOIN (
SELECT MIN(t.create_date)OVER() AS FirstDate,DATEDIFF(d,MIN(t.create_date)OVER(),t.create_date) AS num FROM #tt AS t
) AS n ON n.num<=sv.number
WHERE sv.type='P' AND sv.number>=0 AND MONTH(DATEADD(d,sv.number,n.FirstDate))=MONTH(n.FirstDate)
GROUP BY CONVERT(DATE,DATEADD(d,sv.number,n.FirstDate))
Date Count ---------- ----------- 2016-01-01 1 2016-01-02 3 2016-01-03 4 2016-01-04 6 2016-01-05 6 2016-01-06 6 2016-01-07 6 2016-01-08 7 2016-01-09 7 2016-01-10 7 2016-01-11 7 2016-01-12 7 2016-01-13 7 2016-01-14 7 2016-01-15 7 2016-01-16 7 2016-01-17 7 2016-01-18 7 2016-01-19 7 2016-01-20 7 2016-01-21 7 2016-01-22 7 2016-01-23 7 2016-01-24 7 2016-01-25 7 2016-01-26 7 2016-01-27 7 2016-01-28 7 2016-01-29 7 2016-01-30 7 2016-01-31 7 2017-01-01 7 2017-01-02 7 2017-01-03 7 2017-01-04 7 2017-01-05 7 2017-01-06 7 2017-01-07 7 2017-01-08 7 2017-01-09 7 2017-01-10 7 2017-01-11 7 2017-01-12 7 2017-01-13 7 2017-01-14 7 2017-01-15 7 2017-01-16 7 2017-01-17 7 2017-01-18 7 2017-01-19 7 2017-01-20 7 2017-01-21 7 2017-01-22 7 2017-01-23 7 2017-01-24 7 2017-01-25 7 2017-01-26 7 2017-01-27 7 2017-01-28 7 2017-01-29 7 2017-01-30 7 2017-01-31 7 2018-01-01 7 2018-01-02 7 2018-01-03 7 2018-01-04 7 2018-01-05 7 2018-01-06 7 2018-01-07 7 2018-01-08 7 2018-01-09 7 2018-01-10 7 2018-01-11 7 2018-01-12 7 2018-01-13 7 2018-01-14 7 2018-01-15 7 2018-01-16 7 2018-01-17 7 2018-01-18 7 2018-01-19 7 2018-01-20 7 2018-01-21 7 2018-01-22 7 2018-01-23 7 2018-01-24 7 2018-01-25 7 2018-01-26 7 2018-01-27 7 2018-01-28 7 2018-01-29 7 2018-01-30 7 2018-01-31 7 2019-01-01 7 2019-01-02 7 2019-01-03 7 2019-01-04 7 2019-01-05 7 2019-01-06 7 2019-01-07 7 2019-01-08 7 2019-01-09 7 2019-01-10 7 2019-01-11 7 2019-01-12 7 2019-01-13 7 2019-01-14 7 2019-01-15 7 2019-01-16 7 2019-01-17 7 2019-01-18 7 2019-01-19 7 2019-01-20 7 2019-01-21 7 2019-01-22 7 2019-01-23 7 2019-01-24 7 2019-01-25 7 2019-01-26 7 2019-01-27 7 2019-01-28 7 2019-01-29 7 2019-01-30 7 2019-01-31 7 2020-01-01 7 2020-01-02 7 2020-01-03 7 2020-01-04 7 2020-01-05 7 2020-01-06 7 2020-01-07 7 2020-01-08 7 2020-01-09 7 2020-01-10 7 2020-01-11 7 2020-01-12 7 2020-01-13 7 2020-01-14 7 2020-01-15 7 2020-01-16 7 2020-01-17 7 2020-01-18 7 2020-01-19 7 2020-01-20 7 2020-01-21 7 2020-01-22 7 2020-01-23 7 2020-01-24 7 2020-01-25 7 2020-01-26 7 2020-01-27 7 2020-01-28 7 2020-01-29 7 2020-01-30 7 2020-01-31 7 2021-01-01 7 2021-01-02 7 2021-01-03 7 2021-01-04 7 2021-01-05 7 2021-01-06 7 2021-01-07 7 2021-01-08 7 2021-01-09 7 2021-01-10 7 2021-01-11 7 2021-01-12 7 2021-01-13 7 2021-01-14 7 2021-01-15 7 2021-01-16 7 2021-01-17 7 2021-01-18 7 2021-01-19 7 2021-01-20 7 2021-01-21 7 2021-01-22 7 2021-01-23 7 2021-01-24 7 2021-01-25 7 2021-01-26 7 2021-01-27 7 2021-01-28 7 2021-01-29 7 2021-01-30 7 2021-01-31 7