|
Related to my post in the Excel thread. This is probably really, really simple for anyone who actually knows SQL well. Me? It's just that thing that ActiveRecord hides from me. And my only experience is with MySQL, not MSSQL. But man, you stick a JOIN in there and my eyes just glaze over. In this spreadsheet we have two queries. One, amusingly, queries the other. I'm trying to figure out why. Is there any reason this: code:
code:
I've been over this in my head lots and lots of times and I can't see how a query with more criteria would somehow return more data. The only thing I can think of is that FULL OUTER JOIN in there, but that doesn't make a lot of sense (actually, the existence of the entire join doesn't make sense) because one of the criteria is for that table not to be null. The way the Excel sheet works is that it takes each query and places them into a table. Then there's a field in the first query's table that checks the second query's table for the existence of the hash, and if it's not there, it assumes there was never a response and says so. But it doesn't make a lot of sense to me - they're both querying the same table, they're both using the same restraints - the one that returns "more" data actually has more restraints... I have the feeling that if I could condense this into a single query then I could merge this one with the other query in the spreadsheet and be done with it. Also, I don't know a lot about SQL, but is this an incredibly messy query or is it just my inexperience with the language? Dessert Rose fucked around with this message at 02:21 on Dec 6, 2007 |
# ¿ Dec 6, 2007 02:14 |
|
|
# ¿ May 7, 2024 18:34 |
|
Victor posted:That SQL is revolting, especially the table breaking part. The SQL formatter in the OP sucks, IMHO. Glad to know I'm not the only one who thinks it looks like poo poo. Wouldn't an inner join not actually add any records to the result, though? My understanding of INNER JOIN is that it only pulls extra records in if there's something on both sides.
|
# ¿ Dec 6, 2007 03:04 |
|
Victor posted:Let's say I run a query on the table Thread. Then I add an inner join to Post. Will not the number of results balloon? Okay, that makes sense for a has-many association. To rephrase: Look at the two tables in question. A given install will only have one arch and only one sku; it's impossible to install the x64 and x86 versions simultaneously, for example. So, given that I'm only doing inner joins on tables that have one-to-one relationships with the initial table, is the same true? This is also a little off what my actual question is. Is there a way to construct a query such that I only get the results returned by the first query, that aren't returned by the second? I look at the queries and what I come up with is already in there (the OperationResult.NAME IS NULL bit) but all I'm really doing is stumbling in the dark. And of course I can't find the guy who wrote this to begin with to beat him with a stick. Which is understandable; I know I wouldn't want to maintain this crap.
|
# ¿ Dec 6, 2007 03:20 |
|
Victor posted:Have you verified that they are one-to-one? The only way I can see this not being the culprit is if I don't fully understand the semantics of full outer join when used like you have in your example. You can figure this out (and report back to me): select sessionid from all the tables in the first query and tell me if any are ever null. I think we might be... I don't know what the database is running but I have a feeling it would be 2005 So just so I "get" your pattern, what I would do is essentially create two derived tables, one from each query, and then do that left join on it? I d derived tables and that aspect of it makes sense to me. What does "t2.a = t1.a or t2.a is null and t1.a is null" do in the context of a join, exactly? Or would I just replace that with some entirely different code, like "t1.v_spoperationtype.sessionid = t2.v_spoperationtype.sessionid"?
|
# ¿ Dec 6, 2007 07:30 |