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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:17:33+00:00 2026-05-26T16:17:33+00:00

Insert into x (…) select a,b,c from prq where p_a is null group by

  • 0
Insert into x (...)
select a,b,c from prq where p_a is null group by a,b,c

Now, my requirement is that, if p_a is null then i have to group by a,b,c and select only a,b,c column.

But if p_a is not null then, i have to group by two new column x,y and also select these two columns.
These x,y columns are NULLABLE in table x.

  • 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-26T16:17:34+00:00Added an answer on May 26, 2026 at 4:17 pm

    Table X must have at least five columns whose names, we can presume, are a, b, c, x, y.

    If you are doing a single INSERT, then you’ll need to insert into all five columns. If you are doing multiple INSERT operations, you can insert into 3 and then 5 (or vice versa) columns. You may have to do some juggling with the NULL values in the select-list of the first alternative. I’m assuming that the columns x and y are INTEGER for definiteness – choose the appropriate type.

    1st Alternative

    INSERT INTO x(a, b, c, x, y)
        SELECT a, b, c, MAX(CAST(NULL AS INTEGER)) AS x, MAX(CAST(NULL AS INTEGER)) AS y
          FROM pqr
         WHERE p_a IS NULL
         GROUP BY a, b, c
        UNION
        SELECT MAX(a) AS a, MAX(b) AS b, MAX(c) AS c, x, y
          FROM pqr
         WHERE p_a IS NOT NULL
         GROUP BY x, y;
    

    You could replace the GROUP BY a, b, c clause with a DISTINCT in front of a in the select-list of the first part of the UNION. In most SQL DBMS, you must list all the non-aggregate columns from the select-list in the GROUP BY clause. Using the MAX means that you have aggregates for x and y in the first half of the UNION and for a, b and c in the second half of the UNION.

    2nd Alternative

    INSERT INTO x(a, b, c)
        SELECT DISTINCT a, b, c
          FROM pqr
         WHERE p_a IS NULL;
    INSERT INTO x(a, b, c, x, y)
        SELECT MAX(a) AS a, MAX(b) AS b, MAX(c) AS c, x, y
          FROM pqr
         WHERE p_a IS NOT NULL
         GROUP BY x, y;
    

    As discussed before, you need aggregates on the columns not in the GROUP BY list.

    3rd Alternative

    If you meant that you must group by x and y as well as a, b and c, then the second half of the UNION (or the second SELECT) simplifies to:

        SELECT a, b, c, x, y
          FROM pqr
         WHERE p_a IS NOT NULL
         GROUP BY a, b, c, x, y;
    

    Or you can use DISTINCT again:

        SELECT DISTINCT a, b, c, x, y
          FROM pqr
         WHERE p_a IS NOT NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

INSERT INTO `tableA` SELECT `Col1`, `Col2`, NOW() FROM `tableB` WHERE tableA.Col1 is not already
INSERT INTO [Tasks] ([LoginName] ,[Type] ,[Filter] ,[Dictionary] ,[Description]) Select N'Anonymous',4,'SomeTable.targetcode in (select Code from
INSERT INTO product_search_table (id, score) VALUES ( SELECT product_id, 50 FROM cart_product WHERE product_id
insert into test.qvalues select * from qcvalues.qvalues; i would like to knwo if the
If I type: INSERT INTO table_b SELECT rowid, somecolumn FROM table_a ...the rowid column
INSERT INTO TextTable(Number, Tokens) SELECT (SELECT ID FROM Tureme WHERE leksem IN (SELECT Tokens
INSERT INTO timecrunch.dbo.intervals (IntervalID, Duration) SELECT ixInterval, DATEDIFF(hour, dtStart, dtEnd) FROM fogbugz.dbo.TimeInterval WHERE dtEnd
Here's my script: INSERT INTO AP09 SELECT cds AS CDSCODE FROM P_Schools WHERE active
I am trying to insert into Table A, unique data from Table B that
INSERT INTO Comment (info, pID, cID) VALUES(?,?,?) SELECT Comm.cID FROM Professor P, Comment Comm,

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.