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 8457717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:52:53+00:00 2026-06-10T12:52:53+00:00

Good afternoon, I am trying to use CASE like in this question to transpose

  • 0

Good afternoon,

I am trying to use CASE like in this question to transpose rows to columns:

Rows to columns SQL Server query

However in my case I don’t have a column with information it’s a count of data.

I obtain the data with this query:

select MA, SN, count(*) 
from Original
where MA = 'AB'
group by MA

Result of the query:

MA  SN      COUNT
AB  TEXTA   6
AB  TEXTB   5
AB  TEXTC   3
AB  TEXTD   4

table Original

MA  SN      
AB  TEXTA   
AB  TEXTA   
AB  TEXTA   
AB  TEXTA   
AB  TEXTA   
AB  TEXTA   
.
.
.
AB  TEXTD   
AB  TEXTD   

table Result:

MA      TEXTA   TEXTB   TEXTC   TEXTD
AB      6       5       3       4

And this is my current query:

select  MA,
        count(*) as 'COUNT2',
    MAX(CASE WHEN SN = 'TEXTA' THEN COUNT2 end) as TEXTA1,
    MAX(CASE WHEN SN = 'TEXTB' THEN COUNT2 end) as TEXTB1,
    MAX(CASE WHEN SN = 'TEXTC' THEN COUNT2 end) as TEXTC1,
    MAX(CASE WHEN SN = 'TEXTD' THEN COUNT2 end) as TEXTD1,

from Original
where MA= 'AB'
group by MA

WHat am I doing horribly wrong in the query?. And can someone please explain why in the question I posted it works but not with the count?. Thank you! : ).

  • 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-06-10T12:52:55+00:00Added an answer on June 10, 2026 at 12:52 pm

    Edit: You can’t use COUNT2 in the way you intend because later columns in the select list cannot refer to aliases of previous columns. So COUNT2 has no meaning anywhere else in the query, unless you convert that part into a subquery (or cte) and then refer to it.

    So, with your updated schema, it makes sense that you will need a subquery to do the counts. Essentially drop your first query as a subquery to the second query (in replace of Original), and then add a few missing group by columns, and then it works:

    select  MA,
        MAX(CASE WHEN SN = 'TEXTA' THEN COUNT2 end) as TEXTA1,
        MAX(CASE WHEN SN = 'TEXTB' THEN COUNT2 end) as TEXTB1,
        MAX(CASE WHEN SN = 'TEXTC' THEN COUNT2 end) as TEXTC1,
        MAX(CASE WHEN SN = 'TEXTD' THEN COUNT2 end) as TEXTD1
    from (
        select MA, SN, count(*) as COUNT2
        from Original
        where MA = 'AB'
        group by MA, SN
    ) Original
    where MA= 'AB'
    group by MA
    

    http://www.sqlfiddle.com/#!3/41c79/7

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

Sidebar

Related Questions

Good afternoon I'm facing a trouble, trying to figure out how to use this
Good afternoon, I am trying to use as Linq to SQL datacontext for a
Good Afternoon, I am trying to use the composite filter but in vain. While
Good afternoon, i write in order to ask a question about the use of
Good afternoon all, What I'm trying to accomplish : I'd like to implement an
good afternoon How could I get a design like this in my android application?
I've spent a good while this afternoon trying to implement the deserialization of JSON
Good afternoon, I've run into some issues trying to combine HTTP caching with Rack::Cache
Good afternoon I'm hoping this is going to be nice and a simple f**k
Good afternoon, I have a quick 'top level' question regarding the usage of Facebook

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.