How to combine these two queries into a single one?

How to combine these two queries into a single one?



I have two queries that effectively use the same data. I am trying to combine them into one, so I can save space but I am having problems. I am trying to SELECT from the second query. I will demonstrate in my attempt.



Query A:


SELECT TOP 5 ([Parent]), [Game ID]
FROM Games
WHERE [Parent] = 'Zelda'
ORDER BY [Game ID] DESC;



Query B:


SELECT QueryA.[Parent], QueryA.[Game ID]
FROM QueryA
WHERE Mid(QueryA.[Game ID], 5) NOT IN (
SELECT MAX(Mid(Game.[Game ID], 5))
FROM Game
);



Separately, these queries work, but it seems like this can be combined into a single query. I am trying to figure out how. I thought I could just do something like


SELECT QueryA.[Parent], QueryA.[Game ID]
FROM (
SELECT TOP 5 ([Parent]), [Game ID]
FROM Games
WHERE [Parent] = 'Zelda'
ORDER BY [Game ID] DESC
)
WHERE Mid(QueryA.[Game ID], 5) NOT IN (
SELECT MAX(Mid(Game.[Game ID], 5))
FROM Game
);



But that's not the case. Seems basic, but I am trying to figure it out.






Why do you need two queries. Can you just add your WHERE condition of your outer query into the inner query?

– Shawn
Sep 11 '18 at 20:00


WHERE




1 Answer
1



Your problem can be that you forget the alias name for your subquery. Try this:


SELECT QueryA.[Parent],
QueryA.[Game ID]
FROM
(SELECT TOP 5 ([Parent]), [Game ID]
FROM Games
WHERE [Parent] = 'Zelda'
ORDER BY [Game ID] DESC) as QueryA
WHERE Mid(QueryA.[Game ID], 5) NOT IN
(SELECT MAX(Mid(Game.[Game ID], 5))
FROM Game);



And if this is not the solution, is happening some error that you could show?



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế