SQLServer多个子查询结果拼接和分组查询最新一组数据

多个子查询结果拼接

select * from (
select count(id) cont1 from table_a where delflag = 0 
) a
FULL JOIN (
select count(id) cont2 from table_b where delflag = 0
) b on 1=1
FULL JOIN (
select count(id) cont3 from table_c where delflag = 0 
) c on 1=1

分组取最新一条

select * from (
SELECT  ROW_NUMBER() over (PARTITION By my_type order by my_index desc) as inde,*
FROM (
SELECT my_type,my_index FROM TABLE_a
) c
) t
where inde = 1
--根据my_type分组,根据my_index排序,排序后取第一个(inde = 1)

分组查询最新一组数据

select top 7 * from (
select row_number() over(partition by [c1] order by [c2] desc) as rid,*
from table_A where type = '1' 
) as tt where rid =1 
order by case hymc when '其他' then 999 else 1 end asc ,CAST (c3 AS NUMERIC(10,2) ) desc
--备注:根据c1分组 ,根据c2排序,最终结果中“其他”排在最后,再根据c3倒序

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值