> 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.
> 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