DBAsupport.com Forums - Powered by vBulletin
Results 1 to 10 of 10

Thread: How to merge the following two queries into one?

  1. #1
    Join Date
    Apr 2001
    Posts
    127

    How to merge the following two queries into one?

    Please let me know how to merge the following two queries into one:

    select test1.col2,
    sum(test1.col3)
    from test1
    where test1.col2 is not null
    group by test1.col2
    union
    select test1.col2,
    test1.col3
    from test1
    where test1.col2 is null

  2. #2
    Join Date
    Dec 2005
    Location
    Hangzhou, China
    Posts
    7
    It just looks like this:

    select test1.col2,
    sum(test1.col3)
    from test1
    group by test1.col2

    Don't care about the NULL too much.

  3. #3
    Join Date
    Dec 2005
    Location
    Hangzhou, China
    Posts
    7
    Ahh, sorry, I made a mistake if you didn't mean group by the NULL col2 like the un-null col2.

  4. #4
    Join Date
    Mar 2002
    Posts
    534
    You may replace the union by a "union all".

    Why do you want to join the 2 queries?

  5. #5
    Join Date
    Apr 2001
    Posts
    127
    Because I don't want to query test1 table twice if I can do it in one query.

  6. #6
    Join Date
    Nov 2000
    Location
    Pittsburgh, PA
    Posts
    4,166
    Try this query instead.

    Code:
    SELECT NVL ( test1.col2,    'ISNULL', 'NOTNULL' ) col2_null,
           SUM ( test1.col3 )
      FROM test1
     GROUP BY NVL ( test1.col2, 'ISNULL', 'NOTNULL' );

  7. #7
    Join Date
    Mar 2002
    Posts
    534
    could you please show the explain plan of the query using UNION ALL

    EXPLAIN PLAN FOR
    select test1.col2,
    sum(test1.col3)
    from test1
    where test1.col2 is not null
    group by test1.col2
    union all
    select test1.col2,
    test1.col3
    from test1
    where test1.col2 is null;

    SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY());

  8. #8
    Join Date
    May 2000
    Location
    ATLANTA, GA, USA
    Posts
    3,135
    Quote Originally Posted by zxmgh
    Please let me know how to merge the following two queries into one:

    select test1.col2,
    sum(test1.col3)
    from test1
    where test1.col2 is not null
    group by test1.col2
    union
    select test1.col2,
    test1.col3 ---- sum is missing ???
    from test1
    where test1.col2 is null
    Tamil

  9. #9
    Join Date
    Mar 2002
    Posts
    534
    I dont think that sum is missing. If it should be so there would be no reason to divide the query in two parts.

  10. #10
    Join Date
    Dec 2005
    Location
    Hangzhou, China
    Posts
    7
    Maybe you all misunderstood.

    I guess Mr zxmgh meant that he just wanted the records with un-null col2 be grouped by col2 and calculate col3 as a sum. He didn't want the records with null col2 be grouped, huh?

    If so I don't think those two queries could be combined to one unless there is only one record with null col2 in test1.
    Last edited by tamade; 12-22-2005 at 11:24 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width