Ich habe diese Daten
ID Month PRODUCT VALUE_1 VALUE_2
1234 1 a 34 12
1233 2 B 54
1245 3 c 23 42
1236 4 d 12 8
1238 1 a 56 5
1239 2 B 42 1
1234 3 c 32 6
1233 4 d 3
1245 1 a 8 6
1236 2 B 5 2
1238 3 c 1 6
1239 4 d 2
1234 1 a 15 4
1233 2 c 8 12
1245 3 d 15 6
1236 4 b 1 1
1238 1 c 14 10
1239 2 d 13 6
1234 3 c 13 17
1233 4 b 15 5
1245 1 c 18 11
1236 2 d 12 15
1238 3 c 8 12
1239 4 a 17 4
und versuche das zu erreichen:
SUM a b c d
ID a_1 a_2 a_3 a_4 b_1 b_2 b_3 b_4 c_1 c_2 c_3 c_4 d_1 d_2 d_3 d_4
1233 0 0 0 0 0 54 0 15 0 8 0 0 0 0 0 0
1234 49 0 0 0 0 0 0 0 0 0 45 0 0 0 0 0
1236 0 0 0 0 0 5 0 1 0 0 0 0 0 12 0 12
1238 56 0 0 0 0 0 0 0 14 0 9 0 0 0 0 0
1239 0 0 0 17 0 42 0 0 0 0 0 0 0 13 0 2
1245 8 0 0 0 0 0 0 0 18 0 23 0 0 0 15 0
Grundsätzlich möchte ich jeden Produktwert für jeden Monat aggregiert sehen;
In Python wäre das Äquivalent zu nur einer Codezeile.
data.pivot_table(index=['ID'], columns=["Product","Month"],
values=["Value_1","Value_2"],
aggfunc=np.sum,
dropna = False, fill_value=0)
Ich habe das probiert:
WITH pivot_table as (SELECT * FROM
(
SELECT ID, PRODUCT,Value_1,Value_2, Month
FROM DATASET
) New_dataset
PIVOT (
--#2 aggregation
SUM(Value_1) as value1 , SUM(Value_2) as value2
--#3 Pivot_column
FOR PRODUCT IN ('a','b','c','d')
))
#as pivot_table
SELECT ID ,Month, SUM(value1_a) as a_1, SUM(value2_a) as a_2 FROM pivot_table
GROUP BY ID, Month
ORDER BY value_1
Ich bin so weit bei dieser Tabelle, die die Leerzeichen ignoriert und noch transponiert werden muss
ID MONTH value1_a
1234 1 49
1238 1 56
1239 4 17
1245 1 8
Aber ich muss immer noch für alle anderen laufen und verketten? oder muss ich alle produkte schreiben? und dann transponieren? SQL könnte dies in einem Rutsch tun, oder? oder denke ich zu viel über Python nach?
Ich muss immer noch für alle anderen laufen und verketten? oder muss ich alle produkte schreiben? und dann transponieren? SQL könnte dies in einem Rutsch tun, oder?
Unten Lösung macht es
execute immediate (
select '''select * from (select id, lower(product) || '_' || month product_month, value_1 from `project.dataset.table`)
pivot(sum(value_1) for product_month in ("''' || string_agg(product_month, '", "') || '''"))
order by id
'''
from (
select product || '_' || month product_month
from (select distinct lower(product) product from `project.dataset.table`)
cross join (select distinct month from `project.dataset.table`)
order by product_month)
);
Bei Anwendung auf Beispieldaten in Ihrer Frage - Ausgabe ist
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen