SQL为具有类别总计

时间:2016-06-30 05:30:57

标签: sql oracle sum distinct cube

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

我可以使用类似How do I SUM DISTINCT Rows?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"

进入这对表:

    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计算在两个不同的位置。只有三个不同的人参与这一行。与其他总计类似。

请注意,“猫,象棋”的加权总数为20 = 10 + 10,因为两个不同的人各自为此单元格贡献了10个重量。

请注意,加权表的总计为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);

“猫,象棋”单元格来到10而不是20,因为人们1和2都具有相同的权重。删除“不同”关键字意味着单个细胞计数是正确的,但总数是错误的(它产生总数为52,其中应该是32,因为人1和2在总数中被重复计算)。

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

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

getActionbar().hide();
getActionbar().show();

仍然在单一选择

答案 1 :(得分:1)

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

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
;

答案 2 :(得分:0)

我认为你需要做这样的数学运算:

;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 Server中测试它;)。

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

;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;