Link to home
Start Free TrialLog in
Avatar of arkam chou
arkam chou

asked on

SQL Two column as ones

Dear Expert,

Can I select two column from difference table as two column like attachment?
sql.xlsx
Avatar of ste5an
ste5an
Flag of Germany image

It's called UNION:

SELECT  DateA AS DateIssue ,
        Name
FROM    tblA
UNION ALL
SELECT  DateB ,
        Name
FROM    tblB;

Open in new window

SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice picture.  btw Next time do us a favor and capture a screenshot of your diagram, and use the Image button to post it.  That way we can see your picture and question in one view without having to click back and forth.  Thanks in advance. User generated image
Avatar of arkam chou
arkam chou

ASKER

so it can by using "UNION ALL". How about custom column like attachment?
User generated imagesql.xlsx
Give this a whirl..
SELECT DateIssue, Name, number, price
FROM (
   SELECT DateA as DateIssue, Name, number, NULL as price
   FROM tblA
   UNION ALL
   SELECT DateB, Name, NULL, price
   FROM tblB) 
WHERE number=10 OR price=200

Open in new window

When using large sets, I would use:

 SELECT DateA AS DateIssue ,
        Name ,
        number ,
        NULL AS price
 FROM   tblA
 WHERE  number = 10
 UNION ALL
 SELECT DateB ,
        Name ,
        NULL ,
        price
 FROM   tblB
 WHERE  price = 200;

Open in new window


Cause imho there is no predicate push-down in this case.
Hello All,

@Jim Horn: I saw in MSSQL said that "incorrect syntax. when put mouse hover on "DateIssue", it said that "invalide column name. what should to do?

@ste5an: I need to put some condition after select.
ex: order by name
Just use a sub-query as Jim did.
Dear Expert,

Below are the error when I process it Microsoft SQL

User generated imageUser generated imageUser generated imageUser generated image
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes, it work but I have problem with "Null" display in column "price" and "number". How can I use "group by" and "sum" on this query?
SELECT  DateIssue ,
        Name ,
        sum(number) ,
        sum(price)
FROM    ( SELECT    DateA AS DateIssue ,
                    Name ,
                    number ,
                    NULL AS price
          FROM      tblA
          UNION ALL
          SELECT    DateB ,
                    Name ,
                    NULL ,
                    price
          FROM      tblB
        ) Q
group by dateissue, name
WHERE   number = 10
        OR price = 200;

Open in new window

GROUP BY must be placed after the WHERE clause.
yes, it work. one more thing how to change from "null" value to "0" value?
ok. now it work with "ISNULL(null, 0)".
Thanks you all for you support.