请教大师,一个Sql语句排序问题.

2024-12-11 18:56:16
推荐回答(3个)
回答1:

select * from (
select * from (
select top 3 mdg,qsg,count(mdg) cont from HxTable
where Qsg ='青岛' group by mdg,qsg
order by count(mdg) desc)t1
union all

select * from(
select top 2 mdg,qsg,count(mdg) cont from HxTable
where Qsg ='宁波' group by mdg,qsg
order by count(mdg) desc)t2
union all

select * from (
select top 3 mdg,qsg,count(mdg) cont from HxTable
where Qsg ='上海' group by mdg,qsg
order by count(mdg) desc)t3
) as result
order by cont desc

回答2:

select qsg,count(mdg),cont from(
select top 3 mdg,qsg,count(mdg) cont from HxTable
where Qsg ='青岛'
union all
select top 2 mdg,qsg,count(mdg) cont from HxTable
where Qsg ='宁波'
union all
select top 3 mdg,qsg,count(mdg) cont from HxTable
where Qsg ='上海' ) t1 group by mdg,qsg
order by count(mdg) desc

回答3:

这个问题好像刚刚回答过。