合并2个查询以获得单个结果集的问题

时间:2018-12-11 22:03:35

标签: sql-server nested

我为SQL Server写了两个查询,一个查询提供“总收入”,另一个提供“运营商总计”。我已确认两者均能正常工作。现在,我尝试通过以下查询将所有这些数据提取到一组结果中。

select 
    a.ctripnumber, 'Revenue Total', 'Carrier Total' 
from 
    (
        (select r.ctripnumber, sum(Revenue_Subtotal) as 'Revenue Total' 
         from 
             (select 
                  r.ctripnumber, r.cmethod, sum(r.curevenue) as Revenue_Subtotal
              from  
                  revenuedtl r 
              where 
                  r.cmethod in (select distinct r.cmethod from revenuedtl r)
                  --and r.ctripnumber = '324198'
              group by 
                  r.ctripnumber, r.cmethod) r
         group by 
             r.ctripnumber),
        (select p.ctripnumber, sum(Carrier_Subtotal) as 'Carrier Total' 
         from 
             (select 
                  p.ctripnumber, p.cmethod, sum(p.curevenue) as Carrier_Subtotal 
              from 
                  payrolldtl p 
              where 
                  p.cmethod in (select distinct p.cmethod from payrolldtl p) 
                  --and p.ctripnumber = '324198'
              group by 
                  p.ctripnumber, p.cmethod) p
         group by p.ctripnumber)) a
where 
    p.ctripnumber = r.ctripnumber
group by 
    a.ctripnumber

但是,我遇到了这些错误:

  

第15级州立1行11的消息102
  ','附近的语法不正确。

     

第15级州立1行20的消息102
  ')'附近的语法不正确。

“,”似乎应该是正确的,因为我正试图从三列中获取结果。我勾选了')',没有看到任何孤立的。

我已尝试使用内部联接进行以下操作,但出现了一些不同的错误

select 
    r.ctripnumber, 'Revenue Total', 'Carrier Total'
    (select r.ctripnumber, sum(Revenue_Subtotal) as 'Revenue Total' 
     from 
         (select r.ctripnumber,r.cmethod,sum(r.curevenue) as Revenue_Subtotal
          from revenuedtl r 
          where r.cmethod in (select distinct r.cmethod from revenuedtl r)
          group by r.ctripnumber, r.cmethod) r
     group by r.ctripnumber)
inner join
    (select p.ctripnumber,sum(Carrier_Subtotal) as 'Carrier Total' 
     from 
         (select p.ctripnumber,p.cmethod,sum(p.curevenue) as Carrier_Subtotal 
          from payrolldtl p 
          where p.cmethod in (select distinct p.cmethod from payrolldtl p) 
          group by p.ctripnumber, p.cmethod) p
     group by p.ctripnumber) on r.ctripnumber = p.ctripnumber

这次我得到的错误是:

  

第156条消息,第15级,状态1,第9行
  关键字“内部”附近的语法不正确

     

第15行,州1,第17行的消息156
  关键字“ on”附近的语法不正确

     

澄清

有两个运行良好的查询,一个查询可以增加旅行次数和收入,另一个查询可以增加旅行次数和运营商费用。

  

查询1

select r.ctripnumber, sum(Revenue_Subtotal) as 'Revenue Total' 
         from 
             (select r.ctripnumber, r.cmethod, sum(r.curevenue) as Revenue_Subtotal
              from revenuedtl r 
              where r.cmethod in 
                  (select distinct r.cmethod from revenuedtl r)
              group by r.ctripnumber,r.cmethod) r
         group by r.ctripnumber
  

部分结果是:

ctripnumber Revenue Total
160837  682.00
232813  1151.75
163973  335.00
249132  3024.00
  

查询2

select p.ctripnumber, sum(Carrier_Subtotal) as 'Carrier Total' 
         from 
             (select p.ctripnumber, p.cmethod, sum(p.curevenue) as Carrier_Subtotal 
              from payrolldtl p 
              where p.cmethod in 
                  (select distinct p.cmethod from payrolldtl p) 
         group by p.ctripnumber, p.cmethod) p
    group by p.ctripnumber
  

部分结果是:

ctripnumber Carrier Total
206266  48.00
111099  225.00
107283  206.25
6558    250.00
  

现在我有这些查询,我想合并结果,所以我   写道:

select 
    a.ctripnumber, 'Revenue Total', 'Carrier Total' 
from 
    (
        (select r.ctripnumber, sum(Revenue_Subtotal) as 'Revenue Total' 
         from 
             (select r.ctripnumber, r.cmethod, sum(r.curevenue) as Revenue_Subtotal
              from revenuedtl r 
              where r.cmethod in 
                  (select distinct r.cmethod from revenuedtl r)
              group by r.ctripnumber,r.cmethod) r
         group by r.ctripnumber),
        (select p.ctripnumber, sum(Carrier_Subtotal) as 'Carrier Total' 
         from 
             (select p.ctripnumber, p.cmethod, sum(p.curevenue) as Carrier_Subtotal 
              from payrolldtl p 
              where p.cmethod in 
                  (select distinct p.cmethod from payrolldtl p) 
         group by p.ctripnumber, p.cmethod) p
    group by p.ctripnumber)
) a
where p.ctripnumber = r.ctripnumber
group by a.ctripnumber
  

我遇到以下错误:

     

消息102,级别15,状态1,第12行','附近的语法不正确。

     

消息102,第15级,状态1,第21行,')'附近的语法不正确。

1 个答案:

答案 0 :(得分:0)

我不建议在列名中使用空格,但这对您有用吗?

select 
    tRev.ctripnumber, "Revenue Total", "Carrier Total"
FROM
(select r.ctripnumber, sum(Revenue_Subtotal) as "Revenue Total" 
 from 
     (select r.ctripnumber,r.cmethod,sum(r.curevenue) as Revenue_Subtotal
      from revenuedtl r 
      where r.cmethod in (select distinct r.cmethod from revenuedtl r)
      group by r.ctripnumber, r.cmethod) r
 group by r.ctripnumber) tRev
inner join
(select p.ctripnumber,sum(Carrier_Subtotal) as "Carrier Total" 
 from 
     (select p.ctripnumber,p.cmethod,sum(p.curevenue) as Carrier_Subtotal 
      from payrolldtl p 
      where p.cmethod in (select distinct p.cmethod from payrolldtl p) 
      group by p.ctripnumber, p.cmethod) p
 group by p.ctripnumber) tCar on tRev.ctripnumber = tCar.ctripnumber