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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:47:29+00:00 2026-05-22T17:47:29+00:00

Scenario 1: Table: id column1 column2 1 bla foo 2 bla bar I want

  • 0

Scenario 1:

Table:

id column1 column2
1  "bla"   "foo"
2  "bla"   "bar"

I want to group by column1 and get null for column2, cause there’s not the same value in all rows.

Scenario 2:

Table:

id column1 column2
1  "bla"   "foo"
2  "bla"   "foo"

I want to group by column1 and get foo for column2, cause all values of column2 are equal.

Is it possible to solve this by a sql statement?

  • 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-22T17:47:30+00:00Added an answer on May 22, 2026 at 5:47 pm

    Since you want to group by column1, one way to know if column2 has all same values is to check if max(column2) = min(column2), therefore this should work:

    select column1, case 
                        when max(column2) = min(column2) then max(column2)
                        else null
                    end as col2
      from tabletest
     group by column1;
    

    Edit

    If your column2 cannot accept null values, then the above query is ok, otherwise you need the following one to handle cases when column2 is null:

    select t.column1, case 
                          when (select max(1) from tabletest where column2 is null and column1 = t.column1) = 1 then null
                          when max(t.column2) = min(t.column2) then max(t.column2)
                          else null
                      end as col2
      from tabletest t
     group by t.column1;
    

    The only difference is that we need to add a case condition to cover the case of when column2 is null

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

Sidebar

Related Questions

Here is a simple scenario with table characters: CharacterName GameTime Gold Live Foo 10
batch create date table column type is BATCH_CREATED_DATE DATE NOT NULL With the data
Examine the following scenario. There is a table with two columns - DateWithoutMilliseconds and
I have a scenario where I get a data table with 65 columns and
There are table A and table B. I want to join these tables on
Consider the following design: Company TABLE ( CompanyId int NOT NULL IDENTITY PRIMARY KEY,
Here's my scenario: I've got a table of (let's call them) nodes. Primary key
I am using Spring and JDBCTemplate. The scenario is a CUSTOMER table and ORDERS
I have a case of updating the target table where source columns data not
There can only be one IDENTITY column per table Why is it so? Take

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.