如何使用汇总结果连接两个表

时间:2013-09-23 17:04:30

标签: sql sql-server

我有查询

SELECT ISNULL(sum(s.slh_iltd_units),0) as 'nLTDUnt' , 
ISNULL(SUM(s.slh_iltd_amt),0) as 'nLTDAmt'  ,
ISNULL(sum(s.slh_iytd_units),0) as 'nYTDUnt',   
ISNULL(sum(s.slh_iytd_amt),0) as 'nYTDAmt' 
from title t LEFT join salehist s on t.ttl_cisbn13 = s.slh_cisbn  
where TTL_CSTATUS <> 'D' AND 
ttl_ctitleid+ttl_ceditionno= (select ttl_ctitleid+ttl_ceditionno from title where ttl_cisbn13 =  '9780203005309')

结果是:

nLTDUnt nLTDAmt nYTDUnt nYTDAmt
3379    108771.00   0   0.00

我有第二个查询

Select sls.sal_ipk,sls.sal_cisbn,sls.sal_cruletype,sls.sal_country,
      sls.sal_nqty,sls.sal_namtusd,sls.sal_nlistprusd
from sales sls left join ruletype r on sls.sal_cruletype = r.rut_crultype
 where sls.sal_acctmonth=6 and sls.sal_acctyear=2012 and
       sls.sal_ernipk IS NULL and sls.sal_lclientle=0 and 
       r.rut_lignore <> 1 AND sal_corigen<>'UK' and sls.sal_cisbn='9780203005309'
  order by sls.sal_cisbn 

结果是:

sal_ipk sal_cisbn   sal_cruletype   sal_country sal_nqty    sal_namtusd sal_nlistprusd
1202    9780203005309   1   US  3   112.38  57.95
1203    9780203005309   1   US  -2  -81.14  59.95

我创建了一个加入两个查询的查询

Select  sls.sal_ipk,sls.sal_cisbn,sls.sal_cruletype,
        sls.sal_country,sls.sal_nqty,sls.sal_namtusd,sls.sal_nlistprusd,
       sal1.nLTDAmt ,sal1.nLTDUnt ,sal1.nYTDAmt , sal1.nYTDUnt, sal1.ttl_cisbn13 
from sales sls left join ruletype r on sls.sal_cruletype = r.rut_crultype
        INNER JOIN (
           SELECT  t.ttl_cisbn13 ,  ISNULL(sum(s.slh_iltd_units),0) as 'nLTDUnt' ,   
 ISNULL(SUM (s.slh_iltd_amt),0) as 'nLTDAmt'  ,
 ISNULL(sum(s.slh_iytd_units),0) as 'nYTDUnt',    
 ISNULL(sum(s.slh_iytd_amt),0) as 'nYTDAmt' 
                 from title t left join salehist s on t.ttl_cisbn13 = s.slh_cisbn  
                     where TTL_CSTATUS <> 'D' AND 
                         ttl_ctitleid+ttl_ceditionno= (select ttl_ctitleid+ttl_ceditionno from title where ttl_cisbn13 = s.slh_cisbn     )
                         group by t.ttl_cisbn13 
                     ) AS sal1 on  ttl_cisbn13 = sls.sal_cisbn
  where sls.sal_acctmonth=6 and sls.sal_acctyear=2012 and
       sls.sal_ernipk IS NULL and sls.sal_lclientle=0 and 
       r.rut_lignore <> 1 AND sal_corigen<>'UK' and sls.sal_cisbn='9780203005309'
  order by sls.sal_cisbn 

 sal_ipk    sal_cisbn   sal_cruletype   sal_country sal_nqty    sal_namtusd sal_nlistprusd  nLTDAmt nLTDUnt nYTDAmt nYTDUnt
 1202   9780203005309   1   US  3   112.38  57.95   4310.00 110 0.00    0
1203    9780203005309   1   US  -2  -81.14  59.95   4310.00 110 0.00    0

Select sls.sal_ipk,sls.sal_cisbn,sls.sal_cruletype, sls.sal_country,sls.sal_nqty,sls.sal_namtusd,sls.sal_nlistprusd, sal1.nLTDAmt ,sal1.nLTDUnt ,sal1.nYTDAmt , sal1.nYTDUnt, sal1.ttl_cisbn13 from sales sls left join ruletype r on sls.sal_cruletype = r.rut_crultype INNER JOIN ( SELECT t.ttl_cisbn13 , ISNULL(sum(s.slh_iltd_units),0) as 'nLTDUnt' , ISNULL(SUM (s.slh_iltd_amt),0) as 'nLTDAmt' , ISNULL(sum(s.slh_iytd_units),0) as 'nYTDUnt', ISNULL(sum(s.slh_iytd_amt),0) as 'nYTDAmt' from title t left join salehist s on t.ttl_cisbn13 = s.slh_cisbn where TTL_CSTATUS <> 'D' AND ttl_ctitleid+ttl_ceditionno= (select ttl_ctitleid+ttl_ceditionno from title where ttl_cisbn13 = s.slh_cisbn ) group by t.ttl_cisbn13 ) AS sal1 on ttl_cisbn13 = sls.sal_cisbn where sls.sal_acctmonth=6 and sls.sal_acctyear=2012 and sls.sal_ernipk IS NULL and sls.sal_lclientle=0 and r.rut_lignore <> 1 AND sal_corigen<>'UK' and sls.sal_cisbn='9780203005309' order by sls.sal_cisbn sal_ipk sal_cisbn sal_cruletype sal_country sal_nqty sal_namtusd sal_nlistprusd nLTDAmt nLTDUnt nYTDAmt nYTDUnt 1202 9780203005309 1 US 3 112.38 57.95 4310.00 110 0.00 0 1203 9780203005309 1 US -2 -81.14 59.95 4310.00 110 0.00 0

但是colum nLTDAmt,nLTDUnt,nYTDAmt,nYTDUnt的结果并不理想。它只对1条记录进行了总结。

我试图做这个查询

但是我得到了错误当子查询未引入EXISTS时,只能在选择列表中指定一个表达式。

如何使用所需的结果进行查询?

所需的结果是

Select sls.sal_ipk, sls.sal_cisbn, sls.sal_cruletype, sls.sal_country, sls.sal_nqty, sls.sal_namtusd, sls.sal_nlistprusd,
          (
           SELECT    ISNULL(sum(s.slh_iltd_units),0) as 'nLTDUnt' ,    ISNULL(SUM(s.slh_iltd_amt),0) as 'nLTDAmt'  ,
                       ISNULL(sum(s.slh_iytd_units),0) as 'nYTDUnt',      ISNULL(sum(s.slh_iytd_amt),0) as 'nYTDAmt' 
                 from title t left join salehist s on t.ttl_cisbn13 = s.slh_cisbn  
                     where TTL_CSTATUS <> 'D' AND 
                         ttl_ctitleid+ttl_ceditionno= (select ttl_ctitleid+ttl_ceditionno from title where ttl_cisbn13 = sls.sal_cisbn      )
                     ) AS sal1
        from sales sls left join ruletype r on sls.sal_cruletype = r.rut_crultype                   
  where sls.sal_acctmonth=6 and sls.sal_acctyear=2012 and
       sls.sal_ernipk IS NULL and sls.sal_lclientle=0 and 
       r.rut_lignore <> 1 AND sal_corigen<>'UK' and sls.sal_cisbn='9780203005309'
  order by sls.sal_cisbn 

我找到了问题的解决方案


    sal_ipk sal_cisbn   sal_cruletype   sal_country sal_nqty    sal_namtusd sal_nlistprusd  nLTDAmt nLTDUnt nYTDAmt nYTDUnt ttl_cisbn13
    1202    9780203005309   1   US  3   112.38  57.95   108771.00   3379    0.00    0   9780203005309
    1203    9780203005309   1   US  -2  -81.14  59.95   108771.00   3379    0.00    0   9780203005309

Select sls.sal_ipk,sls.sal_cisbn,sls.sal_cruletype, sls.sal_country,sls.sal_nqty,sls.sal_namtusd,sls.sal_nlistprusd, sal1.nLTDAmt ,sal1.nLTDUnt ,sal1.nYTDAmt , sal1.nYTDUnt from sales sls left join ruletype r on sls.sal_cruletype = r.rut_crultype left join title tt on tt.ttl_cisbn13=sls.sal_cisbn INNER JOIN ( SELECT ttl_ctitleid,ttl_ceditionno, ISNULL(sum(s.slh_iltd_units),0) as 'nLTDUnt' , ISNULL(SUM(s.slh_iltd_amt),0) as 'nLTDAmt' , ISNULL(sum(s.slh_iytd_units),0) as 'nYTDUnt', ISNULL(sum(s.slh_iytd_amt),0) as 'nYTDAmt' from title t left join salehist s on t.ttl_cisbn13 = s.slh_cisbn where t.TTL_CSTATUS <> 'D' group by ttl_ctitleid,ttl_ceditionno ) AS sal1 on sal1.ttl_ctitleid= tt.ttl_ctitleid and sal1.ttl_ceditionno= tt.ttl_ceditionno where sls.sal_acctmonth=6 and sls.sal_acctyear=2012 and sls.sal_ernipk IS NULL and sls.sal_lclientle=0 and r.rut_lignore <> 1 AND sal_corigen<>'UK' order by sls.sal_cisbn

Select sls.sal_ipk,sls.sal_cisbn,sls.sal_cruletype, sls.sal_country,sls.sal_nqty,sls.sal_namtusd,sls.sal_nlistprusd, sal1.nLTDAmt ,sal1.nLTDUnt ,sal1.nYTDAmt , sal1.nYTDUnt from sales sls left join ruletype r on sls.sal_cruletype = r.rut_crultype left join title tt on tt.ttl_cisbn13=sls.sal_cisbn INNER JOIN ( SELECT ttl_ctitleid,ttl_ceditionno, ISNULL(sum(s.slh_iltd_units),0) as 'nLTDUnt' , ISNULL(SUM(s.slh_iltd_amt),0) as 'nLTDAmt' , ISNULL(sum(s.slh_iytd_units),0) as 'nYTDUnt', ISNULL(sum(s.slh_iytd_amt),0) as 'nYTDAmt' from title t left join salehist s on t.ttl_cisbn13 = s.slh_cisbn where t.TTL_CSTATUS <> 'D' group by ttl_ctitleid,ttl_ceditionno ) AS sal1 on sal1.ttl_ctitleid= tt.ttl_ctitleid and sal1.ttl_ceditionno= tt.ttl_ceditionno where sls.sal_acctmonth=6 and sls.sal_acctyear=2012 and sls.sal_ernipk IS NULL and sls.sal_lclientle=0 and r.rut_lignore <> 1 AND sal_corigen<>'UK' order by sls.sal_cisbn

1 个答案:

答案 0 :(得分:0)

您上次查询的语法不起作用,您不能通过SELECT列表中的子查询引入多个列,因此我建议您坚持修复JOIN查询。

由于您需要跨多个记录SUM()而不进行分组,因此您需要通过OVER()来显示总和:

SELECT  sls.sal_ipk
      , sls.sal_cisbn
      , sls.sal_cruletype
      , sls.sal_country
      , sls.sal_nqty
      , sls.sal_namtusd
      , sls.sal_nlistprusd
      , sal1.nLTDAmt
      , sal1.nLTDUnt
      , sal1.nYTDAmt
      , sal1.nYTDUnt
      , sal1.ttl_cisbn13
FROM    sales sls
INNER JOIN (SELECT    t.ttl_cisbn13
                    , ISNULL(SUM(s.slh_iltd_units) OVER(), 0) AS 'nLTDUnt'
                    , ISNULL(SUM(s.slh_iltd_amt) OVER(), 0) AS 'nLTDAmt'
                    , ISNULL(SUM(s.slh_iytd_units) OVER(), 0) AS 'nYTDUnt'
                    , ISNULL(SUM(s.slh_iytd_amt) OVER(), 0) AS 'nYTDAmt'
            FROM    title t
            LEFT JOIN salehist s 
               ON t.ttl_cisbn13 = s.slh_cisbn
            WHERE   TTL_CSTATUS <> 'D'
              AND ttl_ctitleid+ttl_ceditionno= (SELECT ttl_ctitleid+ttl_ceditionno 
                                                FROM title 
                                                WHERE ttl_cisbn13 =  sls.sal_cisbn)
            ) AS sal1 
  ON sal1.ttl_cisbn13 = sls.sal_cisbn
LEFT JOIN ruletype r 
   ON sls.sal_cruletype = r.rut_crultype
WHERE   sls.sal_acctmonth = 6
        AND sls.sal_acctyear = 2012
        AND sls.sal_ernipk IS NULL
        AND sls.sal_lclientle = 0
        AND r.rut_lignore <> 1
        AND sal_corigen <> 'UK'
        AND sls.sal_cisbn = '9780203005309'
ORDER BY sls.sal_cisbn 
相关问题