Monday 15 March 2010

sql - Simple Where Exists vs. In -


I can not see what I'm missing for my life but someone can tell me why the query that first I do not want to, but the second thing works fine? I know that the questions are not exactly the same, but they should return the same 20 rows and they do not do that. (Query fill the top 20 most common items in a table)

  Choose rx.drug_class different from Rx where exists (Select the top 20 rx2.drug_class, COUNT (separate rx2.pat_id) Calculate , More than rank (), according to the order (different-pat_id), RX as RX Rx 2 - when the line given below is commented - this subquery gives the correct answer where rx .drug_class = rx2.drug_class group rx2.drug_class)   

this works fine

  choose rx.drug_class different from RX where In the rx.drug_class (select the top 20 rx.drug_class from the Rx group) 

Where the subdivision present in the novel is not working, what gives?

However, if you are specifying a line number for the first query, you still have to ORDER BY This line number is required:

  where rx.drug_class = rx2.drug_class group rx2.drug_class order by rn   

Als o I am assuming that this is just a simplified example, because the following will be as good as the following:

  Select the top 20 from RX group from RX group. Specific   

Edit:

Your EXISTS will also not work because you are limiting their limitations in the internal before Before you count your ..., set the line in the external set to SELECT then in that case the line will always be present in the top 20 count.

IN works because it is a set of calculations for all rows ... EXISTS fails because every individual line in the external set Is being counted for .. Therefore, every line in the outer set is in the top 20 digits, when those numbers are limited to only the external line's drug_class .

No comments:

Post a Comment