1. 程式人生 > >sql復雜的子查詢,橫向合並結果集

sql復雜的子查詢,橫向合並結果集

group 子查詢 rtti ediff sid 連接 all total top

第一個查詢的結果集

select * from(
select c.msName,a.msId,c.msPrice, c.msPrice*COUNT(a.msId) as totalMoney,sum(a.msAmount) as totalCount    from BillConsume a right join 
 (select top 1 msTime as startTime,GETDATE() as endTime from BillConsume where DateDiff(dd,msTime,getdate())=0  order by msTime asc) b on a.msTime between b.startTime and b.endTime and a.msId!=0
left join Menus c on c.msId=a.msId group by a.msId ,c.msName,c.msPrice)s

連接兩個結果集的語句

 full join 

第二個查詢的結果集

(select sum(t.totalMoney) as allTotalMoney   from (
 select c.msName,a.msId,c.msPrice, c.msPrice*COUNT(a.msId) as totalMoney,sum(a.msAmount) as totalCount  from BillConsume a right join 
 (
select top 1 msTime as startTime,GETDATE() as endTime from BillConsume where DateDiff(dd,msTime,getdate())=0 order by msTime asc) b on a.msTime between b.startTime and b.endTime and a.msId!=0 left join Menus c on c.msId=a.msId group by a.msId ,c.msName,c.msPrice)t)t

連表的條件

on 1=1

sql復雜的子查詢,橫向合並結果集