MS-SQL 按降序排序输出

MS-SQL sort output in descending order(MS-SQL 按降序排序输出)
本文介绍了MS-SQL 按降序排序输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 MS-SQL 查询,数据库中有数千行记录:

I have this MS-SQL query with thousands of row records in database:

SELECT DISTINCT TOP 7 DATENAME(MM, mydatetime) + ' ' + CAST(DAY(mydatetime) AS VARCHAR(2)) as thedate
    , MONTH(mydatetime)
    , DAY(mydatetime)
    , COUNT(Page) as totalcount
    , count(DISTINCT Page) as visitors
  FROM someTable
  WHERE Page LIKE '%AEC%'
  GROUP BY DATENAME(MM, mydatetime) + ' ' + CAST(DAY(mydatetime) AS VARCHAR(2))
    , MONTH(mydatetime)
    , DAY(mydatetime)
  ORDER BY MONTH(mydatetime) DESC
    , DAY(mydatetime) DESC

它会输出这个:

It will output this:

thedate     | totalcount | visitors
-----------------------------------
October 17  |     4      |    1
October 15  |     1      |    1
October 12  |     1      |    1
October 3   |     3      |    3
October 2   |     42     |    22
September 28|     2      |    1
September 21|     14     |    10

我的问题是我根本无法按降序输出,所以它看起来像这样:

My problem is that I simply cant output this in descending order so it will look like this:

thedate     | totalcount | visitors
-----------------------------------
September 21|     14     |    10
September 28|     2      |    1
October 2   |     42     |    22
October 3   |     3      |    3
October 12  |     1      |    1
October 15  |     1      |    1
October 17  |     4      |    1

非常感谢任何帮助.

推荐答案

对于您的实际需求,您可以将当前查询用作派生表并按照您想要的方式排序:

For your actual requirement, you can use your current query as a derived table and order that result in the way you want:

SELECT *
FROM (  SELECT DISTINCT TOP 7 DATENAME(mm, mydatetime) + ' ' 
                              + CAST(DAY(mydatetime) AS VARCHAR(2)) AS thedate, 
                              MONTH(mydatetime)                     AS theMonth, 
                              DAY(mydatetime)                       AS theDay, 
                              COUNT(page)                           AS totalcount, 
                              COUNT(DISTINCT page)                  AS visitors 
        FROM   sometable 
        WHERE  page LIKE '%AEC%' 
        GROUP  BY DATENAME(mm, mydatetime) + ' ' 
                  + CAST(DAY(mydatetime) AS VARCHAR(2)), 
                  MONTH(mydatetime), 
                  DAY(mydatetime) 
        ORDER  BY MONTH(mydatetime) DESC, 
                  DAY(mydatetime) DESC) A
ORDER BY theMonth, theDay

这篇关于MS-SQL 按降序排序输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)