Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Inno
Starting Member
33 Posts |
Posted - 2006-11-21 : 02:55:42
|
| I have tilted and need some help.I have a table with columns:ArtNo, WareNo, State + a few othersSample data:111, 1, 3111, 2, null111, 4, 10222, 1, 8222, 2, 3222, 1, 3and so onEach artNo is in several WareNo and has a stateEach WareNo has several ArtNoI want to run through the table and get * for each combination of ArtNo&WareNo that does not have any post in the table with State = 3.Best regardsInno |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-21 : 03:00:14
|
| select distinct artno, wareno from sampletablewhere state <> 3 or state is nullPeter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-21 : 03:02:59
|
| select distinct e.artno, e.warenofrom sampletable eleft join (select artno, wareno from sampletable where state = 3) q on q.artno = e.artno and q.wareno = e.warenowhere q.artno is nullselect distinct e.artno, e.warenofrom sampletable eleft join (select artno, wareno from sampletable where state = 3 or state is null) q on q.artno = e.artno and q.wareno = e.warenowhere q.artno is nullPeter LarssonHelsingborg, Sweden |
 |
|
|
Inno
Starting Member
33 Posts |
Posted - 2006-11-21 : 03:07:13
|
| I dont get it...I want every combination of ArtNo and WareNo which doesnt have the state 3 in any line. The state kan be several values which I cant specify. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-21 : 03:09:47
|
| select distinct artno, wareno from sampletablewhere state not in (3,x,y,z)???Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-21 : 03:10:34
|
| How about you post the expected output based on the sample data provided above?Peter LarssonHelsingborg, Sweden |
 |
|
|
Inno
Starting Member
33 Posts |
Posted - 2006-11-21 : 03:13:36
|
| Good idea :)The result I want from above is:111, 2, null111, 4, 8None of theese combinations of artNo and wareNo have any line with state 3 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-21 : 03:17:23
|
What's wrong with the second answer I gave you?-- prepare test datadeclare @test table (artno int, wareno int, state int)insert @testselect 111, 1, 3 union allselect 111, 2, null union allselect 111, 4, 10 union allselect 222, 1, 8 union allselect 222, 2, 3 union allselect 222, 1, 3-- do the workselect distinct e.artno, e.wareno, e.statefrom @test eleft join (select artno, wareno from @test where state = 3) q on q.artno = e.artno and q.wareno = e.warenowhere q.artno is null Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-21 : 03:25:06
|
Also this works-- do the workselect distinct e.artno, e.wareno, e.statefrom @test ewhere not exists (select * from @test where state = 3 and artno = e.artno and wareno = e.wareno) However, the suggestion with left join has slightly better performance.Peter LarssonHelsingborg, Sweden |
 |
|
|
Inno
Starting Member
33 Posts |
Posted - 2006-11-21 : 03:37:19
|
| Yah it works, Thatx alot! |
 |
|
|
|
|
|
|
|