我是 Postgres 和函式的新手crosstab()。我有這樣的查詢:
select * from crosstab(
'select artis_clean, release_year, total_count_song from top_artis_5year ' ,
'select distinct release_year from top_artis_5year order by 1 '
) as (
"artis_clean" text, "1965" int , "1966" int, "1967" int, "1968" int,"1969" int,"1970" int
);
我得到這樣的結果:
artis_clean 1965 1966 1967 1968 1969 1970
------------ ---- ---- ---- ---- ---- -----
the beatles null null 23 null null null
led zepelin null null null null 18 null
the beatles 15 null null 13 13 null
led zepelin 12 null null null null null
jimi hendrix null null 9 null null null
jimi hendrix 8 null null null null null
為什么我輸入artis_clean時仍然有重復資料?artis_cleandistinct
'select distinct release_year,artis_clean from top_artis_5year order by 1 '
代碼不起作用,Postgres 說:
ERROR: provided "categories" SQL must return 1 column of at least one row
SQL state: 42601
uj5u.com熱心網友回復:
輸入需要排序。附加ORDER BY 1到您的第一個查詢的第一個引數。
release_year此外,如果列定義串列是靜態的,則動態查詢第二個引數是沒有意義的。
看:
- PostgreSQL 交叉表查詢
要替換生成的 NULL 值,COALESCE請在外部使用SELECT. 例子:
- 在 PostgreSQL 中使用行值作為列
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/415517.html
標籤:
上一篇:使用多列的連續行之間的差異
