0

我有查询

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

但是列 nLTDAmt,nLTDUnt,nYTDAmt,nYTDUnt 的结果不是我们想要的。它仅针对 1 条记录进行汇总。

我试着做这个查询

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 

但是我得到错误只有一个表达式可以在选择列表中指定当子查询没有与 EXISTS 一起引入时。

如何只查询所需的结果?

期望的结果是


    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

4

1 回答 1

0

The syntax of your last query doesn't work, you can't introduce multiple columns via subquery in the SELECT list like that, so I'd suggest sticking with fixing your JOIN query.

Since you need the SUM() across multiple records without grouping, you'll need to window the sum via 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 
于 2013-09-23T17:41:03.863 回答