Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

Weird Requirement... Multiple Left Joins? Am I missing something? Expand / Collapse
Author
Message
Posted Tuesday, August 28, 2012 1:13 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 10:44 AM
Points: 197, Visits: 353
Laurie,

Thanks for your reply. The "Values" reserved word didn't fly in 2005. Is that a 2008+ term?

Thanks
Crusty
Post #1351201
Posted Tuesday, August 28, 2012 1:16 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 13,872, Visits: 9,596
CptCrusty1 (8/28/2012)
Laurie,

Thanks for your reply. The "Values" reserved word didn't fly in 2005. Is that a 2008+ term?

Thanks
Crusty


That's what's called a "Table Values Function", and it's 2008+. It will also only work with data that's limited to no more than 9 values per set.

The Full Outer Join version I posted will work in any version of SQL Server, at least from 7.5-on (I haven't played with anything prior to that, so can't be sure it will work there), and will work with any number of sub-values.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1351202
Posted Tuesday, August 28, 2012 1:17 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 10:44 AM
Points: 197, Visits: 353
@Laurie, Mark, GSquared. I am humbled.

I was starting to lean towards Cross Join; however, I'd never had an opportunity to use it before. Seems like it behaves a bit like a pivot table??

GSQuared, dito, never used Coalesce before. Guess I need to hit the books and learn these two techniques. The results are exactly what I needed.

I will try all three results for performance against a test version with about 100k records....

Thanks all.. .I really appreciate your help.

Sincerely.
Crusty
Post #1351205
Posted Tuesday, August 28, 2012 1:19 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 10:44 AM
Points: 197, Visits: 353
GS, that's what I thought. I've seen that in a 2008 shop; however, I'm currently in a 2005 shop that is in mid-migration, thus my afore-mentioned quandry.

Again... My Thanks.
Post #1351207
Posted Tuesday, August 28, 2012 1:20 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, June 27, 2014 8:03 AM
Points: 317, Visits: 1,079
CptCrusty1 (8/28/2012)
Laurie,

Thanks for your reply. The "Values" reserved word didn't fly in 2005. Is that a 2008+ term?

Thanks
Crusty


Hi - Sorry about that, but you've posted in the SQL 2008 area - one to watch out for next time!
Yes - it is 2008+
Post #1351208
Posted Tuesday, August 28, 2012 1:23 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, June 27, 2014 8:03 AM
Points: 317, Visits: 1,079
GSquared (8/28/2012)
CptCrusty1 (8/28/2012)
Laurie,

Thanks for your reply. The "Values" reserved word didn't fly in 2005. Is that a 2008+ term?

Thanks
Crusty


That's what's called a "Table Values Function", and it's 2008+. It will also only work with data that's limited to no more than 9 values per set.



Just for the record, this example is limited to 9, but you can code the numbers table up to any number.
Post #1351210
Posted Tuesday, August 28, 2012 1:28 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 10:44 AM
Points: 197, Visits: 353
I'm going to remember you guys (and gals) and ChristmaHoniQuanzika.....

Post #1351212
Posted Tuesday, August 28, 2012 1:47 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 13,872, Visits: 9,596
laurie-789651 (8/28/2012)
GSquared (8/28/2012)
CptCrusty1 (8/28/2012)
Laurie,

Thanks for your reply. The "Values" reserved word didn't fly in 2005. Is that a 2008+ term?

Thanks
Crusty


That's what's called a "Table Values Function", and it's 2008+. It will also only work with data that's limited to no more than 9 values per set.



Just for the record, this example is limited to 9, but you can code the numbers table up to any number.


Yes, but it will still have a finite limit, and is an unnecessary limit on the query. This kind of situation is exactly what Full Outer Join is meant for, so working around it with hard-coded things like that is unnecessary.

The numbers table version you came up with isn't a bad solution, it's just unnecessarily complex for the desired end result. That's all. No big deal. And a hard-coded numbers table with a few thousand rows would be backwards compatible, and work for any conceivable data complexity needed by this code. So it's limits aren't that big a deal. Just an Occam fan here.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1351225
Posted Tuesday, August 28, 2012 1:48 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 13,872, Visits: 9,596
CptCrusty1 (8/28/2012)
@Laurie, Mark, GSquared. I am humbled.

I was starting to lean towards Cross Join; however, I'd never had an opportunity to use it before. Seems like it behaves a bit like a pivot table??

GSQuared, dito, never used Coalesce before. Guess I need to hit the books and learn these two techniques. The results are exactly what I needed.

I will try all three results for performance against a test version with about 100k records....

Thanks all.. .I really appreciate your help.

Sincerely.
Crusty


Cross Join just joins every row in one table (or dataset) to every row in another table (or dataset). Produces what's called a "Cartesian Product".

Coalesce is just IsNull's big brother. All it does is pick the first non-null value in a list. If they're all null, it returns null, otherwise, it gets the first one. Since, in an outer join (Full, Left, or Right), one or more of the columns may be null, I used that to make sure it would get something in that column.

The key to my version is Full Outer Join. What that does is get all rows from both sides of the join, whether they have a matching row in the other side or not.

So, a Full Outer from GR to GE gets all rows in GR, even if GE doesn't have a matching row. And it gets all rows from GE, even if GR doesn't have a matching row. So if GR had 5 and GE 2, it would still get all rows in both. Then it does the same thing with Ref, getting all rows, even if they don't have a match in the other two. If anything does match, it puts them in the same row, but if it doesn't match, it still pulls it.

This kind of problem is exactly what Full Outer Join is designed to handle. It's one of those features of SQL that seems to be less well-known. But, issues with how Joins work is one of the things that gives new database devs major headaches, so it's not too surprising.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1351227
Posted Tuesday, August 28, 2012 2:17 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 10:44 AM
Points: 197, Visits: 353
GS, I'm going to move this out of the Forum... I could stand to chat with you... LOL... I'll PM you.
Post #1351247
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse