您沒有指定您所使用的RDBMS但有幾個方法,你可以將這些數據轉化爲你所需要的結果。
如果您使用的是不具有PIVOT
功能的數據庫,那麼可以通過使用UNION ALL
查詢unpivot的數據,然後使用聚合函數與CASE
表達日期轉動到列中完成。查詢將類似於此:
select col,
cat_id,
max(case when dateon = '2013-02-12' then value end) [2013-02-12],
max(case when dateon = '2013-02-13' then value end) [2013-02-13],
max(case when dateon = '2013-02-14' then value end) [2013-02-14],
max(case when dateon = '2013-02-15' then value end) [2013-02-15]
from
(
select cat_id, 'amount1' col, amount1 value, dateon
from tbl_login
where dateon >= '2013-02-12'
and dateon <= '2013-02-15'
union all
select cat_id, 'amount2' col, cast(amount2 as decimal(10,2)) value, dateon
from tbl_login
where dateon >= '2013-02-12'
and dateon <= '2013-02-15'
union all
select cat_id, 'amount3' col, cast(amount3 as decimal(10,2)) value, dateon
from tbl_login
where dateon >= '2013-02-12'
and dateon <= '2013-02-15'
) src
group by col, cat_id
order by cat_id, col
見SQL Fiddle with Demo
如果您正在使用SQL Server 2005+或Oracle 11g +,那麼你就可以同時使用UNPIVOT
和PIVOT
功能:
select col, cat_id,
[2013-02-12], [2013-02-13],
[2013-02-14], [2013-02-15]
from
(
select cat_id, dateon,
col, value
from
(
select cat_id, amount1,
cast(amount2 as decimal(10,2)) amount2,
cast(amount3 as decimal(10,2)) amount3,
dateon
from tbl_login
where dateon >= '2013-02-12'
and dateon <= '2013-02-15'
) s
unpivot
(
value
for col in (Amount1, Amount2, Amount3)
) unpiv
) src
pivot
(
max(value)
for dateon in ([2013-02-12], [2013-02-13],
[2013-02-14], [2013-02-15])
) piv
order by cat_id, col
請參閱SQL Fiddle with Demo。
如果你有一個未知的數字,你要轉換爲列中的日期,那麼你可以使用動態SQL(注:動態代碼爲SQL Server的語法):
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@startdate datetime,
@enddate datetime
set @startdate ='2013-02-12'
set @enddate ='2013-02-15'
;with dates (dt) as
(
select @startdate
union all
select dateadd(dd, 1, dt)
from dates
where dateadd(dd, 1, dt) <= @enddate
)
select dt
into #temp
from dates
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(convert(varchar(10), dt, 120))
from #temp
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT col, cat_id,' + @cols + '
from
(
select cat_id, dateon,
col, value
from
(
select cat_id, amount1,
cast(amount2 as decimal(10,2)) amount2,
cast(amount3 as decimal(10,2)) amount3,
dateon
from tbl_login
where dateon >= '''+convert(varchar(10), @startdate, 120)+'''
and dateon <= '''+convert(varchar(10), @enddate, 120)+'''
) s
unpivot
(
value
for col in (Amount1, Amount2, Amount3)
) unpiv
) src
pivot
(
max(value)
for dateon in (' + @cols + ')
) p
order by cat_id, col'
execute(@query)
見SQL Fiddle with Demo
所有查詢的結果是:
| COL | CAT_ID | 2013-02-12 | 2013-02-13 | 2013-02-14 | 2013-02-15 |
------------------------------------------------------------------------
| amount1 | 1 | 10 | 20 | (null) | (null) |
| amount2 | 1 | 12 | 22 | (null) | (null) |
| amount3 | 1 | 12 | 22 | (null) | (null) |
| amount1 | 2 | 10 | 20 | (null) | (null) |
| amount2 | 2 | 12 | 22 | (null) | (null) |
| amount3 | 2 | 12 | 22 | (null) | (null) |
| amount1 | 3 | 10 | 20 | (null) | (null) |
| amount2 | 3 | 12 | 22 | (null) | (null) |
| amount3 | 3 | 12 | 22 | (null) | (null) |
| amount1 | 4 | 10 | (null) | (null) | (null) |
| amount2 | 4 | 12 | (null) | (null) | (null) |
| amount3 | 4 | 12 | (null) | (null) | (null) |
| amount1 | 5 | (null) | 20 | (null) | (null) |
| amount2 | 5 | (null) | 22 | (null) | (null) |
| amount3 | 5 | (null) | 22 | (null) | (null) |
與其他列S中的類型的類型列「amount1」衝突在UNPIVOT列表中成爲了特徵。 – 2013-02-15 18:12:12
這些列上的數據類型是什麼?如果小數,精度和長度是多少? – Taryn 2013-02-15 18:13:45
amount1 decimal(10,2),amount1 smallint – 2013-02-15 18:24:38