在PostgreSQL中,使用Crosstab查詢時,可以通過在查詢語句中使用FORMAT
子句來設置輸出格式。可以設置的輸出格式包括row
、column
和value
。下面是一個示例:
SELECT * FROM crosstab(
'SELECT year, month, sales
FROM sales_data
ORDER BY 1,2',
'SELECT DISTINCT month FROM sales_data ORDER BY 1'
) AS ct(year text, january int, february int, march int, april int, may int, june int, july int, august int, september int, october int, november int, december int);
在上面的示例中,使用AS ct(year text, january int, february int, march int, april int, may int, june int, july int, august int, september int, october int, november int, december int)
來設置輸出格式,指定了年份和每個月份的銷售額。通過設置輸出格式,可以將結果以更易讀的方式進行展示。