Mam stół w postgach, takich jak poniżej

table

Chcę SQL w postgresach, które liczą kombinację 2 kolumn, które ma YY

Spodziewa się wyjścia jak

Liczba kombinacji

AB 2
AC 1
AD 2
AZ 1
BC 1
BD 3
BZ 2
CD 2
CZ 0
DZ 1

Czy ktoś może mi pomóc?

1
Mehman Q 20 luty 2019, 11:26

2 odpowiedzi

Najlepsza odpowiedź
WITH stacked AS (
    SELECT id
        , unnest(array['A', 'B', 'C', 'D', 'Z']) AS col_name
        , unnest(array[a, b, c, d, z]) AS col_value
    FROM test t
)
SELECT combo, sum(cnt) AS count
FROM (
    SELECT t1.id, t1.col_name || t2.col_name AS combo
        , (CASE WHEN t1.col_value = 'Y' AND t2.col_value = 'Y' THEN 1 ELSE 0 END) AS cnt
    FROM stacked t1
    INNER JOIN stacked t2
    ON t1.id = t2.id
    AND t1.col_name < t2.col_name) t3
GROUP BY combo
ORDER BY combo

Plony

| combo | count |
|-------+-------|
| AB    |     2 |
| AC    |     1 |
| AD    |     2 |
| AZ    |     2 |
| BC    |     1 |
| BD    |     3 |
| BZ    |     2 |
| CD    |     2 |
| CZ    |     0 |
| DZ    |     1 |

Receptura {{X0}


Aby policzyć wystąpienia YYY wśród 3 kolumn, których możesz użyć:

WITH stacked AS (
    SELECT id
        , unnest(array['A', 'B', 'C', 'D', 'Z']) AS col_name
        , unnest(array[a, b, c, d, z]) AS col_value
    FROM test t
)
SELECT combo, sum(cnt) AS count
FROM (
    SELECT t1.id, t1.col_name || t2.col_name || t3.col_name AS combo
        , (CASE WHEN t1.col_value = 'Y' 
               AND t2.col_value = 'Y'
               AND t3.col_value = 'Y' THEN 1 ELSE 0 END) AS cnt
    FROM stacked t1
    INNER JOIN stacked t2
    ON t1.id = t2.id
    INNER JOIN stacked t3
    ON t1.id = t3.id
    AND t1.col_name < t2.col_name 
    And t2.col_name < t3.col_name
    ) t3
GROUP BY combo
ORDER BY combo
;

Która daje

| combo | count |
|-------+-------|
| ABC   |     0 |
| ABD   |     1 |
| ABZ   |     2 |
| ACD   |     1 |
| ACZ   |     0 |
| ADZ   |     1 |
| BCD   |     1 |
| BCZ   |     0 |
| BDZ   |     1 |
| CDZ   |     0 |

Lub do obsługi kombinacji kolumnów N, można użyć WITH RECURSIVE: Na przykład dla N = 3,

WITH RECURSIVE result AS (
    WITH stacked AS (
        SELECT id
            , unnest(array['A', 'B', 'C', 'D', 'Z']) AS col_name
            , unnest(array[a, b, c, d, z]) AS col_value
        FROM test t)
    SELECT id, array[col_name] AS path, array[col_value] AS path_val, col_name AS last_name
    FROM stacked

    UNION

    SELECT r.id, path || s.col_name, path_val || s.col_value, s.col_name
    FROM result r
    INNER JOIN stacked s
    ON r.id = s.id
        AND s.col_name > r.last_name
    WHERE array_length(r.path, 1) < 3)  -- Change 3 to your value for N
SELECT combo, sum(cnt)
FROM (
    SELECT id, array_to_string(path, '') AS combo, (CASE WHEN 'Y' = all(path_val) THEN 1 ELSE 0 END) AS cnt
    FROM result
    WHERE array_length(path, 1) = 3) t  -- Change 3 to your value for N
GROUP BY combo
ORDER BY combo

Należy pamiętać, że N = 3 jest używany w 2 miejscach w powyższym SQL.

2
unutbu 20 luty 2019, 17:47

Zrobiłbym to przy użyciu bocznego dołączenia:

with vals as (
      select v.*
      from t cross join lateral
           (values ('A', A), ('B', B), ('C', C), ('D', D), ('Z', Z)
           ) v(which, val)
     )
select (v1.which || v2.which) as combo,
       sum( (val = 'Y')::int ) as count
from vals v1 join
     vals v2
     on v1.which < v2.which
group by combo
order by combo;

Uważam, że połączenia boczne są bardziej bezpośredni sposób na uniknięcie wartości. Nie ma potrzeby konwertowania wartości do tablicy nieznażonej, znacznie mniej najróżniejszych dwóch macierzy i wyrównać wartości.

0
Gordon Linoff 20 luty 2019, 12:35