基本上我需要一个sql查询,它将返回1列的不同值,然后修剪(DateTime)结果以仅显示日期。而我的其他列应该包含当天的项目数。
我尝试了以下内容:
Select LoggedDate as Date, count(ID) as Amount from Tickets WHERE LoggedDate >=dateadd(day,datediff(day,0,GetDate())- 30,0) AND State = '1' group by LoggedDate
ID | Date | Amount
----------------------------------------
1 | 2016-09-23 11:54:12.000 | 1
2 | 2016-09-29 09:23:58.000 | 1
3 | 2016-09-29 09:34:07.000 | 1
4 | 2016-09-29 09:41:12.000 | 1
5 | 2016-09-29 09:53:37.000 | 1
6 | 2016-09-29 09:57:17.000 | 1
7 | 2016-09-29 10:01:53.000 | 1
8 | 2016-10-03 21:10:27.000 | 1
9 | 2016-10-04 03:20:51.000 | 1
10 | 2016-10-04 03:47:34.000 | 1
11 | 2016-10-04 03:50:09.000 | 1
我尝试过类似的事情:
Select DISTINCT(LEFT(LoggedDate, 11)) AS Date, count(ID) as Amount from Tickets WHERE LoggedDate >=dateadd(day,datediff(day,0,GetDate())- 30,0) AND State = '1' group by LoggedDate
ID | Date | Amount
----------------------------
1 | OCT 3 2016 |1
2 | OCT 2 2016 |1
3 | SEP 22 2016 |1
4 | SEP 23 2016 |1
5 | SEP 29 2016 |1
我想要下面但不确定如何获取我的数据:
ID | Date | Amount
---------------------------
1 | 2016-09-23 |1
2 | 2016-09-29 |5
3 | 2016-10-03 |1
4 | 2016-10-03 |1
5 | 2016-10-04 |2
答案 0 :(得分:0)
如果我做对了,你可以试试这个:
SELECT ROW_NUMBER() Over(ORDER BY Date) [Id], *
FROM
(
SELECT CONVERT(VARCHAR(35), LoggedDate, 106) [Date], SUM(Amount) [Total]
FROM Tickets
GROUP BY CONVERT(VARCHAR(35), LoggedDate, 106)
) a
您可以通过更改CONVERT
函数中的第三个参数来试验日期格式。
您还需要提出任何条件等。
答案 1 :(得分:0)
我认为你需要这样的东西:
SELECT DATE(LoggedDate) AS date, COUNT(id) AS Amount
FROM Tickets
WHERE state = 1
GROUP BY DATE(LoggedDate);
答案 2 :(得分:0)
尝试使用以下脚本。
SELECT ROW_NUMBER() OVER ( ORDER BY (select 1) ) AS id
,CAST(LoggedDate as date) AS Date
, count(ID) as Amount
FROM Tickets
WHERE LoggedDate >=dateadd(day,datediff(day,0,GetDate())- 30,0) AND State = '1'
GROUP BY CAST(LoggedDate as date)
答案 3 :(得分:0)
我只是猜测你的表结构,但这是我放在一起的示例代码:
DECLARE @t TABLE ( id INT, loggedDate DATETIME )
INSERT INTO @t ( id, loggedDate ) VALUES ( 1, '2016-09-23 11:54:12.000' )
INSERT INTO @t ( id, loggedDate ) VALUES ( 2, '2016-09-29 09:23:58.000' )
INSERT INTO @t ( id, loggedDate ) VALUES ( 3, '2016-09-29 09:34:07.000' )
INSERT INTO @t ( id, loggedDate ) VALUES ( 4, '2016-09-29 09:41:12.000' )
INSERT INTO @t ( id, loggedDate ) VALUES ( 5, '2016-09-29 09:53:37.000' )
INSERT INTO @t ( id, loggedDate ) VALUES ( 6, '2016-09-29 09:57:17.000' )
INSERT INTO @t ( id, loggedDate ) VALUES ( 7, '2016-09-29 10:01:53.000' )
INSERT INTO @t ( id, loggedDate ) VALUES ( 8, '2016-10-03 21:10:27.000' )
INSERT INTO @t ( id, loggedDate ) VALUES ( 9, '2016-10-04 03:20:51.000' )
INSERT INTO @t ( id, loggedDate ) VALUES ( 10, '2016-10-04 03:47:34.000' )
INSERT INTO @t ( id, loggedDate ) VALUES ( 11, '2016-10-04 03:50:09.000' )
SELECT ROW_NUMBER() OVER ( ORDER BY CONVERT(DATE,loggedDate) ) AS id, CONVERT(DATE, loggedDate) AS loggedDate, COUNT(1) AS amount
FROM @t
GROUP BY CONVERT(DATE,loggedDate)
/* output
id loggedDate amount
1 2016-09-23 1
2 2016-09-29 6
3 2016-10-03 1
4 2016-10-04 3
*/