在SQL Server 2008中关键字'asc by Order by附近的语法不正确 [英] Incorrect syntax near the keyword 'asc by Order by in sql server 2008

查看:99
本文介绍了在SQL Server 2008中关键字'asc by Order by附近的语法不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好所有访客
在我的查询中,我将``5 AM-7AM''选择为``小时'',将sum(field)作为值
它显示了5行,就像这样
小时_______________值
5 AM-7AM __________ 409
9 AM-10AM __________ 545
8 AM-9AM __________ 885
10 AM-11AM _________ 7353
7 AM-8AM __________ 788

我如何像这样使用 order by 进行排序?

小时_______________值
5 AM-7AM __________ 409
7 AM-8AM __________ 788
8 AM-9AM __________ 885
9 AM-10AM __________ 545
10 AM-11AM _________ 7353

我也按(LEFT(T0.Hour,CHARINDEX(''-'',T0.Hour-1))asc这样使用 order
但它显示了错误关键字"asc"附近的语法不正确""
为此请帮我.
最好的问候,

Hi all visitors
In my query,I select ''5AM-7AM'' as ''Hour'',sum(field) as value
and it shows 5 rows like this
Hour_____________value
5AM-7AM__________409
9AM-10AM__________545
8AM-9AM__________885
10AM-11AM_________7353
7AM-8AM__________788

How can i sort by using order by like this?

Hour_____________value
5AM-7AM__________409
7AM-8AM__________788
8AM-9AM__________885
9AM-10AM__________545
10AM-11AM_________7353

I also use like this order by (LEFT(T0.Hour,CHARINDEX(''-'', T0.Hour-1)) asc
but it shows me the error "Incorrect syntax near the keyword ''asc''"
Please help me for this.
Best Regards,

推荐答案

如果正确粘贴/输入了代码,则您的支撑数不匹配. 3''(''and only 2'')''
If your code is correctly pasted/entered you have a mismatch in the Brace count. 3 ''('' and only 2 '')''


您没有提到小时数的来源.如果您有实际的时间信息,则可以将其用于排序.如果没有,也许您可​​以生成必要的范围(并加入相关信息以进行总和计算等).例如,类似这样的东西可以用来生成时间范围:
You didn''t mention where you get the hour ranges from. If you have the actual time information you could use that for sorting. If you don''t have it, perhaps you can generate the necessary ranges (and join the the relevant information for sum calculation etc). For example something like this could be used to generate the time ranges:
select *
from (select CAST('05:00' AS TIME) AS StartTime, CAST('07:00' AS TIME) AS EndTime UNION ALL
      select CAST('08:00' AS TIME) AS StartTime, CAST('09:00' AS TIME) AS EndTime UNION ALL
      select CAST('09:00' AS TIME) AS StartTime, CAST('10:00' AS TIME) AS EndTime UNION ALL
      select CAST('10:00' AS TIME) AS StartTime, CAST('11:00' AS TIME) AS EndTime) times
order by times.StartTime


这篇关于在SQL Server 2008中关键字'asc by Order by附近的语法不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆