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

  • Home
  • SEARCH
  • 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 7496315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:33:51+00:00 2026-05-29T18:33:51+00:00

I am using SQL Server and I have to group by a few columns

  • 0

I am using SQL Server and I have to group by a few columns but only if a setting is true.

DECLARE @setting tinyint
SET @setting = 0

SELECT col1 FROM table1 
GROUP BY col1,
  CASE WHEN @setting = 1 THEN col2 ELSE NULL END

OR should I default to col1 that I always use to group by if the setting is set, so the code would would be

CASE WHEN @test = 1 THEN col2 ELSE col1 END

It does actually work to use the NULL but I can’t find an example on the internet to prove that it is correct usage.

  • 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-29T18:33:53+00:00Added an answer on May 29, 2026 at 6:33 pm
    SELECT col1 FROM table1 
    GROUP BY col1,
      CASE WHEN @setting = 1 THEN col2 ELSE NULL END
    

    Your usage is correct. You already know it works.
    ELSE NULL is implied in CASE, so you could have written it as

    GROUP BY col1, CASE WHEN @setting = 1 THEN col2 END

    You could try to be coy with variants like

    GROUP BY col1, @setting * col2 –for numeric col2
    GROUP BY col1, COALESCE(NULLIF(@setting,1), col2)

    But the CASE statement actually unrolls to a better, simpler plan.

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

Sidebar

Related Questions

I'm using SQL Server 2008 and I have a table with three columns: Length
HI, Using SQL server 2005 I have the following query: SELECT contact_id ,YEAR(date_created) AS
I'm using SQL Server 2008 Management studio viewing a 2005 server and have just
[using SQL Server 2005] I have a table full of users, I want to
Using MS SQL Server .I have made a Stored Procedure named SP_Get_CallsLogged . I
We are using SQL Server 2000 and have to move data across servers to
I am using SQL Server 2000 and I have two databases that both replicate
I'm using SQL Server 2005 express I have a datetime field in a table
I am using Microsoft SQL Server and I have a master-detail scenario where I
I am using SQL Server 2005. I have a table with a text column

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.