2010-09-13 64 views
6

我不知道如何最好的方法 - 我認爲應該使用樞軸/ unpivot,但不知道如何使它工作(因爲pivoting列是非-numeric)SQL查詢(可能與pivot/unpivot解決嗎?)

我有如下表(基於查詢我不能修改)

的:

CREATE TABLE #data 
(donor_id NVARCHAR(50) 
,last_gift DATETIME 
,[2005] NVARCHAR(50) 
,numgifts05 INT 
,value_05 MONEY 
,[2006] NVARCHAR(50) 
,numgifts06 INT 
,value_06 MONEY 
,[2007] NVARCHAR(50) 
,numgifts07 INT 
,value_07 MONEY 
,[2008] NVARCHAR(50) 
,numgifts08 INT 
,value_08 MONEY 
,[2009] NVARCHAR(50) 
,numgifts09 INT 
,value_09 MONEY 
,[2010] NVARCHAR(50) 
,numgifts10 INT 
,value_10 MONEY 
) 


INSERT INTO #data VALUES (001,'2000-03-23 00:00:00.000','lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00) 
INSERT INTO #data VALUES (002,'2006-08-01 00:00:00.000','reactivated donor',1,25.00,'2yrs consecutive',2,47.20,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00) 
INSERT INTO #data VALUES (003,'2010-02-12 00:00:00.000','non-donor',0,0.00,'non-donor',0,0.00,'non-donor',0,0.00,'non-donor',0,0.00,'new donor',2,112.50,'2yrs consecutive',1,116.08) 
INSERT INTO #data VALUES (004,'2010-01-04 00:00:00.000','non-donor',0,0.00,'non-donor',0,0.00,'non-donor',0,0.00,'non-donor',0,0.00,'non-donor',0,0.00,'new donor',1,12.00) 
INSERT INTO #data VALUES (005,'2009-12-09 00:00:00.000','non-donor',0,0.00,'non-donor',0,0.00,'non-donor',0,0.00,'non-donor',0,0.00,'new donor',1,18.00,'lapsed donor',0,0.00) 
INSERT INTO #data VALUES (006,'2005-01-10 00:00:00.000','new donor',1,20.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00) 
INSERT INTO #data VALUES (007,'2009-01-07 00:00:00.000','non-donor',0,0.00,'non-donor',0,0.00,'non-donor',0,0.00,'non-donor',0,0.00,'new donor',1,25.00,'lapsed donor',0,0.00) 
INSERT INTO #data VALUES (008,'1997-11-05 00:00:00.000','lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00) 
INSERT INTO #data VALUES (009,'1999-06-18 00:00:00.000','lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00,'lapsed donor',0,0.00) 
INSERT INTO #data VALUES (010,'2010-03-09 00:00:00.000','3/4yrs consecutive',12,120.00,'3/4yrs consecutive',12,120.00,'5yrs+ consecutive',13,142.40,'5yrs+ consecutive',13,170.00,'5yrs+ consecutive',13,160.00,'5yrs+ consecutive',3,30.00) 

,我需要產生什麼是它看起來像這樣的表:

Category   | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 

Lapsed donor  | count(lapsed for 2005) | 
New donor   | count(new donor for 2005) | 
2yrs consecutive | count(2yrs consecutive for 2005) | 
etc... 

但是,我的問題是,如果有人我作爲2005年已經失效的捐助者,他們不一定在2006 - 2010年期間屬於這一類別,因此一個人每年有可能出現在不同的行中?

我知道,所有的類別選項是固定的,所以沒有需要進行動態旋轉之類的東西,在情況下,它可以幫助:)

的主要目標是能夠看到的計數每一年的類別一目瞭然(所以我cna比較所有年份彼此) - 任何幫助大量讚賞!

+0

+1對於有用的DDL。 – 2010-09-13 12:51:25

回答

5

這是你所需要的?

SELECT Category,[2005],[2006],[2007],[2008],[2009],[2010] 
FROM 
(SELECT [2005],[2006],[2007],[2008],[2009],[2010],donor_id FROM #DATA) P 
UNPIVOT (Category FOR [YEAR] IN 
     ([2005],[2006],[2007],[2008],[2009],[2010]) 
)AS unpvt 
PIVOT 
(
COUNT (donor_id) 
FOR [YEAR] IN 
([2005],[2006],[2007],[2008],[2009],[2010]) 
) AS pvt 
ORDER BY Category 

返回

Category      2005  2006  2007  2008  2009  2010 
------------------------------ ----------- ----------- ----------- ----------- ----------- ----------- 
2yrs consecutive    0   1   0   0   0   1 
3/4yrs consecutive    1   1   0   0   0   0 
5yrs+ consecutive    0   0   1   1   1   1 
lapsed donor     3   4   5   5   5   7 
new donor      1   0   0   0   3   1 
non-donor      4   4   4   4   1   0 
reactivated donor    1   0   0   0   0   0 

以下澄清

With base AS 
(
SELECT 2005 as [Year], [2005] as Category, numgifts05 as numgifts, value_05 as value 
from #data 
union all 
SELECT 2006 as [Year], [2006] as Category, numgifts06 as numgifts, value_06 as value 
from #data 
) 
select 
    Category 
    ,COUNT(case when [Year]=2005 then 1 end) as [2005] 
    ,SUM(case when [Year]=2005 then numgifts end) as [numgifts05] 
    ,SUM(case when [Year]=2005 then value end) as [value_05] 
    ,COUNT(case when [Year]=2006 then 1 end) as [2006] 
    ,SUM(case when [Year]=2006 then numgifts end) as [numgifts06] 
    ,SUM(case when [Year]=2006 then value end) as [value_06] 
from base 
group by Category 
order by Category 
+0

太棒了!作爲一個假設的問題(如果你真的可以抽出時間來回答它),如果我想在每個欄目中增加詳細的禮物數量/禮品價值,那麼佈局將是2005年| #gifts05 | value05 | 2006 | #gifts06是否可以使用pivot/unpivot組合,或者這會需要不同的方法? – Dibstar 2010-09-13 13:17:40

+0

驚人的(嘗試所有的工會,但沒有重命名numgifts05是numgifts) - 再次感謝您的幫助,作品一個治療:) – Dibstar 2010-09-13 13:53:48