【问题标题】:sql: order by Datesql:按日期排序
【发布时间】:2017-03-08 13:44:43
【问题描述】:

我想按日期对表格进行排序,但我的查询显示错误:

ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效,除非还指定了 TOP、OFFSET 或 FOR XML。

我的查询:

    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

如何按 IntervalDate 订购?

【问题讨论】:

  • 您使用的是哪个 DBMS?

标签: sql


【解决方案1】:

正如消息错误所说,您不能在派生表中使用order by(在您的情况下)。 您应该将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

【讨论】:

    猜你喜欢
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 2012-09-09
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多