SQL 使用 PIVOT 权
SQL using PIVOT right
我有一个问题,我正在尝试在我的查询中使用 PIVOT 但没有任何结果。现在我有一个 table 看起来像:
Category Month Value
A August 10
B August 19
C August 15
A September 20
B September 23
C September 25
A October 24
B October 87
C October 44
我想做成这样:
Category August September October
A 10 20 24
B 19 23 87
C 15 25 47
在我的 select 中是这样的:
Select cat_name, CAST(month AS VARCHAR(20)), value from dbo.table1.
_
select * from (
select ft.categoryData as [category], CAST(fft.date AS VARCHAR(20)) as [month], tt.value as [value] from firstt ft
join secondt st on ft.id = st.id
join thirdt tt on ft.id = tt.type_id
join fourtht fft on ft.id = fft.category_id
where ft.date between '2015-07-01' and '2015-09-01' and ft.country = 'EUR'
group by fft.date, ft.categoryData, tt.value
) as t
PIVOT (
max(value)
for [date] in ([jul], [aug], [sept])
) as pvt
尝试Conditional Aggregate
。
select Category,
max(case when Month = 'August' then Value END) as August,
max(case when Month = 'September' then Value END) as September,
max(case when Month = 'October' then Value END) as October
from Yourtable
Group by Category
或使用Pivot
select *
from Yourtable
pivot (max(Value) For Month in ([August],[September],[October]))pv
当月份列中的值未知时使用动态 sql
declare @sql nvarchar(max)= '',
@col_list varchar(max)=''
set @col_list =(select distinct quotename(Month)+',' from yourtable for xml path (''))
set @col_list = left(@col_list,len(@col_list)-1)
set @sql = '
select *
from yourtable
pivot (max(Value) For Month in ('+@col_list+'))pv'
exec sp_executesql @sql
通过使用 pivot 我们可以编写下面的查询,语法遵循下面的 link
如前所述
SELECT *
FROM TABLE_NAME
PIVOT(MAX(VALUE) FOR MONTH IN (
[AUGUST]
,[SEPTEMBER]
,[OCTOBER]
)) AS
PIVOT_SALES
输出是
>
你可以使用下面的代码
SELECT * from (SELECT Category, Month, Value
FROM table1 t) pivot (sum (Value) for Month in ('August', 'September', October') ) order by Category;
我有一个问题,我正在尝试在我的查询中使用 PIVOT 但没有任何结果。现在我有一个 table 看起来像:
Category Month Value
A August 10
B August 19
C August 15
A September 20
B September 23
C September 25
A October 24
B October 87
C October 44
我想做成这样:
Category August September October
A 10 20 24
B 19 23 87
C 15 25 47
在我的 select 中是这样的:
Select cat_name, CAST(month AS VARCHAR(20)), value from dbo.table1.
_
select * from (
select ft.categoryData as [category], CAST(fft.date AS VARCHAR(20)) as [month], tt.value as [value] from firstt ft
join secondt st on ft.id = st.id
join thirdt tt on ft.id = tt.type_id
join fourtht fft on ft.id = fft.category_id
where ft.date between '2015-07-01' and '2015-09-01' and ft.country = 'EUR'
group by fft.date, ft.categoryData, tt.value
) as t
PIVOT (
max(value)
for [date] in ([jul], [aug], [sept])
) as pvt
尝试Conditional Aggregate
。
select Category,
max(case when Month = 'August' then Value END) as August,
max(case when Month = 'September' then Value END) as September,
max(case when Month = 'October' then Value END) as October
from Yourtable
Group by Category
或使用Pivot
select *
from Yourtable
pivot (max(Value) For Month in ([August],[September],[October]))pv
当月份列中的值未知时使用动态 sql
declare @sql nvarchar(max)= '',
@col_list varchar(max)=''
set @col_list =(select distinct quotename(Month)+',' from yourtable for xml path (''))
set @col_list = left(@col_list,len(@col_list)-1)
set @sql = '
select *
from yourtable
pivot (max(Value) For Month in ('+@col_list+'))pv'
exec sp_executesql @sql
通过使用 pivot 我们可以编写下面的查询,语法遵循下面的 link 如前所述
SELECT *
FROM TABLE_NAME
PIVOT(MAX(VALUE) FOR MONTH IN (
[AUGUST]
,[SEPTEMBER]
,[OCTOBER]
)) AS
PIVOT_SALES
输出是
你可以使用下面的代码
SELECT * from (SELECT Category, Month, Value
FROM table1 t) pivot (sum (Value) for Month in ('August', 'September', October') ) order by Category;