在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)
来设置输出格式,指定了年份和每个月份的销售额。通过设置输出格式,可以将结果以更易读的方式进行展示。