分组乘法,阅读障碍

时间:2012-06-08 11:14:20

标签: tsql

现在这是一个带有一个子查询的简单插入查询,它运行正常:

INSERT INTO CSolutions..Report2  

SELECT  Nov.Chain, CG.Grouping, reg.code as regcode,reg.pname,reg.fname,reg.sname, SUM(NS.Currency) as sumeur ,   sum(ns2.kpl) as sumkpl,0,0,0,0


FROM CSolutions..NovSales NS,CSolutions..NSaleskpl NS2, CSolutions..NovCGroup CG, sales..reg_eng reg,
        (SELECT DISTINCT P.Code, Nov.Chain
        FROM Register..Pcy P
            INNER JOIN Register..TCustomer TC ON (P.Code = TC.PCode)
            INNER JOIN CSolutions..Novies2 Nov ON (TC.Code = Nov.TCode)
        ) AS Nov

WHERE NS.SpCode = CG.SpCode and ns.spcode = ns2.spcode AND Ns.PCode = Nov.Code
and ns.spcode = reg.code
 and ns.pcode = ns2.pcode

  **and NS.SalesMonth = '201205'
   and  NS2.SalesMonth = '201205'**


GROUP BY Nov.Chain, CG.Grouping , reg.code,reg.pname,reg.fname,reg.sname

这是同一查询的派生,但它无法正常工作:

INSERT INTO CSolutions..Report2  

SELECT  Nov.Chain, CG.Grouping, reg.code as regcode,reg.pname,reg.fname,reg.sname, SUM(NS.Currency) as sumeur ,   sum(ns2.kpl) as sumkpl,0,0,0,0


FROM CSolutions..NovSales NS,CSolutions..NSaleskpl NS2, CSolutions..NovCGroup CG, sales..reg_eng reg,
        (SELECT DISTINCT P.Code, Nov.Chain
        FROM Register..Pcy P
            INNER JOIN Register..TCustomer TC ON (P.Code = TC.PCode)
            INNER JOIN CSolutions..Novies2 Nov ON (TC.Code = Nov.TCode)
        ) AS Nov

WHERE NS.SpCode = CG.SpCode and ns.spcode = ns2.spcode AND Ns.PCode = Nov.Code
and ns.spcode = reg.code
 and ns.pcode = ns2.pcode

  **and NS.SalesMonth between '201201' and '201205'
   and  NS2.SalesMonth between '201201' and '201205'**


GROUP BY Nov.Chain, CG.Grouping , reg.code,reg.pname,reg.fname,reg.sname

这将是5个月总和金额的5倍,

我是一名阅读障碍者,我很难发现错误,希望有人可以帮助我? :)

1 个答案:

答案 0 :(得分:0)

CSolutions..NovSales和CSolutions..NSaleskpl NS2中的salesmonth没有加入。在frist查询中,这并不重要,因为您只查看一个salesmonth。在第二个查询中,我假设有多个salesmonth,因为您从每个表中选择一个值范围。由于这个原因,你正在进行交叉加入。