存储过程,将输出分组到新列

时间:2011-08-18 05:06:47

标签: sql sql-server-2005 tsql sql-server-2008 stored-procedures

我在下面有这个存储过程,它允许我选择一个问题ID并列出用户名和他们的答案。

我如何更改它,以便只显示带有新列名为total 的问题ID,如下所示

enter image description here

CREATE PROCEDURE dbo.report
  @Question_ID varchar(5)
AS
  SELECT * FROM submit_Answer
  WHERE Question_ID=@Question_ID

谢谢!

1 个答案:

答案 0 :(得分:1)

使用此方法为每个Question_ID获取一行,并在总计列中回答并计算出现次数。

CREATE PROCEDURE dbo.report
  @Question_ID varchar(5)
AS
  SELECT Question_ID,
         Answer,
         COUNT(*) as Total
  FROM submit_Answer
  WHERE Question_ID=@Question_ID
  GROUP BY Question_ID,
           Answer