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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:09:15+00:00 2026-05-16T20:09:15+00:00

I have a large, messy report to write that joins across 5 tables. There

  • 0

I have a large, messy report to write that joins across 5 tables. There is one column in one table that is being used for several different values–essentially a “tag” column, where the tags are used in creative ways depending on what kind of miscellaneous metadata the users want to use.

As a result, my query for the report returns 3 nearly-identical rows that differ only in the “tag” column; for instance, I might get:

NAME TAG EMAIL  
BOB  A   BOB@EXAMPLE.COM  
BOB  B   BOB@EXAMPLE.COM  
BOB  C   BOB@EXAMPLE.COM  

What I’d like to do is split the contents of the TAG column to be returned as 3 separate columns from the query, like this:

NAME A B C EMAIL
BOB  A B C BOB@EXAMPLE.COM

So I tried using the SQL SERVER CASE/WHEN functionality to do this; I’m saying, for instance, when the value of the Tag column is “A”, return it in column “A”; if it’s “B,” put it in “B”; etc. I thought this would return the above, but instead it gives me this:

NAME A    B    C    EMAIL
BOB  A    NULL NULL BOB@EXAMPLE.COM
BOB  NULL B    NULL BOB@EXAMPLE.COM
BOB  NULL NULL C    BOB@EXAMPLE.COM

Which is clearly less than ideal.

Any thoughts, geniuses of Stack Overflow?

  • 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-16T20:09:16+00:00Added an answer on May 16, 2026 at 8:09 pm

    try this:

       Select Name,
        Min (Case When tag = 'A' Then Tag End) A,
        Min (Case When tag = 'B' Then Tag End) B,
        Min (Case When tag = 'C' Then Tag End) C,
        email
       From tableName
       Group By Name, email
    

    EDIT: to explain…
    Anytime you use a Group By, you are telling the query processor that you want it to aggregate the results into “buckets”, where each bucket is defined by unique values in the columns [or expressions] defined in the Group By clause. That means that the final result set will have one and only one row for each unique set of values in those columns [or expressions] defined in the Group By. All other columns or expressions used in the query, (other than those defined in the Group By), must be an expression based on an aggregation function (like Count(), Sum(), Avg(), Min(), Max(), etc…), that produce a value based on a calculation which will be applied to all the rows in the pre-aggregated result set. If, for example, I were to Group By the first character of the last name:

    Select Left(LastName, 1), Count(*), 
       Sum(Salaray, Avg(Height), 
       Min(DateOfBirth), etc.
      From Table
      Group By Left(LastName, 1)
    

    then I will get at most 26 rows in the output (one for each letter in the alphabet) and all other columns in my output must be based on some aggregation function to be applied to all the rows in the original set where the last name starts with an ‘A”, then all the rows where the last name starts with qa ‘B’ etc..

    In your problem, the Group By is used simply to restrict the output set to one row per distinct user and email. Once this is done, there will be only one row per The other columns in the Select statement need to have a Min(), [Max() would work just as well), only in order to satisfy the syntacical requirement mentioned in bold italics above.. In your case, there will only be one non-null row in the set so taking the Min(), or the Max() is necessary only because of the syntax requirement…

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

Sidebar

Related Questions

I have a Clojure proxy statement that was getting large and messy, so I
i have large numbers of text files and i am in problem that i
I have large table, with around 200 fields. Around a 100 of those fields
I have large database table, approximately 5GB, now I wan to getCurrentSnapshot of Database
I have large video files (~100GB) that are local on my machine. I have
I've inherited a rather large and somewhat messy codebase, and have been tasked with
I've written a program that queries a large and messy sql database and then
My mind seems to be going blank on this one. I have to write
This report used to take about 16 seconds when there were 8000 rows to
I have a rather large program that have some odd behaviour once in a

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.