sql 查询(可以用pivot / unpivot 解决?)

发布于 2024-09-18 12:09:21 字数 2872 浏览 5 评论 0原文

我不确定如何最好地解决这个问题 - 我认为应该使用数据透视/反透视,但不确定如何使其工作(因为数据透视列是非数字)

我有下表(基于我的查询)无法修改):

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 年不一定属于该类别,所以一个人有可能每年出现在不同的行中?

我知道所有类别选项都是固定的,因此不需要动态旋转或类似的东西,以防有帮助:)

主要目标是能够一目了然地看到每年每个类别的计数(所以我可以将所有年份相互比较)-非常感谢任何帮助!

I'm not sure how best to approach this - I think a pivot / unpivot should be used, but not sure how to make it work (as pivoting column is a non-numeric)

I have the following table (based on a query I can't modify):

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)

And what I need to produce is a table which looks like this:

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...

However the problem I have is that if someone is a lapsed donor in 2005, they aren't necessarily in that category for 2006-2010, so one person has the potential to appear in a different row for each year?

I know that all of the category options are fixed, so there is no need for dynamic pivoting or anything like that, in case it helps :)

The main objective is to be able to see a count of each category for each year at a glance (so i cna compare all years against each other) - any help massively appreciated!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

徒留西风 2024-09-25 12:09:21

这是你需要的吗?

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

Is this what you need?

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

Returns

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

Following Clarification

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文