8

I have a table with the following fields in an SQL Server 2005 database:

  • id, integer
  • value, string
  • create_date, datetime

New data is constantly being inserted into this table (tens of thousands of records per day) so I use the following query to compare how much data has been inserted on different days.

SELECT CONVERT(varchar(10), create_date, 101) as 'Date', COUNT(*) as 'Record Count',
FROM the_table
GROUP BY CONVERT(varchar(10), create_date, 101)
ORDER BY 'Date' desc

This query returns data looking like this:

12/20/2012 | 48155
12/19/2012 | 87561
12/18/2012 | 71467

However, when running this query today, I noticed the sorting did not work as expected with multiple years worth of data in the database. Instead of the data for this year being at the very top of the result set, it ended up at the bottom (records omitted for clarity)

06/29/2012 | 9987
01/04/2013 | 15768
01/03/2013 | 77586
01/02/2013 | 23566

I understand why this is happening, as my formatted date is simply a string, and sql server can't possibly be expected to sort it as anything but a string. But I would like the ordering to be accurate. How can I achieve this? (the most recent day always appearing first)

4

7 回答 7

13

Thanks to Oded's suggestion I changed my order by clause and this seems to give me what I want:

SELECT CONVERT(varchar(10), create_date, 101) as 'Date', COUNT(*) as 'Record Count',
FROM the_table
GROUP BY CONVERT(varchar(10), create_date, 101)
ORDER BY MIN(create_date) desc
于 2013-01-04T19:36:35.987 回答
4

您可以将日期作为日期数据类型包含在 中GROUP BY,然后在ORDER BY

SELECT top 100 CONVERT(varchar, create_date, 101) as 'Date', COUNT(*) as 'Record Count'
FROM constituent
GROUP BY CONVERT(varchar, create_date, 101), CONVERT(date, create_date)
ORDER BY CONVERT(date, create_date)
于 2013-01-04T19:40:30.697 回答
0

您可以将日期截断为上午 12:00,而不是转换为字符串:

SELECT dateadd(dd, datediff(dd, 0, create_date), 0) as 'Date'
     , COUNT(*) as 'Record Count',
  FROM the_table
 GROUP BY dateadd(dd, datediff(dd, 0, create_date), 0)
 ORDER BY dateadd(dd, datediff(dd, 0, create_date), 0) desc
于 2013-01-04T19:40:42.423 回答
0

数据是否必须只有您指定的两列?如果没有,您可以选择截断为午夜的日期(如user1948904建议的那样)以及格式化日期字段,然后按日期字段排序。然后,您可以忽略任何使用数据的日期字段。

编辑以更正原始查询中的错误,并将格式化日期字段添加到GROUP BY,这是必需的。

SELECT DATEADD(DAY, 0, DATEDIFF(DAY, 0, create_date)) AS raw_date,
        CONVERT(VARCHAR(10), create_date, 101) AS 'Date',
        COUNT(*) AS 'Record Count',
FROM the_table
GROUP BY DATEADD(DAY, 0, DATEDIFF(DAY, 0, create_date)),
        CONVERT(VARCHAR(10), create_date, 101)
ORDER BY raw_date DESC
于 2013-01-05T02:28:00.877 回答
0

您可能可以 substr 然后按年份 desc 排序,然后按月份 asc 和日期 asc 排序。

于 2013-01-04T21:18:00.103 回答
0

我发现其他答案不适合我的情况,因为我不想要额外的冗余日期列,或者GROUP BY如果我没有真正聚合查询中的任何信息(假设 OP 的问题包括count(*)- 我的情况是相同的,除了我'没有汇总)。

这个解决方案使用一个DATEADD()实际上并没有做任何事情来强制 SQL Server 将其视为实际日期并返回正确的顺序。

SELECT    [Date]  = CONVERT(varchar(10), t.[create_date], 101)
          [Thing] = t.[other_column]     -- that I don't want to aggregate
FROM      [db].[dbo].[mytable] t
ORDER BY  DATEADD(dd, 0, t.[create_date]) DESC
于 2015-08-14T10:02:52.443 回答
-1

I don't know anything about sql-server but I'll try to help. You should replace this column with one that is a Date type. I'm sure sql-server will know how to sort that correctly.

If that isn't an option for you, maybe in sql-server you can order by a function that converts the string to a date type.


But it already looks like you're using a date type here. I think you should just expand your query to include the date column in the select as the date type and sort by that column instead of the converted column.

于 2013-01-04T19:34:15.100 回答