SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple query with GROUP BY question

    Hello,

    Imagine this simples query:

    Code:
    select 1 as A, 'test1' as B, '' as C
    	
    union
    	
    select 1 as A, '' as B, 'test2' as C
    this returns :

    Code:
    ---------------------
    | A |   B   |   C   | 
    ---------------------
    | 1 | test1 |       |
    ---------------------
    | 2 |       | test2 |
    ---------------------
    But I want to get:

    Code:
    ---------------------
    | A |   B   |   C   | 
    ---------------------
    | 1 | test1 | test2 |
    ---------------------
    I tried doing something like:

    Code:
    select t.A, t.B, t.C
    from
    (
    	select 1 as A, 'test1' as B, '' as C
    	
    	union
    	
    	select 1 as A, '' as B, 'test2' as C
    ) as t
    group by t.A, t.B, t.C
    But that doesn't work.. How can I do this?

    Thanks in advance!

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT t.A, MAX(t.B), MAX(t.C)
      FROM ( SELECT 1 AS A, 'test1' AS B, '' AS C
             UNION ALL
             SELECT 1 AS A, '' AS B, 'test2' AS C
           ) AS t
    GROUP 
        BY t.A
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    Code:
    SELECT t.A, MAX(t.B), MAX(t.C)
      FROM ( SELECT 1 AS A, 'test1' AS B, '' AS C
             UNION ALL
             SELECT 1 AS A, '' AS B, 'test2' AS C
           ) AS t
    GROUP 
        BY t.A
    Thanks


Bookmarks

Posting Permissions

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