-2

现在这是一个带有一个子查询的简单插入查询,并且运行良好:

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 倍,

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

4

1 回答 1

0

CSolutions..NovSales 和 CSolutions..NSaleskpl NS2 中的 salesmonth 没有加入。在第一个查询中,这无关紧要,因为您只查看一个销售月。在第二个查询中,我假设当您从每个表中选择一个值范围时有多个销售月。由于 salesmonth 的原因,您因此获得了交叉加入。

于 2012-06-08T11:51:46.690 回答