| Author |
Topic |
|
KabirPatel
Yak Posting Veteran
54 Posts |
Posted - 2007-06-07 : 08:48:26
|
| Hi,I have a table as follows:Column A | Column B | Column C------------------------------CityA RegionA CarACityA RegionA CarBCityA RegionA CarCCityB RegionB CarDCityB RegionB CarECityB RegionB CarFCityC Null CarGCityC Null CarHCityD RegionA CarAetc....I also have a table as follows:Column B | Column C---------------------RegionA CarARegionA CarB RegionA CarCRegionA CarDetc...I am trying to output all the records from the first table where a ColumnC value is missing from the second Table e.g. In the above example I want the following record to be output CityA RegionA CarD "Removed"since CityA in the first table has a RegionA but does not have a CarD despite the fact that CarD exists in the second table fro RegionA.I have been trying all sorts of things but keep getting into a knot.Thanks in advance,Kabir |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-07 : 08:51:46
|
[code]select t1.ColumnA, t1.ColumnB, t1.ColumnC, status = 'Removed'from table1 left join table2on t1.ColumnB = t2.ColumnBand t1.ColumnC = t2.ColumnCwhere t2.ColumnB is null[/code] KH |
 |
|
|
KabirPatel
Yak Posting Veteran
54 Posts |
Posted - 2007-06-07 : 09:41:42
|
| Thanks for that.It doesn't actually work as it didnt pick up CityA, RegionA, CarD.I believe the left join didn't work as its using 2 columns for the join.Do you have any other ideas?PS. I ran the following scripts:-----------------------------------------------CREATE TABLE table1( COL1 VARCHAR(255) ,COL2 VARCHAR(255) ,COL3 VARCHAR(255))INSERT INTO table1SELECT 'CityA', 'RegionA', 'CarA'UNIONSELECT 'CityA', 'RegionA', 'CarB'UNIONSELECT 'CityA', 'RegionA', 'CarC'UNIONSELECT 'CityB', 'RegionB', 'CarD'UNIONSELECT 'CityB', 'RegionB', 'CarE'UNIONSELECT 'CityB', 'RegionB', 'CarF'UNIONSELECT 'CityC', NULL, 'CarG'UNIONSELECT 'CityC', NULL, 'CarH'UNIONSELECT 'CityD', 'RegionA', 'CarA'CREATE TABLE table2( COL2 VARCHAR(255) ,COL3 VARCHAR(255))Insert into table2select 'RegionA', 'CarA'unionselect 'RegionA', 'CarB'unionselect 'RegionA', 'CarC'unionselect 'RegionA', 'CarD'select t1.Col1, t1.Col2, t1.Col3, status = 'Removed'from table1 t1 left join table2 t2on t1.Col2 = t2.Col2and t1.Col3 = t2.Col3where t2.Col2 is null |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2007-06-07 : 10:47:25
|
The left join is the wrong way round. Also, to get the city, a derived table will be needed.-- *** Test Data ***DECLARE @t1 TABLE( City varchar(255) NOT NULL ,Region varchar(255) NULL ,Car varchar(255) NOT NULL)INSERT INTO @t1SELECT 'CityA', 'RegionA', 'CarA' UNION ALLSELECT 'CityA', 'RegionA', 'CarB' UNION ALLSELECT 'CityA', 'RegionA', 'CarC' UNION ALLSELECT 'CityB', 'RegionB', 'CarD' UNION ALLSELECT 'CityB', 'RegionB', 'CarE' UNION ALLSELECT 'CityB', 'RegionB', 'CarF' UNION ALLSELECT 'CityC', NULL, 'CarG' UNION ALLSELECT 'CityC', NULL, 'CarH' UNION ALLSELECT 'CityD', 'RegionA', 'CarA'DECLARE @t2 TABLE( Region varchar(255) NOT NULL ,Car varchar(255) NOT NULL)INSERT INTO @t2SELECT 'RegionA', 'CarA' UNION ALLSELECT 'RegionA', 'CarB' UNION ALLSELECT 'RegionA', 'CarC' UNION ALLSELECT 'RegionA', 'CarD'-- *** End Test Data ***SELECT D.City, T2.Region, T2.Car, 'Removed' AS StatusFROM @t2 T2 JOIN ( SELECT T3.Region, MIN(T3.City) AS City FROM @t1 T3 GROUP BY T3.Region ) D ON T2.Region = D.Region LEFT JOIN @t1 T1 ON T2.Region = T1.Region AND T2.Car = T1.CarWHERE T1.City IS NULL |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-07 : 10:53:32
|
trySELECT A.COL1, A.COL2, T2.COL3FROM( SELECT COL1, COL2 FROM TABLE1 GROUP BY COL1, COL2) A INNER JOIN TABLE2 T2ON A.COL2 = T2.COL2LEFT JOIN TABLE1 T1ON A.COL1 = T1.COL1AND A.COL2 = T1.COL2AND T2.COL3 = T1.COL3WHERE T1.COL3 IS NULLORDER BY A.COL1, A.COL2, T2.COL3 KH |
 |
|
|
|
|
|