mattibutt
asked on
Combining two Joins in one statement
problem scenario
i have stored procedure which select unique videoitem for a specify timeline, it also make sure that game is unique as well but now i have scenario where one videoitem is about two games so i need to still preserves the previous rule and apply it to the new situation i have created another field in the videoitems table which is called gameidtwo and assigned 2nd game of videoitem so if either game is already selected it shouldn't return as previously it worked for one game
i have done everything possible but it just doesn't work for the 2nd solution i am thinking maybe i need to change the approach and i am not sure what other approach i can take
thankyou
i have stored procedure which select unique videoitem for a specify timeline, it also make sure that game is unique as well but now i have scenario where one videoitem is about two games so i need to still preserves the previous rule and apply it to the new situation i have created another field in the videoitems table which is called gameidtwo and assigned 2nd game of videoitem so if either game is already selected it shouldn't return as previously it worked for one game
i have done everything possible but it just doesn't work for the 2nd solution i am thinking maybe i need to change the approach and i am not sure what other approach i can take
thankyou
solution works
LEFT JOIN (Select VI1.GameId
from #SlotVideoItems S1
Join dbo.VideoItems VI1
ON S1.[ScheduledSlotId] = @ScheduledSlotId
AND S1.[VideoItemID] = VI1.[VideoItemID]
Group by VI1.GameId) GM
ON VI.GameID = GM.GameID
AND GM.GameID is null
solution doesn't work
LEFT JOIN (Select VI5.GameIdtwo -- get the games already selected
from #SlotVideoItems S5
Join dbo.VideoItems VI5
ON S5.[ScheduledSlotId] = @ScheduledSlotId
AND S5.[VideoItemID] = VI5.[VideoItemID]
Group by VI5.GameIdtwo)SV
ON VI.GameIDtwo = SV.GameIDtwo
AND SV.GameID is null
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
experts : please be aware that this could be a triplicate
1) https://www.experts-exchange.com/questions/23918190/very-complex-left-join-problem.html
2) https://www.experts-exchange.com/questions/23915121/MS-SQL-Join.html
3) https://www.experts-exchange.com/questions/23924830/Combining-two-Joins-in-one-statement.html (this one)
mattibutt : could you please clarify ?
1) https://www.experts-exchange.com/questions/23918190/very-complex-left-join-problem.html
2) https://www.experts-exchange.com/questions/23915121/MS-SQL-Join.html
3) https://www.experts-exchange.com/questions/23924830/Combining-two-Joins-in-one-statement.html (this one)
mattibutt : could you please clarify ?
ASKER
hi tedbilly
i have read your thoughts thankyou, i am trying to understand what you sent me i am gonna write my response to see whether i understood you clearly
i have tried this query as well yesterday but it didnt produce any result either
AND NOT EXISTS( SELECT NULL FROM VideoItems VI
-- WHERE SV.GameIDtwo = VI.[VideoItemID]
now i think what i understood by your suggestion is that i run your queries which make sure that gameid and gameidtwo are not already selected?
so i will not use the left join clauses for both GM and SV ? i take them out?
thanks
i have read your thoughts thankyou, i am trying to understand what you sent me i am gonna write my response to see whether i understood you clearly
i have tried this query as well yesterday but it didnt produce any result either
AND NOT EXISTS( SELECT NULL FROM VideoItems VI
-- WHERE SV.GameIDtwo = VI.[VideoItemID]
now i think what i understood by your suggestion is that i run your queries which make sure that gameid and gameidtwo are not already selected?
so i will not use the left join clauses for both GM and SV ? i take them out?
thanks
--(I Keep Both of these queries)?
LEFT JOIN (Select VI1.GameId
from #SlotVideoItems S1
Join dbo.VideoItems VI1
ON S1.[ScheduledSlotId] = @ScheduledSlotId
AND S1.[VideoItemID] = VI1.[VideoItemID]
Group by VI1.GameId) GM
ON VI.GameID = GM.GameID
AND GM.GameID is null
LEFT JOIN (Select VI5.GameIdtwo -- get the games already selected
from #SlotVideoItems S5
Join dbo.VideoItems VI5
ON S5.[ScheduledSlotId] = @ScheduledSlotId
AND S5.[VideoItemID] = VI5.[VideoItemID]
Group by VI5.GameIdtwo)SV
ON VI.GameIDtwo = SV.GameIDtwo
AND SV.GameID is null
-- then i add these queries as well?
WHERE
VI.GameID NOT IN (Select VI1.GameId
from #SlotVideoItems S1
Join dbo.VideoItems VI1
ON S1.[ScheduledSlotId] = @ScheduledSlotId
AND S1.[VideoItemID] = VI1.[VideoItemID]
Group by VI1.GameId)
WHERE
VI.GameIDtwo NOT IN (Select VI5.GameIdtwo
from #SlotVideoItems S5
Join dbo.VideoItems VI5
ON S5.[ScheduledSlotId] = @ScheduledSlotId
AND S5.[VideoItemID] = VI5.[VideoItemID]
Group by VI5.GameIdtwo)
ASKER
hi
i think my previous post maybe a bit confusing are you suggesting that i only use the following codes?
VI.GameID NOT IN (Select VI1.GameId
from #SlotVideoItems S1
Join dbo.VideoItems VI1
ON S1.[ScheduledSlotId] = @ScheduledSlotId
AND S1.[VideoItemID] = VI1.[VideoItemID]
Group by VI1.GameId)
WHERE
VI.GameIDtwo NOT IN (Select VI5.GameIdtwo
from #SlotVideoItems S5
Join dbo.VideoItems VI5
ON S5.[ScheduledSlotId] = @ScheduledSlotId
AND S5.[VideoItemID] = VI5.[VideoItemID]
Group by VI5.GameIdtwo)
i think my previous post maybe a bit confusing are you suggesting that i only use the following codes?
VI.GameID NOT IN (Select VI1.GameId
from #SlotVideoItems S1
Join dbo.VideoItems VI1
ON S1.[ScheduledSlotId] = @ScheduledSlotId
AND S1.[VideoItemID] = VI1.[VideoItemID]
Group by VI1.GameId)
WHERE
VI.GameIDtwo NOT IN (Select VI5.GameIdtwo
from #SlotVideoItems S5
Join dbo.VideoItems VI5
ON S5.[ScheduledSlotId] = @ScheduledSlotId
AND S5.[VideoItemID] = VI5.[VideoItemID]
Group by VI5.GameIdtwo)
I'm not clear what you are trying to achieve. I'm sorry but your requirements in the question aren't clear.
From you're code it looked like you wanted to find items that are in one list but not the other. However, it now seems like you want to find matches or possible duplicates? If yes, then we need another query that would be far simpler.
From you're code it looked like you wanted to find items that are in one list but not the other. However, it now seems like you want to find matches or possible duplicates? If yes, then we need another query that would be far simpler.
ASKER
hi Tedbilly
sorry if i confused you, i am only looking for unique values , let me write below
gameid gameidtwo
g1 g5
g2 g4
g3
g4
g5
expected output
g1 + g2 + g3
g4 and g5 shouldnt come because they are already selected either at gameid or gameidtwo stage
i havent tested your codes yet i will do it tomorrow
what my assumption is that when i run the below it will only select unique values ? if it does that then this is what i want to acheive.
VI.GameID NOT IN (Select VI1.GameId
from #SlotVideoItems S1
Join dbo.VideoItems VI1
ON S1.[ScheduledSlotId] = @ScheduledSlotId
AND S1.[VideoItemID] = VI1.[VideoItemID]
Group by VI1.GameId)
WHERE
VI.GameIDtwo NOT IN (Select VI5.GameIdtwo
from #SlotVideoItems S5
Join dbo.VideoItems VI5
ON S5.[ScheduledSlotId] = @ScheduledSlotId
AND S5.[VideoItemID] = VI5.[VideoItemID]
Group by VI5.GameIdtwo)
sorry if i confused you, i am only looking for unique values , let me write below
gameid gameidtwo
g1 g5
g2 g4
g3
g4
g5
expected output
g1 + g2 + g3
g4 and g5 shouldnt come because they are already selected either at gameid or gameidtwo stage
i havent tested your codes yet i will do it tomorrow
what my assumption is that when i run the below it will only select unique values ? if it does that then this is what i want to acheive.
VI.GameID NOT IN (Select VI1.GameId
from #SlotVideoItems S1
Join dbo.VideoItems VI1
ON S1.[ScheduledSlotId] = @ScheduledSlotId
AND S1.[VideoItemID] = VI1.[VideoItemID]
Group by VI1.GameId)
WHERE
VI.GameIDtwo NOT IN (Select VI5.GameIdtwo
from #SlotVideoItems S5
Join dbo.VideoItems VI5
ON S5.[ScheduledSlotId] = @ScheduledSlotId
AND S5.[VideoItemID] = VI5.[VideoItemID]
Group by VI5.GameIdtwo)
ASKER
hi Tedbilly
i have tried to run your query it actually doesnt even let me select one videoitem, i have tried to run it with and without the existing joins but it doesn't work at all
i have tried to run your query it actually doesnt even let me select one videoitem, i have tried to run it with and without the existing joins but it doesn't work at all
And VI.GameID NOT IN (Select VI1.GameId
from #SlotVideoItems S1
Join dbo.VideoItems VI1
ON S1.[ScheduledSlotId] = @ScheduledSlotId
AND S1.[VideoItemID] = VI1.[VideoItemID]
Group by VI1.GameId)
And VI.GameIDtwo NOT IN (Select VI5.GameIdtwo
from #SlotVideoItems S5
Join dbo.VideoItems VI5
ON S5.[ScheduledSlotId] = @ScheduledSlotId
AND S5.[VideoItemID] = VI5.[VideoItemID]
Group by VI5.GameIdtwo)
ASKER
i have run your query in isolation it doesnt actually select anything at all however if i run without "NOT" then it returns the results.
ASKER
i found another approach by using the union but thanks for your help
Hmm I appreciate the points but based on reading your last response a union should be used. If you had given me the schema for the table(s) I could have answered it correctly.
Hi tedbilly, that is exactly what mattibutt ended up doing : https://www.experts-exchange.com/questions/23918190/very-complex-left-join-problem.html?cid=238&anchorAnswerId=23029738#a23029738
Quick question for you - have you been plagued with some ee mail problems ? seems my notifications are running between 12 and 24 hours late ...
Quick question for you - have you been plagued with some ee mail problems ? seems my notifications are running between 12 and 24 hours late ...
Oops, actually I typed that last comment wrong. I don't think he need a union at all. I have a feeling that whole query could have be reduced into a NOT IN statement but without the schema it was hard to read. Oh well.
Hmm you are correct. There is some big time gaps. I just received an email that the body states 1am my time and arrived at 8pm!
There is something else odd as well. I can't view my 'Zone Rank' It says I'm not ranked!
Hmm you are correct. There is some big time gaps. I just received an email that the body states 1am my time and arrived at 8pm!
There is something else odd as well. I can't view my 'Zone Rank' It says I'm not ranked!
Hi tedbilly: there are a few reported anomolies going on - the EE team are working on fixing it, but still not quite right. I am told at these times that patience is a virtue - a virtue that has already seen me loose out on many thousands of points.
If you want to do intersections then use the NOT IN or IN clause. NOTE: NOT IN is an expensive operation. It requires a full scan of the tables.