SQL查询需要合并相似的行
我有一个如下的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,因为我不知道结果的每一列来自哪个表,所以我必须为此使用临时表。然后,您将需要动态 sql,因此请先查看此链接。然后,尝试这样的事情:
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:
感谢大家的努力。一旦我们的数据库升级到 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.