[英]How to return a specific row to the Bottom from a SQL Query results?
I have a simple sql query which return the data as 我有一个简单的SQL查询,它返回数据为
select [BusinessModel], count(*) As Total from DimHotelExpand
group by [BusinessModel]
The result comes as: 结果如下:
DA 53894
DL 7098
ECM 1472
Flex 14789
GDS 33487
Lead 1499050
MT 71306
Unknown 572467
I want the row with value 'Lead' to appear at the bottom of the result. 我希望值“ Lead”的行显示在结果的底部。 How can I do this? 我怎样才能做到这一点?
Try this one - 试试这个-
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
You can order it by your total column (since Lead's count is higher) like this: 您可以按总计列排序(因为Lead的计数更高),如下所示:
SELECT [BusinessModel], count(*) As Total from DimHotelExpand
GROUP BY [BusinessModel]
ORDER BY count(*)
You can try this, if you don't know if Lead has always the biggest value: 如果您不知道Lead是否始终具有最大价值,可以尝试一下:
select
[BusinessModel], count(*) As Total
from DimHotelExpand
group by
[BusinessModel]
order by
case [BusinessModel] when 'Lead' then 1 else 0 end asc
You can try with "union 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]
A solution with "Order by", I believe don't work fine. 我认为使用“排序依据”的解决方案效果不佳。 Only work fine while "Lead" is more greater bussines model. 仅当“ Lead”是更大的商务模型时才能正常工作。
I think frikozoid solution is also valid. 我认为frikozoid解决方案也是有效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.