0

到目前为止,我有这个查询

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;
4

2 回答 2

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

应该做的伎俩。

于 2013-10-10T17:33:03.123 回答
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)

我相信您可以将子字符串位添加到 select 语句中,然后按顺序添加总和。它默认为升序,将 desc 放在语句的末尾以反转它

于 2013-10-10T17:34:25.207 回答