Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3306832
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:20:07+00:00 2026-05-17T21:20:07+00:00

Trying to get a basic understanding of T-SQL here in SQL Server 2008. Suppose

  • 0

Trying to get a basic understanding of T-SQL here in SQL Server 2008. Suppose I have a table named “Issues” with columns such as:

Priority User
1        Foo
1        Foo
2        Foo
5        Foo
4        Bar
5        Bar
1        Bar
1        Fuz

and I wish to display a count of the Priority for each User, along with a breakdown of each Priority, such that the resulting table might be named “Breakdown” might look like

User Total 1 2 3 4 5
Foo  4     2 1 0 0 1
Bar  3     1 0 0 1 1
Fuz  1     1 0 0 0 0

I was thinking I might declare variables and write my query something like

DECLARE @P1 INT
DECLARE @P2 INT
DECLARE @P3 INT
DECLARE @P4 INT
DECLARE @P5 INT

SELECT COUNT(id) AS Total,UserName, 
CASE Priority
 WHEN 1 Then @P1 = @P1 + 1
 WHEN 2 Then @P2 = @P2 + 1
 WHEN 3 Then @P3 = @P3 + 1
 WHEN 4 Then @P4 = @P4 + 1
 WHEN 5 Then @P5 = @P5 + 1
END,
FROM Breakdown
GROUP BY UserName

but I’m pretty sure I’m on the wrong track. Does anyone have any suggestions?

Thanks, and sorry for the noobish question; but I’m not sure exactly what to google for here…

-R.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:20:07+00:00Added an answer on May 17, 2026 at 9:20 pm

    Use:

      SELECT i.user, 
             COUNT(i.priority) AS total,
             SUM(CASE WHEN i.priority = 1 THEN 1 ELSE 0 END) AS 1,
             SUM(CASE WHEN i.priority = 2 THEN 1 ELSE 0 END) AS 2,
             SUM(CASE WHEN i.priority = 3 THEN 1 ELSE 0 END) AS 3,
             SUM(CASE WHEN i.priority = 4 THEN 1 ELSE 0 END) AS 4,
             SUM(CASE WHEN i.priority = 5 THEN 1 ELSE 0 END) AS 5
        FROM ISSUES i
    GROUP BY i.user
    

    It’s a pivot query, converting row data into columnar data.
    Not a noob/beginner issue to deal with. SQL Server 2005+ added the (now ANSI) PIVOT/UNPIVOT syntax, but this is portable to most databases (because few currently support PIVOT/UNPIVOT).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get some basic understanding of console functionalities. I am having issues so
Im trying to get into some basic JavaFX game development and I'm getting confused
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
I'm trying to gain a basic understanding of what is meant by a Windows
I'm trying to get a better understanding of how Windows, 32-bit, calculates the virtual
I've used procedural for sometime now and trying to get a better understanding of
I am currently taking a c++ course and trying to get a deep understanding
I have some small problem in understanding Value Objects in Flex... I'm trying to
I have been trying to get this to work. Basically I have a search
I have a new project that I am trying to get up and running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.