简体   繁体   English

T-SQL转换每个日期的DateTime和Count条目

[英]T-SQL Convert DateTime and Count entries for each date

I am lost on how to solve what I believe should be a simple query. 我不知道该如何解决我认为应该是一个简单查询的问题。

I want to count the number of entries for each date in a table. 我想计算表中每个日期的条目数。 The column DateCreated is in DateTime format. DateCreatedDateTime格式。 I can convert the datetime to date using 我可以使用将datetime时间转换为日期

convert(VARCHAR, JobApps.DateCreated, 2) as Date

but with COUNT(ID) as Qty I get a count of 1 with multiple "Date" rows. 但是使用COUNT(ID) as Qty我得到了多个“日期”行的计数1。

Here is the SQL query I am using. 这是我正在使用的SQL查询。

SELECT  
    convert(VARCHAR, DateCreated, 2) as Date, COUNT(CompanyName) as Qty
FROM Apps  
GROUP BY DateCreated
ORDER BY DateCreated DESC

This is the results I get. 这是我得到的结果。

Date       Qty
------------------
13.05.29   1
13.05.29   1
13.05.29   1
13.05.29   1
13.05.29   1
13.05.28   1
13.05.28   1
13.05.27   1
13.05.27   1

etc... 等等...

What I wanting is a result like this... 我想要的是这样的结果...

Date       Qty
-----------------
13.05.29   5
13.05.28   2
13.05.27   2

etc... 等等...

Just gotta change your GROUP BY to use the actual value you want: 只需更改您的GROUP BY以使用所需的实际值即可:

SELECT convert(VARCHAR, DateCreated, 2) as Date, COUNT(1) as Qty
FROM Apps
GROUP BY convert(VARCHAR, DateCreated, 2)
ORDER BY Date DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM