db2中具有不同分组的子查询未正确返回除法结果

时间:2019-01-25 15:52:28

标签: sql db2 subquery

我绝对不知道我在做什么错。

我的数据很接近,但是我的V2和V3子查询未返回正确的数据,我认为这是基于以下事实:我试图按SUM进行分组划分,并且感觉好像使用了相同的strCount数全面。

查询:

git remote rm origin
git remote add https://...

这给了我

WITH T (employee, manager, GROUP, detail1, detail2, strCount, QUANT, LAST_SHIP_DATE) AS (
VALUES  (21177, 165, 4867, 2878, 29, 1, 1, CURRENT DATE - 10 DAY),
        (19445, 182, 4867, 2878, 29, 1, 1, CURRENT DATE - 10 DAY),
        (20949, 340, 4867, 2878, 29, 1, 2, CURRENT DATE - 10 DAY),
        (21347, 353, 4867, 2878, 29, 1, 1, CURRENT DATE - 10 DAY),
        (23068, 353, 4867, 2878, 29, 1, 1, CURRENT DATE - 10 DAY),
        (17195, 353, 4867, 2878, 29, 1, 6, CURRENT DATE - 10 DAY),
        (23040, 353, 4867, 2878, 29, 1, 1, CURRENT DATE - 10 DAY),
        (5620,  380, 4867, 2878, 29, 1, 4, CURRENT DATE - 10 DAY),
        (23009, 380, 4867, 2878, 29, 1, 3, CURRENT DATE - 10 DAY),
        (13529, 390, 4867, 2878, 29, 1, 1, CURRENT DATE - 10 DAY),
        (22706, 394, 4867, 2878, 29, 1, 1, CURRENT DATE - 10 DAY),
        (17049, 394, 4867, 2878, 29, 1, 4, CURRENT DATE - 10 DAY),
        (17879, 397, 4867, 2878, 29, 1, 1, CURRENT DATE - 10 DAY),
        (21320, 445, 4867, 2878, 29, 1, 1, CURRENT DATE - 10 DAY),
        (21231, 457, 4867, 2878, 29, 1, 1, CURRENT DATE - 10 DAY)
)
SELECT T.employee, T.manager, T.GROUP, T.detail1, T.detail2, T.strCount, T.QUANT
, COALESCE(T1.V1, 0) V1, COALESCE(T2.V2, 0) V2, COALESCE(T3.V3, 0) V3
FROM T
LEFT JOIN (
SELECT employee, GROUP, detail1, detail2, SUM(QUANT)/SUM(strCount)*12 V1
FROM T
WHERE last_ship_date > CURRENT_DATE - 30 DAYS
GROUP BY employee, GROUP, detail1, detail2,strCount
) T1 ON T1.employee=T.employee AND T1.GROUP=T.GROUP AND T1.detail1=T.detail1 AND T1.detail2=T.detail2 
LEFT JOIN (
SELECT manager, GROUP, detail1, detail2, SUM(QUANT)/SUM(strCount)*12 V2
FROM T
WHERE last_ship_date > CURRENT_DATE - 30 DAYS
GROUP BY manager, GROUP, detail1, detail2,strCount
) T2 ON T2.manager=T.manager AND T2.GROUP=T.GROUP AND T2.detail1=T.detail1 AND T2.detail2=T.detail2
LEFT JOIN (
SELECT GROUP, detail1, detail2, SUM(QUANT)/SUM(strCount)*12 V3
FROM T
WHERE last_ship_date > CURRENT_DATE - 30 DAYS
GROUP BY GROUP, detail1, detail2,strCount
) T3 ON T3.GROUP=T.GROUP AND T3.detail1=T.detail1 AND T3.detail2=T.detail2;

但期望:

employee | manager | group | detail1 | detail2 | strCount | quant | V1 | V2 | V3
================================================================================
21177    | 165     | 4867  | 2878    | 29      | 1        | 1     | 12 | 12 | 12
19445    | 182     | 4867  | 2878    | 29      | 1        | 1     | 12 | 12 | 12
20949    | 340     | 4867  | 2878    | 29      | 1        | 2     | 24 | 24 | 12
21347    | 353     | 4867  | 2878    | 29      | 1        | 1     | 12 | 24 | 12
23068    | 353     | 4867  | 2878    | 29      | 1        | 1     | 12 | 24 | 12
17195    | 353     | 4867  | 2878    | 29      | 1        | 6     | 72 | 24 | 12
23040    | 353     | 4867  | 2878    | 29      | 1        | 1     | 12 | 24 | 12
5620     | 380     | 4867  | 2878    | 29      | 1        | 4     | 48 | 36 | 12
23009    | 380     | 4867  | 2878    | 29      | 1        | 3     | 36 | 36 | 12
13529    | 390     | 4867  | 2878    | 29      | 1        | 1     | 12 | 12 | 12
22706    | 394     | 4867  | 2878    | 29      | 1        | 1     | 12 | 24 | 12
17049    | 394     | 4867  | 2878    | 29      | 1        | 4     | 48 | 24 | 12
17879    | 397     | 4867  | 2878    | 29      | 1        | 1     | 12 | 12 | 12
21320    | 445     | 4867  | 2878    | 29      | 1        | 1     | 12 | 12 | 12
21231    | 457     | 4867  | 2878    | 29      | 1        | 1     | 12 | 12 | 12

我该如何适当地进行更改,以使员工,经理和总数的分组反映出strCount的分组总和以及quant的分组总和?

更新:

这里的逻辑是

我想获得与排行信息相同的行数,但现在我的V1,V2和V3列应该是这样的:

V1:employee / group / detail1 / detail2分组的数量总和除以同一分组的strCount的总和(当前正确返回)

V2:manager / group / detail1 / detail2分组的数量总和除以相同分组的strCount的总和

V3:组/明细1 /明细2的总数之和除以相同分组的总strCount之和

1 个答案:

答案 0 :(得分:0)

INT/INT = INT在DB2中,即3/2 = 1,而不是1.5

使用decfloat(SUM(QUANT))/SUM(strCount)*12dec(SUM(QUANT), 28)/SUM(strCount)*12

并且您不需要按strCount进行分组,因为您在其上使用了聚合函数,除非由于某些应用程序逻辑确实需要将该列包括在分组中。