views:

85

answers:

7

ok so this is what I am trying to achieve is to have two SELECT statements and then join results of each so I have something like this

SELECT table.ID, tst.Value 
FROM blah AS table JOIN results AS tst ON tst.RUNID = table.RUNID
WHERE table.RUNID IN
(
  ...// nothing important but in the end i get my tst.Value  
)

second statement is almost identical

SELECT table.ID, tst2.Value 
FROM blah AS table JOIN results AS tst2 ON tst2.RUNID = table.RUNID
WHERE table.RUNID IN
(
  ...// nothing important but in the end i get my tst2.Value differently
)

I need to combine these two results in format of

SELECT table.ID, tst.Value, tst2.Value
...... // Somehow using those two statements

So anyone fluent in SQL language who could tell me how to do this or what statements should I use...Join sound like a good place to start but they use tables. I guess I could use a CREATE TABLE from SELECT and join but as I said I am not fluent in SQL so wondering if thats good idea or there is a better idea...

Thanks :)

A: 

This should work for you:

SELECT table.ID, tst.Value, tst2.Value 
FROM blah AS table 
Inner JOIN results AS tst ON tst.RUNID = table.RUNID
Inner JOIN results AS tst2 ON tst2.RUNID = table.RUNID
WHERE table.RUNID IN
(  
  ...// nothing important but in the end i get my tst2.Value differently
)
Ben Griswold
A: 

It is possible to use more than one JOIN in a query. This should solve your question.

Andrejs Cainikovs
+1  A: 

The naive approach, assuming table.ID is what relates the two value fields:

SELECT first_subquery.ID, first_subquery.Value, second_subquery.Value
FROM (
  SELECT table.ID, tst.Value 
  FROM blah AS table JOIN results AS tst ON tst.RUNID = table.RUNID
  WHERE table.RUNID IN
  (
    ...// nothing important but in the end i get my tst.Value  
  )
) first_subquery
INNER JOIN (
  SELECT table.ID, tst2.Value 
  FROM blah AS table JOIN results AS tst2 ON tst2.RUNID = table.RUNID
  WHERE table.RUNID IN
  (
    ...// nothing important but in the end i get my tst2.Value differently
  )
) second_subquery
ON first_subquery.ID = second_subquery.ID

This query is based on both of yours, joined together.

Welbog
If someone can point out why this doesn't work, I'll take away my upvote...
Austin Salonen
+1, should work fine, almost the same as mine
KM
A: 

try something like:

SELECT table.ID, tst.Value, dt.Value
    FROM blah AS table 
        JOIN results AS tst ON tst.RUNID = table.RUNID
        left outer join (
                         SELECT table.ID, tst2.Value
                             FROM blah AS table 
                                 JOIN results AS tst2 ON tst2.RUNID = table.RUNID
                             WHERE table.RUNID IN(  ...// nothing important but in the end i get my tst2.Value differently)
                        ) dt ON table.id=dt.ID
    WHERE table.RUNID IN(  ...// nothing important but in the end i get my tst.Value  )
KM
wow, 4 people get down votes at about the same time, with no reasons listed???
KM
@KM - This is apparently a dangerous question to answer. I'm thinking about deleting my answer before I get a drive-by downvote.
Ben Griswold
+1. Though naive, this approach is roughly the same as my answer. It wouldn't be very sportsmanlike of me to not upvote people who think the same way I do.
Welbog
A: 

Try this...

SELECT T1.ID, R1.Value,
       T2.RunId, R2.Value
FROM (blah AS T1 JOIN results AS R1
         ON R1.RUNID = table.RUNID
           And T1.RUNID IN ( /... / ))
    Cross Join (blah AS T2  JOIN results AS R2 
         ON R.RUNID = T2.RUNID
           And T2.RunId In (/... /))
Charles Bretana
A: 

Try this:

select a.ID, a.Value, b.Value from 
(
  SELECT table.ID, tst.Value 
  FROM blah AS table JOIN results AS tst ON tst.RUNID = table.RUNID
  WHERE table.RUNID IN
  (
    ...// nothing important but in the end i get my tst.Value  
  ) 
 ) a, 
(
  SELECT table.ID, tst2.Value 
  FROM blah AS table JOIN results AS tst2 ON tst2.RUNID = table.RUNID
  WHERE table.RUNID IN
  (
    ...// nothing important but in the end i get my tst2.Value differently
  )
 ) b
ck
+3  A: 

The easiest method (from what you've described) would be to move the IN clause into your join condition and do this all in one query. Like this:

select
    table.ID,
    tst.Value as Value1,
    txt2.Value as Value2

from blah table

left join results tst on tst.RUNID = table.RUNID and tst.RUNID in (...first conditions...)
left join results tst2 on tst.RUNID = table.RUNID and tst2.RUNID in (...second conditions...)

This structure is a little nonsensical to me (since you're filtering the values from your root table differently each time). Could you post the actual structure and SQL for us to look at? That may make it more clear.

Adam Robinson
My issue with this is the same as my issue with Tom's answer. Joining this way effectively joins `tst` and `tst2` by their `RUNIDs`, which I'm assuming are necessarily different (possibly overlapping, but different) based on the structure of the OP's original two queries. The left joins are a good way to deal with it, but the records might need to be joined on `table.ID` rather than `RUNID`. It's up to the OP to decide, though, so no downvote or upvote from me.
Welbog
I can not show actual code ... but the design of the database forces me to do it this way.thanks!
grobartn
@Welbog: The syntax here was taken from the OP's post. That IS how they were joined. I'm not sure why you would believe that they need to be joined on the ID without having any information about the poster's schema beyond what was listed.
Adam Robinson
@grobartn: If this worked for you, then great; I'd appreciate your accepting the answer :). If you need additional help, you can just provide the schema (or at least a hint as to what's going in your IN clause).
Adam Robinson