简体   繁体   English

sql:按日期排序

[英]sql: order by Date

I would like to order the table by date but my query displays an error: 我想按日期排序表,但查询显示错误:

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified. 除非还指定了TOP,OFFSET或FOR XML,否则ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效。

My Query: 我的查询:

    select intervaldate, [M1], [M2], [M3], [M4], [M5], [M6], [M7], [M8]
    from 
    (select intervaldate,amount, name from (Select tSystem.Name, IntervalCount.IntervalDate, 
    sum(case when CounterName = 'Prod' then calculationUnits else 0 end) as Amount from IntervalCount, tsystem where 

    IntervalCount.intervaldate between '2017-03-06 00:00:00.000' and '2017-03-08 00:00:00.000' and IntervalCount.systemid =tsystem.id 

    group by tSystem.Name, IntervalCount.Intervaldate
order by IntervalCount.Intervaldate asc
    ) as T3) as T1 
    pivot 
    (sum (amount)
    for name in ([M1], [M2], [M3], [M4], [M5], [M6], [M7], [M8])

    ) as t2

how can I order by IntervalDate? 如何通过IntervalDate订购?

As message error said you can't use order by in derived tables (in your case). 如消息错误所述,您不能在派生表中使用order by (针对您的情况)。 You should replace order by to the end of query as below 您应将order by替换为查询末尾,如下所示

    select intervaldate, [M1], [M2], [M3], [M4], [M5], [M6], [M7], [M8]
    from 
    (select intervaldate,amount, name from (Select tSystem.Name, IntervalCount.IntervalDate, 
    sum(case when CounterName = 'Prod' then Units else 0 end) as Amount from IntervalCount, tsystem where 

    IntervalCount.intervaldate between '2017-03-06 00:00:00.000' and '2017-03-08 00:00:00.000' and IntervalCount.systemid =tsystem.id 

    group by tSystem.Name, IntervalCount.Intervaldate
order by IntervalCount.Intervaldate asc
    ) as T3) as T1 
    pivot 
    (sum (amount)
    for name in ([M1], [M2], [M3], [M4], [M5], [M6], [M7], [M8])

    ) as t2
    order by Intervaldate asc

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

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