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 |
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2008-09-09 : 11:45:28
|
Here's the STATIC tableReference Location Product200413130 D zzz200413130 D aaa200413130 A zzz200413130 A aaa200519800 F aaa200519800 F zzz200519800 E zzz200519800 E aaaHere's the Dynamic table200413130 A200413130 A200519800 F200413130 A200413130 A200519800 F200519800 F200413130 A200413130 KI only want to match Dynamics aginst the "zzz" Products in STATIC.At the same time, I want to identify NON-Matches (like the new "K" entry in Dynamics. However, no matter what I do, I endup with a doubling of data:SELECT Dynamic.[Reference], Count(Dynamic.[Reference]) AS [CountOfReference], [Static].[Location]FROM [Static] LEFT JOIN Dynamic ON [Static].[Reference] = Dynamic.[Reference]GROUP BY Dynamic.[Reference], [Static].[Location];Result: Loc CountOfLocation Reference ProductA 10 200413130 zzzF 6 200519800 zzzK 2 200413130 zzzMy query found the new "K" location record using the LEFT JOIN, but it tells me there are 2, when there's only one. It's doubling even the actual matched count. How can I get this result instead?Loc CountOfLocation Reference ProductA 5 200413130 zzzF 3 200519800 zzzK 1 200413130 zzzThanks for your efforts!Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-09 : 12:24:27
|
[code]DECLARE @Static TABLE ( Reference INT, Location CHAR(1), Product CHAR(3) )INSERT @StaticSELECT 200413130, 'D', 'zzz' UNION ALLSELECT 200413130, 'D', 'aaa' UNION ALLSELECT 200413130, 'A', 'zzz' UNION ALLSELECT 200413130, 'A', 'aaa' UNION ALLSELECT 200519800, 'F', 'aaa' UNION ALLSELECT 200519800, 'F', 'zzz' UNION ALLSELECT 200519800, 'E', 'zzz' UNION ALLSELECT 200519800, 'E', 'aaa'DECLARE @Dynamic TABLE ( Reference INT, Location CHAR(1) )INSERT @DynamicSELECT 200413130, 'A' UNION ALLSELECT 200413130, 'A' UNION ALLSELECT 200519800, 'F' UNION ALLSELECT 200413130, 'A' UNION ALLSELECT 200413130, 'A' UNION ALLSELECT 200519800, 'F' UNION ALLSELECT 200519800, 'F' UNION ALLSELECT 200413130, 'A' UNION ALLSELECT 200413130, 'K'DECLARE @Product CHAR(3)SET @Product = 'zzz'SELECT d.Location, COUNT(*) AS CountOfLocation, d.Reference, COALESCE(s.Product, @Product) AS ProductFROM @Dynamic AS dLEFT JOIN @Static AS s ON s.Reference = d.Reference AND s.Location = d.Location AND s.Product = @ProductGROUP BY d.Location, d.Reference, s.Product[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-24 : 15:43:04
|
Was the suggestion helpful? E 12°55'05.63"N 56°04'39.26" |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-25 : 01:24:33
|
[code]SELECT d.Reference,d.Location,s.Product,CASE WHEN s.Product IS NULL THEN 'Non-Match' ELSE 'Match' ENDFROM (SELECT DISTINCT Reference,Location FROM Dynamic) dLEFT JOIN STATIC sON s.Reference=d.ReferenceAND s.Location=d.LocationAND Product='zzz'[/code] |
 |
|
|
|
|
|
|