如何从SQL查询结果返回特定行到底部?

时间:2013-04-22 07:39:51

标签: sql sql-server

我有一个简单的SQL查询,它将数据作为

返回
select [BusinessModel], count(*) As Total from DimHotelExpand
group by [BusinessModel] 

结果如下:

DA        53894
DL        7098
ECM       1472
Flex      14789
GDS       33487
Lead      1499050
MT        71306
Unknown   572467

我希望值为“Lead”的行显示在结果的底部。我怎样才能做到这一点?

4 个答案:

答案 0 :(得分:2)

试试这个 -

SELECT 
      d.BusinessModel
    , Total = COUNT(1)
FROM dbo.DimHotelExpand d
GROUP BY d.BusinessModel
ORDER BY 
      CASE WHEN d.BusinessModel = 'Lead' THEN 0 ELSE 1 END
    , Total

答案 1 :(得分:1)

您可以按照总栏目订购(因为Lead的数量更高),如下所示:

SELECT [BusinessModel], count(*) As Total from DimHotelExpand
GROUP BY [BusinessModel]
ORDER BY count(*)

答案 2 :(得分:1)

如果您不知道Lead是否始终具有最大价值,您可以试试这个:

select 
    [BusinessModel], count(*) As Total 
from DimHotelExpand
group by 
    [BusinessModel] 
order by 
    case [BusinessModel] when 'Lead' then 1 else 0 end asc

答案 3 :(得分:1)

您可以尝试使用“all all”。

select [BusinessModel], count(*) As Total from DimHotelExpand
where [BusinessModel] <> 'Lead'
group by [BusinessModel] 
union all
select [BusinessModel], count(*) As Total from DimHotelExpand
where [BusinessModel] = 'Lead'
group by [BusinessModel] 

使用“Order by”的解决方案,我相信不能正常工作。只有工作正常,而“领导”是更大的商务模式。

我认为frikozoid解决方案也是有效的。