| Author |
Topic |
|
Hillman
Starting Member
2 Posts |
Posted - 2006-10-20 : 07:15:46
|
| I usually write (very simple) queries and export to Excel, then work my magic there. But there's got to be a better way.sampletableCUSTNO PARTNOABC001 A1ABC002 A1ABC002 A2ABC003 A2I need to find all CUSTNO that have both PARTNO A1 and A2.select * from sampletablewhere PARTNO in('A1','A2')returns 2 rows.I really only want a list of unique CUSTNO ... "Only 1 customer has both partno A1 & A2".Thank you,--mark |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-10-20 : 07:31:54
|
| [code]Select distinct CustNofrom Tblwhere PartNo = 'A1' and PartNo = 'A2'[/code]Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
Hillman
Starting Member
2 Posts |
Posted - 2006-10-20 : 07:48:22
|
| I appreciate the quick response, but that query returns 0 rows.I do not have update rights to the data, so I cannot add any columns or additional tables.I've also tried various combinations of JOIN and INTERSECT without success. Then again, my typical usage ends after a basic SELECT WHERE statement. |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-10-20 : 08:03:54
|
My sincere appologies!!I didn't read it quite correctly. Try this:Select CustNoFrom tbl t1where partno = 'A1'and exists(Select CustNofrom tbl t2where t2.CustNo = t1.CustNo andpartno = 'A2') Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2006-10-20 : 09:09:17
|
| SELECT custno, count(*) from(select * FROM dbo.tblWHERE partno IN ('A1,',A2')) a GROUP BY custnoHAVING count(*) = 2 |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-10-20 : 10:17:05
|
SELECT DISTINCT CUSTNOFROM sampletableWHERE PARTNO IN ('A1','A2')My bad - didn't read the question, like the subject said - <blush> |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2006-10-23 : 06:46:25
|
| snSQL...SELECT DISTINCT CUSTNO FROM sampletableWHERE PARTNO IN ('A1','A2')This won't work....because it would break the requirement "I need to find all CUSTNO that have both PARTNO A1 and A2." |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-23 : 07:08:05
|
| [code]-- Prepare test datadeclare @test table (CUSTNO varchar(6), PARTNO varchar(2))insert @testselect 'ABC001', 'A1' union allselect 'ABC002', 'A1' union allselect 'ABC002', 'A2' union allselect 'ABC003', 'A2'-- Do the workselect distinct t1.custnofrom @test t1inner join @test t2 on t2.custno = t1.custno and t2.partno = 'a2'where t1.partno = 'a1'order by t1.custno[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-23 : 07:12:26
|
quote: Originally posted by AndrewMurphy SELECT custno, count(*) from(select * FROM dbo.tblWHERE partno IN ('A1,',A2')) a GROUP BY custnoHAVING count(*) = 2
What if there are two ABC001 with PartNo A1?However, this worksSELECT custno, count(*)from @testGROUP BY custnoHAVING min(partno) = 'a1' and max(partno) = 'a2' But also gives the right result if partno is A11 for same custno. But the original posting doesn't tell if A1 and A2 are the ONLY VALID options.Peter LarssonHelsingborg, Sweden |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-10-23 : 08:37:48
|
| lots of different variations here, some work and some don't, but assuming that PK of your table is PartNo/CustNo then the easiest solution is:select CustNofrom YourTablewhere PartNo in('a1','a2')group by CustNohaving count(*) = 2 If there is no PK on that table or a different PK, then you need to add a derived table with a DISTINCT:select CustNofrom (select distinct CustNo, PartNo from YourTable) xwhere PartNo in('a1','a2')group by CustNohaving count(*) = 2 - Jeff |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-23 : 09:01:59
|
Here is a query based on my previous that gets the custno's that has ONLY 'A1' and 'A2' as partno.SELECT distinct custno, count(*)from @testGROUP BY custnoHAVING min(partno) = 'a1' and max(partno) = 'a2' and count(distinct partno) = 2 Peter LarssonHelsingborg, Sweden |
 |
|
|
samuelclay
Yak Posting Veteran
71 Posts |
Posted - 2006-10-24 : 11:41:46
|
| looks like this is answered, with several ways to go (I think Peso's first answer with the join is a good way to go, easy for a starting sql programmer to understand).. but, I was curious about the poster's comment :select * from sampletablewhere PARTNO in('A1','A2')returns 2 rows.How can this return 2 rows? It returns 4 rows from his sample data.. even if he put select distinct custno instead of select * it would return 3 rows.... curious... |
 |
|
|
|