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 |
grimmus
Yak Posting Veteran
53 Posts |
Posted - 2007-03-02 : 05:11:00
|
Hi,Im trying to select data from Table2 below only when there is an exact match on ALL the columns in Table1. i.e. based on the data in Table2, hotelID 7 should be the only one selected (because it matches the 3 rows in Table1).I can do a union likeSELECT featureID,featureDetailID FROM Table1UNIONSELECT featureID,featureDetailID FROM Table2it works, but then the hotelID row isnt selected. If i include hotelID and NULL for the union then i get any hotelID matched on any 1 column which isnt what i need !Basically i need the results of the union above but with the hotelID aswellThanks for any help (sorry if table formatting is bad)Table1featureID featureDetailID1 162 84 5Table2hotelID featureID featureDetailID7 1 162 1 165 2 87 2 88 1 167 4 54 2 89 4 5 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-02 : 06:17:47
|
[code]-- Prepare sample datadeclare @table1 table (featureid int, featuredetailid int)insert @table1select 1, 16 union allselect 2, 8 union allselect 4, 5declare @table2 table (hotelid int, featureid int, featuredetailid int)insert @table2select 7, 1, 16 union allselect 2, 1, 16 union allselect 5, 2, 8 union allselect 7, 2, 8 union allselect 8, 1, 16 union allselect 7, 4, 5 union allselect 4, 2, 8 union allselect 9, 4, 5-- Show the expected resultSELECT t2.HotelIDFROM @Table2 AS t2INNER JOIN @Table1 AS t1 ON t1.FeatureID = t2.FeatureID AND t1.FeatureDetailID = t2.FeatureDetailIDGROUP BY t2.HotelIDHAVING COUNT(*) = (SELECT COUNT(*) From @Table1)ORDER BY t2.HotelID[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|