1

I have three queries:

SELECT EVENT_TITLE, ORIG_START_DT FROM EVENT_MAIN WHERE USERID='4' AND EVENT_STATUS='P'
SELECT GREET_TITLE, BROADCAST_SCH_DT FROM GREET_MAIN WHERE USERID='4' AND GREET_STATUS='P'
SELECT POLL_TITLE,CREATE_DTTM FROM POLL_MAIN WHERE USERID='4' AND POLL_STATUS='P'

How can I combine these three queries into single one to get result like this.

Tables may contain more than one row for each USERID for same status.

EVENT_TITLE | ORIG_START_DT | GREET_TITLE | BROADCAST_SCH_DT | POLL_TITLE | CREATE_DTTM
3
  • 1
    What do you mean by "combine these 3 queries"? Give an example of the output you want. Also show the structure of your tables and sample data for each.
    – dan1111
    Dec 19, 2012 at 14:09
  • Will there only be one row for each USERID with the specified "status" column in the 3 tables?
    – Ciarán
    Dec 19, 2012 at 14:25
  • there can be more than one row for each USERID in all 3 table Dec 19, 2012 at 15:11

2 Answers 2

7

Use UNION(implicit distinct) or UNION ALL like so:

SELECT 
  EVENT_TITLE "Title", 
  ORIG_START_DT "DT"
FROM EVENT_MAIN WHERE USERID='4' AND EVENT_STATUS = 'P'
UNION ALL
SELECT 
  GREET_TITLE, 
  BROADCAST_SCH_DT 
FROM GREET_MAIN WHERE USERID='4' AND GREET_STATUS = 'P'
UNION ALL
SELECT 
  POLL_TITLE,
  CREATE_DTTM 
FROM POLL_MAIN  WHERE USERID='4' AND POLL_STATUS   = 'P';

Update

If you want to get the data from these three queries in the form:

EVENT_TITLE | ORIG_START_DT | GREET_TITLE | BROADCAST_SCH_DT | POLL_TITLE | CREATE_DTTM

You can do this:

SELECT  
  EVENT_TITLE , 
  ORIG_START_DT ,
  GREET_TITLE, 
  BROADCAST_SCH_DT,
  POLL_TITLE,
  CREATE_DTTM
FROM
(
  SELECT * 
  FROM EVENT_MAIN
  WHERE USERID='4' AND EVENT_STATUS = 'P'
) t1
INNER JOIN
(
  SELECT *
  FROM GREET_MAIN
  WHERE USERID='4' AND GREET_STATUS = 'P'
) t2
INNER JOIN 
(
  SELECT * 
  FROM POLL_MAIN
  WHERE USERID='4' AND POLL_STATUS = 'P'
) t3

SQL Fiddle Demo

This will give you something like:

| EVENT_TITLE | ORIG_START_DT | GREET_TITLE | BROADCAST_SCH_DT | POLL_TITLE | CREATE_DTTM |
-------------------------------------------------------------------------------------------
|      Title4 |           O44 |        DTdd |              O44 |    ddsfTdd |         O44 |
|      Title4 |           O33 |        DTdd |              O44 |    ddsfTdd |         O44 |
........

Note that: As far as I know, this query, this way, will cross join the three tables, since I didn't specify a JOIN condition, becuase you didn't determine in your question. If there is any relation between the three tables, specify a JOIN condtion using the ON clause.

5
  • Sadly, this UNION will not return the results that the OP has asked for. He wants to pivot the results in some manner, but the question is incomplete and difficult to answer without some idea of the source data and expected result.
    – Ciarán
    Dec 19, 2012 at 14:24
  • @Ciarán - And how did you know he is looking for pivoting the three queries, since the question is incomplete and difficult to answer without some idea of the source data and expected result? And how is the pivot is used to combine three queries returning the same number of columns? Dec 19, 2012 at 14:27
  • Well if you look at the column names in the output you will see there are 6 columns, - 2 from each of the 3 source tables and not the 2 that your union returns. It's difficult to answer because there's no indication in the question of how many rows in the source tables there are. I'm sorry your answer isn't right, but please don't shoot the messenger.
    – Ciarán
    Dec 19, 2012 at 14:31
  • @Ciarán - Yes, you are right. Didn't see that output list, when I posted my answer. I will update my answer. Thanks. Dec 19, 2012 at 14:33
  • that's closer but since the OP won't give an example of the data it's impossible to say whether it's correct. It's really annoying when you get incomplete questions like this.
    – Ciarán
    Dec 19, 2012 at 16:43
1

To get the results on one line, you want to use a join:

SELECT EVENT_TITLE, ORIG_START_DT, GREET_TITLE, BROADCAST_SCH_DT, POLL_TITLE,CREATE_DTTM
FROM EVENT_MAIN em join
     GREET_MAIN gm
     on em.userid = gm.userid join
     POLL_MAIN pm
     on em.userid = pm.userid
WHERE em.EVENT_STATUS='P' and
      gm.GREET_STATUS='P' and
      pm.POLL_STATUS='P' and
      em.USERID = '4'

This also makes it easier when you want to look at more users than just 1.

2
  • This assumes that there is only one row for each USERID in each of the tables with a STATUS of P. If there isn't you will probably return the wrong results.
    – Ciarán
    Dec 19, 2012 at 14:32
  • @Ciarán . . . You are correct that there will be duplicate rows in this case. Whether this is correct or incorrect is not explained in the question. However, the specific columns are given, which is what the join returns. Dec 19, 2012 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.