Author |
Topic |
leosuth
Starting Member
5 Posts |
Posted - 2008-02-01 : 11:28:41
|
I have two tables with different numbers of rows so different numbers of values with a single field of interest. Values that can appear anywhere in the Values field in either table are<lpn> (these are actually freight Licence Plate Numbers of the form "JD0002267647000213")<blank>NULL"N/A"any of these can be duplicated ie. appear in both tables - although there are any number of <blank>,NULL, and "N/A" in each table, <lpn> are unique within each table. What I need is code to produce a list of unique values that exist in the two tables - ie. I want to end up with something likeNULL<blank>N/AJD0002267647nnnnn1JD0002267647nnnnn2JD0002267647nnnnn3JD0002267647nnnnn4JD0002267647nnnnn5JD0002267647nnnnn6etcI don't want to involve temporary tables becuase the result of the query is to be a dataset to my VB.net applicationGood one,eh? |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-01 : 15:36:49
|
SELECT Col1 FROM Table1UNIONSELECT Col1 FROM Table2 E 12°55'05.25"N 56°04'39.16" |
 |
|
leosuth
Starting Member
5 Posts |
Posted - 2008-02-03 : 09:28:45
|
Hi ThanksYes I know that bit - but that will return duplicates - if JD0002267647000213 appears in both tables then the Union will returnJD0002267647000213JD0002267647000213What I want is to return a Unique value , ie so JD0002267647000213 appears only once - I just don't know how to apply the DISTINCT and/or GROUP By to the UNION to get the result.I could do it as a two stage opereation using a temp table, but I'm sure it can be done in a single query. This Query also has to exlude the <blank>, NULL and "N/A" values as well, so my list will be only of unique Licence Plate NumbersBut thx for your help anyway |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-03 : 10:30:07
|
I think you need to read Books Online about the difference between UNION and UNION ALL.If I had suggested UNION ALL, you would have get duplicates. The UNION operator takes care of that for you.You will get NO DUPLICATES with the use of UNION. E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-03 : 10:31:56
|
[code]SELECT Col1 FROM Table1 WHERE Col1 > '' AND Col1 <> 'N/A'UNIONSELECT Col1 FROM Table2 WHERE Col1 > '' AND Col1 <> 'N/A'[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-03 : 10:33:04
|
If all license plate numbers starts with 'J', then you can useSELECT Col1 FROM Table1 WHERE Col1 > '' AND Col1 < 'N/A'UNIONSELECT Col1 FROM Table2 WHERE Col1 > '' AND Col1 < 'N/A' This will automatically elimitate the NULL values for you. E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-03 : 10:39:41
|
To demonstrate the validity of my statements, try this-- Prepare sample dataDECLARE @Table1 TABLE (Plate VARCHAR(20))INSERT @Table1SELECT NULL UNION ALLSELECT '' UNION ALLSELECT 'N/A' UNION ALLSELECT 'JD0002267647nnnnn1' UNION ALLSELECT 'JD0002267647nnnnn2' UNION ALLSELECT 'JD0002267647nnnnn3' UNION ALLSELECT 'JD0002267647nnnnn4'DECLARE @Table2 TABLE (Plate VARCHAR(20))INSERT @Table2SELECT NULL UNION ALLSELECT '' UNION ALLSELECT 'N/A' UNION ALLSELECT 'JD0002267647nnnnn3' UNION ALLSELECT 'JD0002267647nnnnn4' UNION ALLSELECT 'JD0002267647nnnnn5' UNION ALLSELECT 'JD0002267647nnnnn6'-- Show the expexted outputSELECT Plate FROM @Table1 WHERE Plate > '' AND Plate < 'N/A'UNIONSELECT Plate FROM @Table2 WHERE Plate > '' AND Plate < 'N/A' And this is the output from the sample data abovePlateJD0002267647nnnnn1JD0002267647nnnnn2JD0002267647nnnnn3JD0002267647nnnnn4JD0002267647nnnnn5JD0002267647nnnnn6 E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-03 : 10:40:20
|
quote: Originally posted by leosuth But thx for your help anyway
You're welcome.It's a comfort to know you are that sure my suggestion was wrong. Good luck with your studies.Feel free to ask a new question anytime. E 12°55'05.25"N 56°04'39.16" |
 |
|
leosuth
Starting Member
5 Posts |
Posted - 2008-02-04 : 06:20:27
|
quote: It's a comfort to know you are that sure my suggestion was wrong. Good luck with your studies.
Really no need for sarcasm - I was not at all sure I was right and you were wrong. I'd just forgotten about UNION ALL, is all However, I've proven it to myself, thanks for helping me - have to say DOH! here I should really have known the answer myself |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-04 : 06:58:32
|
I was objecting to quote: but that will return duplicates
since that indicated that1) You didn't care to even test the suggestion2) You had made your mind up that the suggestion would not work3) You did test but did not copy and paste exact code as given in the suggestion.It is good you tested yourself and found the answer. E 12°55'05.25"N 56°04'39.16" |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-04 : 07:25:05
|
quote: Originally posted by leosuth Hi ThanksYes I know that bit - but that will return duplicates - if JD0002267647000213 appears in both tables then the Union will returnJD0002267647000213JD0002267647000213What I want is to return a Unique value , ie so JD0002267647000213 appears only once - I just don't know how to apply the DISTINCT and/or GROUP By to the UNION to get the result.I could do it as a two stage opereation using a temp table, but I'm sure it can be done in a single query. This Query also has to exlude the <blank>, NULL and "N/A" values as well, so my list will be only of unique Licence Plate NumbersBut thx for your help anyway
select 'JD0002267647000213' union select 'JD0002267647000213' MadhivananFailing to plan is Planning to fail |
 |
|
|