SQL 对具有类别总计的不同行的非不同权重求和

SQL sum non-distinct weights for distinct rows with category totals

我想在个人可以为多个单元格做出贡献的情况下对一些加权调查数据进行交叉制表。挑战在于确保在不重复计算的情况下完成小计和总计。

我可以使用类似于 How do I SUM DISTINCT Rows? or Sum Distinct By Other Column 中的解决方案的方法获取单个单元格值,但不能获取总计值。我正在尝试使用 Oracle CUBE 语句以一种很好的方式获取总数。

这是一个小例子。假设我们根据他们拥有的宠物和他们的爱好来统计人数。问题是一个人可能有不止一只宠物,或者不止一种爱好。我们需要翻出这组单位记录:

person_id, weight
1, 10
2, 10
3, 12

person_id, pet
1, "cat"
1, "dog"
2, "cat"
3, "cat"

person_id, hobby
1, "chess"
2, "chess"
2, "skydiving"
3, "skydiving"

进入这对table:

    Unweighted count

      | chess | skydiving | total
------+-------+-----------+--------
cat   |  2    |  2        | 3
------+-------+-----------+--------
dog   |  1    |  0        | 1
------+-------+-----------+--------
total |  2    |  2        | 3      


Weighted count

      | chess | skydiving | total
------+-------+-----------+--------
cat   |  20   |  22       | 32
------+-------+-----------+--------
dog   |  10   |  0        | 10
------+-------+-----------+--------
total |  20   |  22       | 32     

请注意,"cat" 行的未加权总数是 3,而不是 2+2=4,因为第 2 个人是在两个不同的地方计算的。只有三个不同的人对这一行做出贡献。其他总数也是如此。

请注意 "cat, chess" 的加权总和为 20=10+10,因为两个不同的人分别为该单元格贡献权重 10。

请注意,加权 table 的总计是 32。这来自第 1 个人和第 2 个人各贡献了 10 个,第 3 个人贡献了 12 个。总和不仅仅是所有个人的总和细胞!

对于未加权的计数,我可以通过以下方式获得所有单元格计数和总数:

CREATE TABLE weights(person_id INTEGER, weight INTEGER);
INSERT INTO weights(person_id,weight) VALUES (1,10);
INSERT INTO weights(person_id,weight) VALUES (2,10);
INSERT INTO weights(person_id,weight) VALUES (3,12);

CREATE TABLE pets(person_id INTEGER, pet VARCHAR(3));
INSERT INTO pets(person_id,pet) VALUES (1,'cat');
INSERT INTO pets(person_id,pet) VALUES (1,'dog');
INSERT INTO pets(person_id,pet) VALUES (2,'cat');
INSERT INTO pets(person_id,pet) VALUES (3,'cat');

CREATE TABLE hobbies(person_id INTEGER, hobby VARCHAR(9));
INSERT INTO hobbies(person_id,hobby) VALUES (1,'chess');
INSERT INTO hobbies(person_id,hobby) VALUES (2,'chess');
INSERT INTO hobbies(person_id,hobby) VALUES (2,'skydiving');
INSERT INTO hobbies(person_id,hobby) VALUES (3,'skydiving');

SELECT pet, hobby, COUNT(DISTINCT weights.person_id)
FROM weights JOIN pets on weights.person_id=pets.person_ID
JOIN hobbies on weights.person_id=hobbies.person_id
GROUP BY CUBE(pet, hobby);

COUNT(DISTINCT ...)CUBE 的组合给出了正确的总数。

对于加权计数,如果我尝试相同的想法:

SELECT pet, hobby, SUM(DISTINCT weight)
FROM weights JOIN pets on weights.person_id=pets.person_ID
JOIN hobbies on weights.person_id=hobbies.person_id
GROUP BY CUBE(pet, hobby);

"cat, chess" 单元格的结果是 10 而不是 20,因为人 1 和 2 的体重相同。删除 "distinct" 关键字意味着单个单元格计数是正确的,但总数是错误的(它产生总计 52 而应该是 32,因为人 1 和 2 在总数中被重复计算)。

有什么建议吗?

试试这个,下面给出了正确的结果,但这是最简单的结果

SELECT pet, hobby, SUM(weight)
FROM weights JOIN pets on weights.person_id=pets.person_ID
JOIN hobbies on weights.person_id=hobbies.person_id
GROUP BY pet, hobby
UNION
SELECT pet, NULL, SUM(weight)
FROM weights JOIN pets on weights.person_id=pets.person_ID
GROUP BY pet
UNION
SELECT NULL, hobby, SUM(weight)
FROM weights JOIN hobbies on weights.person_id=hobbies.person_id
GROUP BY hobby
UNION
SELECT SUM(weight)
FROM weights

仍在单身select

您可以使用嵌套查询来执行此操作,其中内部查询指定从行到 table 单元格的映射(即哪些记录在每个 table 单元格的范围内),以及外部查询查询指定要应用的汇总函数:

SELECT pet, hobby, COUNT(1), SUM(weight) FROM
(SELECT pet, hobby, weights.person_ID, weight
FROM weights JOIN pets on weights.person_id=pets.person_ID
JOIN hobbies on weights.person_id=hobbies.person_id
GROUP BY CUBE(pet, hobby), weights.person_ID, weight)
GROUP BY pet, hobby;

Results

旁白:您也可以在不使用 CUBE 运算符的情况下编写内部查询,但它会更加混乱:

WITH
    pet_cube_map as (SELECT DISTINCT pet, NULL as pet_cubed FROM pets UNION ALL SELECT DISTINCT pet, pet as pet_cubed FROM pets),
    hobby_cube_map as (SELECT DISTINCT hobby, NULL as hobby_cubed FROM hobbies UNION ALL SELECT DISTINCT hobby, hobby as hobby_cubed FROM hobbies)
SELECT DISTINCT pet_cubed as pet, hobby_cubed as hobby, weights.person_ID, weight
FROM weights
    JOIN pets on weights.person_ID=pets.person_ID
    JOIN pet_cube_map on pets.pet=pet_cube_map.pet
    JOIN hobbies on weights.person_ID=hobbies.person_ID
    JOIN hobby_cube_map on hobbies.hobby=hobby_cube_map.hobby
;

我认为你需要像这样做一些数学运算:

;WITH t AS (
    SELECT 
        p.pet,
        SUM(DISTINCT CASE WHEN h.hobby = 'chess' THEN POWER(2,h.person_id) ELSE 0 END) chess,
        SUM(DISTINCT CASE WHEN h.hobby = 'skydiving' THEN POWER(2,h.person_id) ELSE 0 END) skydiving,
        SUM(DISTINCT POWER(2,h.person_id)) total
    FROM 
        hobbies h
        LEFT JOIN
        pets p ON h.person_id = p.person_id
    GROUP BY
        p.pet
    UNION ALL 
    SELECT 
        'total',
        SUM(DISTINCT CASE WHEN h.hobby = 'chess' THEN POWER(2,h.person_id) ELSE 0 END),
        SUM(DISTINCT CASE WHEN h.hobby = 'skydiving' THEN POWER(2,h.person_id) ELSE 0 END),
        SUM(DISTINCT POWER(2,h.person_id))
    FROM 
        hobbies h
), w(person_id, weight) as (
    SELECT POWER(2,person_id), weight
    FROM weights
), cte(person_id, weight) AS (
    SELECT *
    FROM w
    UNION ALL
    SELECT w1.person_id + w2.person_id, w1.weight + w2.weight
    FROM cte w1 JOIN w w2 ON w2.person_id > w1.person_id
)
SELECT 
    pet,
    COALESCE((SELECT cte.weight FROM cte WHERE cte.person_id = t.chess), 0) AS chess,
    COALESCE((SELECT cte.weight FROM cte WHERE cte.person_id = t.skydiving), 0) AS skydiving,
    COALESCE((SELECT cte.weight FROM cte WHERE cte.person_id = t.total), 0) AS total
FROM t;

不是立方体,静态方式,有点脏。但我只是在 SQL 服务器上测试它 ;).


这可以是立方版本(未测试):

;With t as (
SELECT  h.hobby, p.pet, POWER(2,h.person_id) weight
FROM    hobbies h 
JOIN    pets p
ON      h.person_id = p.person_id
JOIN    weights w
ON      h.person_id = w.person_id
), w(person_id, weight) as (
    SELECT POWER(2,person_id), weight
    FROM weights
), cte(person_id, weight) AS (
    SELECT *
    FROM w
    UNION ALL
    SELECT w1.person_id + w2.person_id, w1.weight + w2.weight
    FROM cte w1 JOIN w w2 ON w2.person_id > w1.person_id
), c as (
SELECT  
    hobby, pet, SUM(DISTINCT weight) person_id
FROM    t
GROUP BY CUBE(hobby, pet)
)
SELECT c.hobby, c.pet, cte.weight
FROM c JOIN
    cte ON c.person_id = cte.person_id;