Author |
Topic |
ch9862
Yak Posting Veteran
76 Posts |
Posted - 2007-02-15 : 17:56:26
|
I have two tables:declare @a table (id int, sth char(3))insert @aselect 1, 'ABC' union allselect 2, 'ADG' union allselect 3, 'AFJ' union allselect 4, 'QET' union allselect 5, 'QRU'declare @b table (id int, nme varchar(6), dt datetime, cde char(1))insert @bselect 1, 'AV209', '10/11/2004', 'P' union allselect 2, 'CI101', '03/28/2001', 'P' union allselect 2, 'GH331', '08/01/2004', 'R' union allselect 3, 'AT202', '10/09/2006', 'V' They can be joined on id (inner join). I need to retrieve the following fields:select a.sth, b.nme, (b.dt where cde='P') as dteA, (b.dt where cde='R') as dteBfrom @a as ainner join @b as b on a.id=b.id The dteA and dteB are dates from records with the same id as the one retrieved, where cde is as indicated. So I thought I need to self-join table b two times (as c and d for example, narrowing the selection with WHERE), but MSSQL is complaining whatever I try. How do I do it?I should add that some ids may not have both of these records (cde='P' and cde='R') - in which case I need to place NULLs in the output field(s). |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-15 : 18:27:21
|
Without your supplied output based on the sample data you have provided, all we can give are educated guesses.declare @a table (id int, sth char(3))insert @aselect 1, 'ABC' union allselect 2, 'ADG' union allselect 3, 'AFJ' union allselect 4, 'QET' union allselect 5, 'QRU'declare @b table (id int, nme varchar(6), dt datetime, cde char(1))insert @bselect 1, 'AV209', '10/11/2004', 'P' union allselect 2, 'CI101', '03/28/2001', 'P' union allselect 2, 'GH331', '08/01/2004', 'R' union allselect 3, 'AT202', '10/09/2006', 'V'select a.sth, b.nme, case when cde = 'P' then b.dt end as dteA, case when cde = 'R' then b.dt end as dteBfrom @a as ainner join @b as b on b.id = a.id Peter LarssonHelsingborg, Sweden |
 |
|
ch9862
Yak Posting Veteran
76 Posts |
Posted - 2007-02-16 : 08:12:00
|
quote: Originally posted by Peso Without your supplied output based on the sample data you have provided, all we can give are educated guesses.
Sorry for ambiguous question. What I'm after is result like:sth nme dteA dteBABC AV209 10/11/2004 NULLADG CI101 03/28/2001 08/01/2004ADG GH331 03/28/2001 08/01/2004AFJ AT202 NULL NULL For a particular id, the first two fields are what they are, and the others are b.dt where b.cde='P' (dteA) or b.cde='R' (dteB) - for this id. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-16 : 08:33:08
|
[code]-- prepare sample datadeclare @a table (id int, sth char(3))insert @aselect 1, 'ABC' union allselect 2, 'ADG' union allselect 3, 'AFJ' union allselect 4, 'QET' union allselect 5, 'QRU'declare @b table (id int, nme varchar(6), dt datetime, cde char(1))insert @bselect 1, 'AV209', '10/11/2004', 'P' union allselect 2, 'CI101', '03/28/2001', 'P' union allselect 2, 'GH331', '08/01/2004', 'R' union allselect 3, 'AT202', '10/09/2006', 'V'-- show the outputselect q.sth, q.nme, d.dteA, d.dteBfrom ( select id, max(case when cde = 'p' then dt end) as dteA, max(case when cde = 'r' then dt end) as dteB from @b group by id ) as dinner join ( select a.sth, a.id, b.nme from @a as a inner join @b as b on b.id = a.id ) as q on q.id = d.id[/code]Peter LarssonHelsingborg, Sweden |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-16 : 08:35:30
|
[code]select a.sth, b.nme, c.dteA, c.dteBfrom @a a inner join @b b on a.id = b.id inner join ( select id, dteA = max(case when cde = 'P' then dt end), dteB = max(case when cde = 'R' then dt end) from @b group by id ) c on a.id = c.idorder by a.sth[/code] KH |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-16 : 08:36:39
|
too slow KH |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-16 : 08:39:51
|
But neater!Peter LarssonHelsingborg, Sweden |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-16 : 08:50:14
|
quote: Originally posted by Peso But neater!Peter LarssonHelsingborg, Sweden
nah. It's actually the same solution  KH |
 |
|
|
|
|