1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| WITH ventes (id_client, dt_vente, qte) AS
(
select 1, cast('2006-03-05' as smalldatetime), 1 union all
select 1, cast('2007-04-06' as smalldatetime), 3 union all
select 1, cast('2008-02-15' as smalldatetime), 5 union all
select 1, cast('2009-12-25' as smalldatetime), 7 union all
select 2, cast('2006-03-05' as smalldatetime), 2 union all
select 2, cast('2007-05-12' as smalldatetime), 4 union all
select 2, cast('2008-09-23' as smalldatetime), 6 union all
select 2, cast('2009-11-29' as smalldatetime), 8 union all
select 2, cast('2009-07-05' as smalldatetime), 2 union all
select 2, cast('2006-03-05' as smalldatetime), 4 union all
select 3, cast('2006-03-05' as smalldatetime), 1 union all
select 3, cast('2006-03-05' as smalldatetime), 2 union all
select 3, cast('2006-03-05' as smalldatetime), 3 union all
select 4, cast('2009-09-21' as smalldatetime), 4 union all
select 5, cast('2003-10-22' as smalldatetime), 5
)
select
id_client,
coalesce([2005], 0) as [2005],
coalesce([2006], 0) as [2006],
coalesce([2007], 0) as [2007],
coalesce([2008], 0) as [2008],
coalesce([2009], 0) as [2009]
from
(
select id_client, year(dt_vente) as an_vente, qte
from ventes
) as vte pivot ( sum(qte) for an_vente in ( [2005], [2006], [2007], [2008], [2009]) ) as pvt;
id_client 2005 2006 2007 2008 2009
----------- ----------- ----------- ----------- ----------- -----------
1 0 1 3 5 7
2 0 6 4 6 10
3 0 6 0 0 0
4 0 0 0 0 4
5 0 0 0 0 0 |
Partager