Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / April 2008

Tip: Looking for answers? Try searching our database.

How do I turn 2 separate select queries into the columns of 1 select     query

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
barrybevel@gmail.com - 27 Mar 2008 17:31 GMT
Hi,
 I'm having a really hard time trying to figure this out so any help
would be great.
I'm working on DB2 V9.5 but I think my problem is generic sql.

I have 2 independent sql queries which are very similar with just a
different value in the where clause:

SELECT weekscum as fabbed
FROM mytable
WHERE mytable.ccin_id = 1 AND mytable.plant_id = 1 AND mytable.mytype
= 8

SELECT weekscum as planned
FROM mytable
WHERE mytable.ccin_id = 1 AND mytable.plant_id = 1 AND mytable.mytype
= 15

Both queries return 1 column of data, (13 rows long)

How do I create an SQL query that will have the output of each of
these queries as a column
e.g. 2 columns one called fabbed and the other planned

I have tried the following query but I get 13*13 rows instead of just
13 rows!

SELECT t1.planned, t2.fabbed FROM
(
SELECT weekscum as planned, mytable.ccin_id, mytable.plant_id
FROM mytable
WHERE mytable.ccin_id = 1 AND mytable.plant_id = 1 AND
mytable.mytype_id = 15
) AS t1
JOIN
(
SELECT weekscum as fabbed, mytable.ccin_id, mytable.plant_id
FROM mytable
WHERE mytable.ccin_id = 1 AND mytable.plant_id = 1 AND
mytable.mytype_id = 8
) AS t2
ON t2.ccin_id = t1.ccin_id AND t2.plant_id = t1.plant_id

Thanks in advance to anyone who can help me.
Lennart - 27 Mar 2008 20:30 GMT
On Mar 27, 5:31 pm, barrybe...@gmail.com wrote:
> Hi,
>   I'm having a really hard time trying to figure this out so any help
[quoted text clipped - 40 lines]
>
> Thanks in advance to anyone who can help me.

What is the relationship between "weekscum as fabbed" and  "weekscum
as planned"? If there is nothing that restricts your query further you
will be performing an "crossjoin", i.e. get 13x13 rows. BTW, what is
the primary key of mytable?

/Lennart
barrybevel@gmail.com - 28 Mar 2008 00:24 GMT
> On Mar 27, 5:31 pm, barrybe...@gmail.com wrote:
>
[quoted text clipped - 49 lines]
>
> /Lennart

First thanks for your reply Lennart.

weekscum is fabbed if mytype_id = 8

weekscum is planned if mytype_id = 15

id is the primiary key

I want to select weekscum from gapp twice,

once when mytype is 8 (fabbed) and again when mytype is 15 (planned)

Cheers.
barrybevel@gmail.com - 28 Mar 2008 00:27 GMT
> On Mar 27, 5:31 pm, barrybe...@gmail.com wrote:
>
[quoted text clipped - 49 lines]
>
> /Lennart

First thanks for your reply Lennart.

weekscum is fabbed if mytype_id = 8

weekscum is planned if mytype_id = 15

id is the primiary key

I want to select weekscum from mytable twice,

once when mytype is 8 (fabbed) and again when mytype is 15 (planned)

Cheers.
Lennart - 28 Mar 2008 11:17 GMT
On Mar 28, 12:27 am, barrybe...@gmail.com wrote:

> > On Mar 27, 5:31 pm, barrybe...@gmail.com wrote:
>
[quoted text clipped - 55 lines]
>
> weekscum is planned if mytype_id = 15

Yes, that can I tell from your query, but they must be somehow related
in another way. Otherwise I don't see how you can tell which fabbed
that belongs to which planned. Can you post the result from:

SELECT id, weekscum as planned
FROM mytable
WHERE mytable.ccin_id = 1 AND mytable.plant_id = 1 AND
mytable.mytype_id = 15

and from

SELECT id, weekscum as fabbed
FROM mytable
WHERE mytable.ccin_id = 1 AND mytable.plant_id = 1 AND
mytable.mytype_id = 8

/Lennart
Tonkuma - 28 Mar 2008 11:32 GMT
How about this?
If it is guaranteed that number of rows returned from two queries are
always same, you can use INNER JOIN instead of FULL OUTER JOIN.
[code]
SELECT planned, fabbed
 FROM
      (SELECT weekscum  AS fabbed
            , ROWNUMBER() OVER() AS rn
         FROM mytable
        WHERE ccin_id  = 1
          AND plant_id = 1
          AND mytype   = 8
      ) T1
      FULL OUTER JOIN
      (SELECT weekscum  AS planned
            , ROWNUMBER() OVER() AS rn
         FROM mytable
        WHERE ccin_id  = 1
          AND plant_id = 1
          AND mytype   = 15
     ) T2
       ON t1.rn = t2.rn
[/code]
Tonkuma - 28 Mar 2008 11:40 GMT
If you have some columns to relate a planned row with a fabbed row,
you can use them for join condition.
Otherwise, how about this?
(You can use INNER JOIN instead of FULL OUTER JOIN, if it is
guaranteed that number of rows returned from two queries are always
same.)
SELECT planned, fabbed
 FROM
      (SELECT weekscum  AS fabbed
            , ROWNUMBER() OVER() AS rn
         FROM mytable
        WHERE ccin_id  = 1
          AND plant_id = 1
          AND mytype   = 8
      ) T1
      FULL OUTER JOIN
      (SELECT weekscum  AS planned
            , ROWNUMBER() OVER() AS rn
         FROM mytable
        WHERE ccin_id  = 1
          AND plant_id = 1
          AND mytype   = 15
      ) T2
        ON t1.rn = t2.rn
barrybevel@gmail.com - 28 Mar 2008 13:05 GMT
> If you have some columns to relate a planned row with a fabbed row,
> you can use them for join condition.
[quoted text clipped - 20 lines]
>        ) T2
>          ON t1.rn = t2.rn

Hey Tonkuma,
 Your query works PERFECTLY!!!!  Thanks a million. I would never have
figured it out!
ChrisC - 23 Apr 2008 23:18 GMT
> If you have some columns to relate a planned row with a fabbed row,
> you can use them for join condition.

If you have that column to join on, you can ignore the join
altogether.  For instance, if you have a WEEK column that you are
reporting by, you could use:

SELECT WEEK, SUM(case when mytype = 8 then weekscum end) as fabbed,
SUM(case when mytype = 15 then weekscum end) as planned
FROM mytable
WHERE ccin_id = 1
 AND plant_id = 1
GROUP BY WEEK
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.