SQL查询需要合并相似的行

发布于 2024-11-05 20:25:18 字数 1752 浏览 1 评论 0原文

我有一个如下的 sql 查询:

    SELECT
        EVENTTYPEDESCRIPTION,
        YEAR(EVENTSTARTDATE) as 'EVENTYEAR',
        COUNT(PARTICIPANTID) AS 'TOTALPARTICIPANTS'
    FROM WEBPROGRAMPARTICIPANTS
    INNER JOIN WEBPROGRAMS
        ON WEBPROGRAMPARTICIPANTS.PROGRAMCODE = WEBPROGRAMS.PROGRAMCODE
    INNER JOIN WEBEVENTS
        ON WEBPROGRAMS.PROGRAMID = WEBEVENTS.EVENTID
    INNER JOIN WEBEVENTTYPEDESCRIPTIONS
        ON WEBEVENTS.EVENTTYPE = WEBEVENTTYPEDESCRIPTIONS.EVENTTYPE
    WHERE WEBPROGRAMPARTICIPANTS.ACCEPTED = 1
    GROUP BY EVENTTYPEDESCRIPTION, YEAR(EVENTSTARTDATE)
    ORDER BY EVENTTYPEDESCRIPTION, YEAR(EVENTSTARTDATE)

返回以下结果:

EventTypeDescripti  Year    TotalParticipants
Affiliate Workshop  2004    96
Affiliate Workshop  2005    132
Affiliate Workshop  2006    94
Affiliate Workshop  2007    125
Affiliate Workshop  2008    121
Affiliate Workshop  2010    170
Affiliate Workshop  2011    1
IPAM Long Program   2000    59
IPAM Long Program   2001    203
IPAM Long Program   2002    94
IPAM Long Program   2003    182
IPAM Long Program   2004    147
IPAM Long Program   2005    123
IPAM Long Program   2006    99
IPAM Long Program   2007    116
IPAM Long Program   2008    98
IPAM Long Program   2009    127
IPAM Long Program   2010    147
IPAM Long Program   2011    105
IPAM Long Program   2012    14
IPAM Reunion Conference 2002    108
IPAM Reunion Conference 2003    100
IPAM Reunion Conference 2004    98
IPAM Reunion Conference 2005    68

我的问题是如何重建查询以将每个 EventTypeDescription 压缩为一行,并将每年显示为一列及其各自的计数

示例:

Description            Year            Year           etc...
Affiliate Workshop     96              132            ....    

I have a sql query as follows:

    SELECT
        EVENTTYPEDESCRIPTION,
        YEAR(EVENTSTARTDATE) as 'EVENTYEAR',
        COUNT(PARTICIPANTID) AS 'TOTALPARTICIPANTS'
    FROM WEBPROGRAMPARTICIPANTS
    INNER JOIN WEBPROGRAMS
        ON WEBPROGRAMPARTICIPANTS.PROGRAMCODE = WEBPROGRAMS.PROGRAMCODE
    INNER JOIN WEBEVENTS
        ON WEBPROGRAMS.PROGRAMID = WEBEVENTS.EVENTID
    INNER JOIN WEBEVENTTYPEDESCRIPTIONS
        ON WEBEVENTS.EVENTTYPE = WEBEVENTTYPEDESCRIPTIONS.EVENTTYPE
    WHERE WEBPROGRAMPARTICIPANTS.ACCEPTED = 1
    GROUP BY EVENTTYPEDESCRIPTION, YEAR(EVENTSTARTDATE)
    ORDER BY EVENTTYPEDESCRIPTION, YEAR(EVENTSTARTDATE)

Which returns the following results:

EventTypeDescripti  Year    TotalParticipants
Affiliate Workshop  2004    96
Affiliate Workshop  2005    132
Affiliate Workshop  2006    94
Affiliate Workshop  2007    125
Affiliate Workshop  2008    121
Affiliate Workshop  2010    170
Affiliate Workshop  2011    1
IPAM Long Program   2000    59
IPAM Long Program   2001    203
IPAM Long Program   2002    94
IPAM Long Program   2003    182
IPAM Long Program   2004    147
IPAM Long Program   2005    123
IPAM Long Program   2006    99
IPAM Long Program   2007    116
IPAM Long Program   2008    98
IPAM Long Program   2009    127
IPAM Long Program   2010    147
IPAM Long Program   2011    105
IPAM Long Program   2012    14
IPAM Reunion Conference 2002    108
IPAM Reunion Conference 2003    100
IPAM Reunion Conference 2004    98
IPAM Reunion Conference 2005    68

My question is how do I reconstruct the query to show each EventTypeDescription condensed into one line and show each year as a column with its respective count

Example:

Description            Year            Year           etc...
Affiliate Workshop     96              132            ....    

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

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

发布评论

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

评论(3

把时间冻结 2024-11-12 20:25:18

好的,因为我不知道结果的每一列来自哪个表,所以我必须为此使用临时表。然后,您将需要动态 sql,因此请先查看此链接。然后,尝试这样的事情:

DECLARE @Years NVARCHAR(MAX), @Query NVARCHAR(MAX)
SET @Years = ''

SELECT eventtypedescription, 
       YEAR(eventstartdate) AS 'EVENTYEAR', 
       COUNT(participantid) AS 'TOTALPARTICIPANTS' 
INTO #Results
FROM   webprogramparticipants 
INNER JOIN webprograms 
 ON webprogramparticipants.programcode = webprograms.programcode 
INNER JOIN webevents 
 ON webprograms.programid = webevents.eventid 
INNER JOIN webeventtypedescriptions 
 ON webevents.eventtype = webeventtypedescriptions.eventtype 
WHERE  webprogramparticipants.accepted = 1 
GROUP  BY eventtypedescription, YEAR(eventstartdate) 


SELECT @Years = @Years + '[Year ' + CAST(EVENTYEAR AS VARCHAR) +'],'
FROM #Results
GROUP BY EVENTYEAR
ORDER BY EVENTYEAR

SET @Years = LEFT(@Years,LEN(@Years)-1)

SET @Query = '
SELECT *
FROM (  SELECT eventtypedescription, TOTALPARTICIPANTS, ''Year ''+CAST(EVENTYEAR AS VARCHAR) Years
        FROM #Results) T
PIVOT(SUM(TOTALPARTICIPANTS) FOR Years IN ('+@Years+')) AS PT'

EXEC sp_executesql @Query

Ok, since I don't know from what table each column of your result comes, then I'm gonna have to use a temp table for this. Then, you are gonna need dynamic sql, so take a look at this link first. Then, try something like this:

DECLARE @Years NVARCHAR(MAX), @Query NVARCHAR(MAX)
SET @Years = ''

SELECT eventtypedescription, 
       YEAR(eventstartdate) AS 'EVENTYEAR', 
       COUNT(participantid) AS 'TOTALPARTICIPANTS' 
INTO #Results
FROM   webprogramparticipants 
INNER JOIN webprograms 
 ON webprogramparticipants.programcode = webprograms.programcode 
INNER JOIN webevents 
 ON webprograms.programid = webevents.eventid 
INNER JOIN webeventtypedescriptions 
 ON webevents.eventtype = webeventtypedescriptions.eventtype 
WHERE  webprogramparticipants.accepted = 1 
GROUP  BY eventtypedescription, YEAR(eventstartdate) 


SELECT @Years = @Years + '[Year ' + CAST(EVENTYEAR AS VARCHAR) +'],'
FROM #Results
GROUP BY EVENTYEAR
ORDER BY EVENTYEAR

SET @Years = LEFT(@Years,LEN(@Years)-1)

SET @Query = '
SELECT *
FROM (  SELECT eventtypedescription, TOTALPARTICIPANTS, ''Year ''+CAST(EVENTYEAR AS VARCHAR) Years
        FROM #Results) T
PIVOT(SUM(TOTALPARTICIPANTS) FOR Years IN ('+@Years+')) AS PT'

EXEC sp_executesql @Query
不醒的梦 2024-11-12 20:25:18
  1. 使用游标生成您需要的行的字符串
  2. 使用数据透视表数据透视表 EventTypeDescription 列上的数据。示例正在路上。
  1. Use a cursor to generate a string of which rows you'll need
  2. Use a pivot to pivot the data on the EventTypeDescription column. Example on its way.
猥︴琐丶欲为 2024-11-12 20:25:18

感谢大家的努力。一旦我们的数据库升级到 2008,我就会记住 PIVOT 示例。由于现在我们使用 SQL Server 2000,所以我决定在我的 C# 代码中进行数据透视。

无论如何,这种方式实际上对我来说更好,因为我可以传入任何选择查询结果,并且它将对其进行旋转。

Thank you all for your efforts. I am going to remember the PIVOT example once our database is upgraded to 2008. For now since we are using SQL Server 2000 I decided to do the pivot within my c# code.

It is actually better this way forme anyway, since the way Icreaetd it I can pass in any select query result and it will pivot it.

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