Stefan Joseph
Stefan Joseph

Reputation: 63

Stored procedure, grouping output into a new column

I have this stored procedure below, it allows me to select a question id and list the username and their answer.

How would i change it so it only shows me the question id with a new column called total as below

enter image description here

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

Thanks!

Upvotes: 2

Views: 175

Answers (1)

Mikael Eriksson
Mikael Eriksson

Reputation: 138960

Use this to get one row for each Question_ID and Answer and count the number of occurrences in column Total.

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

Upvotes: 1

Related Questions