计算来自内部联接的所有记录

时间:2019-05-13 09:12:11

标签: sql sql-server

我有4个表,这些表具有aantalcount,每个表都显示正常数字或带有-的数字(例如-20),现在我需要计算所有记录。但我不知道该如何解决。

抱歉,我是sql的菜鸟。

这是我的代码

我要面对的问题是来自不同表的所有记录,这些记录的aantal列未总计。

示例: CSSDKMagento_10_Plankvoorraad返回10 CSSDKMagento_20_GeenAllocatieWelFiat返回-3和-2 CSSDKMagento_30_AllocatieVoorraad返回5 CSSDKMagento_50_AllocatieBestellingBinnen返回-1和-1

这意味着我从Voorraad得到的回报是8。

我尝试了count(*),但这不是解决方案。用最好的方法可以做到吗?

SELECT 
    i.ItemCode, 
    g.warehouse,
    SUM(g.aantal) AS Voorraad,
    MAX(CASE 
        WHEN g.transtype = 'N' THEN g.sysmodified 
        ELSE NULL 
    END) AS LastDate
FROM    dbo.CSSDKMagento_10_Plankvoorraad AS g 
    INNER JOIN  dbo.Items AS i
        ON (g.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_20_GeenAllocatieWelFiat AS a 
        ON (a.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_30_AllocatieVoorraad AS v 
        ON (v.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_50_AllocatieBestellingBinnen AS b 
        ON (b.artcode = i.ItemCode)
WHERE 
       i.itemcode = 'TEST' 
GROUP  BY i.itemcode, 
          g.warehouse;

3 个答案:

答案 0 :(得分:0)

编辑:

SELECT SUM(Voorraad) FROM (
SELECT 
    i.ItemCode, 
    g.warehouse,
    g.aantal AS Voorraad,
    MAX(CASE 
        WHEN g.transtype = 'N' THEN g.sysmodified 
        ELSE NULL 
    END) AS LastDate
FROM    dbo.CSSDKMagento_10_Plankvoorraad AS g 
    INNER JOIN  dbo.Items AS i
        ON (g.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_20_GeenAllocatieWelFiat AS a 
        ON (a.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_30_AllocatieVoorraad AS v 
        ON (v.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_50_AllocatieBestellingBinnen AS b 
        ON (b.artcode = i.ItemCode)
WHERE 
       i.itemcode = 'TEST' enter code here
GROUP  BY i.itemcode, 
          g.warehouse
)src 

答案 1 :(得分:0)

尝试一下

SELECT 
    i.ItemCode, 
    g.warehouse,
    SUM(g.aantal)+SUM(a.aantal)+SUM(v.aantal)+SUM(b.aantal) AS Voorraad,
    MAX(CASE 
        WHEN g.transtype = 'N' THEN g.sysmodified 
        ELSE NULL 
    END) AS LastDate
FROM    dbo.CSSDKMagento_10_Plankvoorraad AS g 
    INNER JOIN  dbo.Items AS i
        ON (g.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_20_GeenAllocatieWelFiat AS a 
        ON (a.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_30_AllocatieVoorraad AS v 
        ON (v.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_50_AllocatieBestellingBinnen AS b 
        ON (b.artcode = i.ItemCode)
WHERE 
       i.itemcode = 'TEST' 
GROUP  BY i.itemcode, 
          g.warehouse;

答案 2 :(得分:0)

看起来像这样可以工作:

  WITH itemCodesScope AS
  (
    SELECT 'TEST' as target_ItemCode
  ),
  aggregated_CSSDKMagento_10_Plankvoorraad AS 
  (
    SELECT g.artcode, 
           g.warehouse,
           COUNT(g.aantal) as count_aantal,
           SUM(g.aantal) as sum_aantal,
           MAX(CASE 
              WHEN g.transtype = 'N' THEN g.sysmodified 
              ELSE NULL 
           END) AS LastDate
    FROM dbo.CSSDKMagento_10_Plankvoorraad AS g
        JOIN itemCodesScope ON itemCodesScope.target_itemCode = g.artcode
    GROUP BY
           g.artcode, 
           g.warehouse
  ),
  aggregated_CSSDKMagento_20_GeenAllocatieWelFiat AS 
  (
    SELECT a.artcode ,
           COUNT(a.aantal) as count_aantal,
           SUM(a.aantal) as sum_aantal
    FROM dbo.CSSDKMagento_20_GeenAllocatieWelFiat AS a
        JOIN itemCodesScope ON itemCodesScope.target_itemCode = a.artcode
    GROUP BY
           a.artcode
  ),
  aggregated_CSSDKMagento_30_AllocatieVoorraad AS 
  (
    SELECT v.artcode ,
           COUNT(v.aantal) as count_aantal,
           SUM(v.aantal) as sum_aantal
    FROM dbo.CSSDKMagento_30_AllocatieVoorraad AS v
        JOIN itemCodesScope ON itemCodesScope.target_itemCode = v.artcode
    GROUP BY
           v.artcode
  ),
  aggregated_CSSDKMagento_50_AllocatieBestellingBinnen AS 
  (
    SELECT b.artcode ,
           COUNT(b.aantal) as count_aantal,
           SUM(b.aantal) as sum_aantal
    FROM dbo.CSSDKMagento_50_AllocatieBestellingBinnen AS b
        JOIN itemCodesScope ON itemCodesScope.target_itemCode = b.artcode
    GROUP BY
           b.artcode
  )
  SELECT 
    g.artcode as ItemCode, 
    g.warehouse,
    g.sum_aantal AS Voorraad,
    g.LastDate AS LastDate,
    g.sum_aantal + ISNULL(a.sum_aantal, 0) + ISNULL(v.sum_aantal, 0) + ISNULL(b.sum_aantal, 0) as sum_aantal,
    g.count_aantal + ISNULL(a.count_aantal, 0) + ISNULL(v.count_aantal, 0) + ISNULL(b.count_aantal, 0) as count_aantal
  FROM    aggregated_CSSDKMagento_10_Plankvoorraad AS g 
    INNER JOIN  dbo.Items AS i
        ON (g.artcode = i.ItemCode)
    INNER JOIN itemCodesScope 
        ON itemCodesScope.target_itemCode = i.ItemCode
    LEFT JOIN aggregated_CSSDKMagento_20_GeenAllocatieWelFiat AS a 
        ON (a.artcode = i.ItemCode)
    LEFT JOIN aggregated_CSSDKMagento_30_AllocatieVoorraad AS v 
        ON (v.artcode = i.ItemCode)
    LEFT JOIN aggregated_CSSDKMagento_50_AllocatieBestellingBinnen AS b 
        ON (b.artcode = i.ItemCode)

说明

SQL-joins产生Cartesian Products,这很可能导致初始查询中出现意外结果。这里有4个“数量”表,它们通过具有条件为“ ON(b.artcode = i.ItemCode)”的Joins连接,因此,如果任何表的每个条件输出包含多个记录,则每个条件代码将包含多个记录。

比方说,一个表中有9条记录,每个i.ItemCode都有a.artcode,因此存在一对多关系。假设每个i.ItemCode在b表中有1条记录。 Join的输出将有9个a.aantal记录,但还有9个重复的b.aantal记录。假定存在一个聚合(分组依据),那么它将影响此Joins查询中的SUM(b.aantal)仅比对b表的独立查询中的sum(b.aantal)多产生9倍。

如果在不进行汇总的情况下运行初始查询,则可以更容易地看到笛卡尔积:

SELECT *
FROM    dbo.CSSDKMagento_10_Plankvoorraad AS g 
    INNER JOIN  dbo.Items AS i
        ON (g.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_20_GeenAllocatieWelFiat AS a 
        ON (a.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_30_AllocatieVoorraad AS v 
        ON (v.artcode = i.ItemCode)
    INNER JOIN dbo.CSSDKMagento_50_AllocatieBestellingBinnen AS b 
        ON (b.artcode = i.ItemCode)
WHERE 
       i.itemcode = 'TEST'

固定装置是:在加入联接之前按汇总进行分组。此恕我直言最方便的方法是CTE。使用CTE,我创建了4个临时表,每个ItemCode都有汇总,所以每个临时表是一对一的。然后,一对一的Joins每个ItemCode仅产生单个输出行。