到目前为止,我有这个查询
select count(*) as howMany,sum(Duration) as totalTime
from [Tmp].[dbo].[tmp2]
group by SUBSTRING(TextData,1,25)
可以正常返回两列:howMany和totalTIme。我希望它还返回一个包含SUBSTRING(TextData,1,25)结果的列,每个组都是相同的。但是我不知道如何修改查询来完成此任务。
我还想按totalTime排序的结果,但以下尝试无法编译
select *
from (select count(*) as howMany,sum(Duration) as totalTime
from [Tmp].[dbo].[tmp2]
group by SUBSTRING(TextData,1,25) )
order by totalTime;
答案 0 :(得分:2)
select SUBSTRING(TextData,1,25),
count(*) as howMany,
sum(Duration) as totalTime
from [Tmp].[dbo].[tmp2]
group by SUBSTRING(TextData,1,25)
order by 3 --3rd column
应该做的伎俩。
答案 1 :(得分:1)
select SUBSTRING(TextData,1,25) as textdata, count(*) as howMany,sum(Duration) as totalTime
from [Tmp].[dbo].[tmp2]
group by SUBSTRING(TextData,1,25)
order by sum(duration)
我相信您可以将substring位添加到select语句中,并按顺序添加总和。它默认为升序,将desc放在语句的末尾以反转