0

I have a Sql Server query as:

SELECT  AC.Author_ID, A.Author_Name, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID, AC.Year
FROM    AuthorCoAuthor  AC
JOIN    AuthorPaper     AP ON AP.Author_ID  = AC.Author_ID AND 
                        AP.Paper_ID = AC.Paper_ID
JOIN    Author          A  ON A.Author_ID   = AC.Author_ID
ORDER BY AC.Author_ID, AC.Year, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID  

which returns almost 221317 rows. The sample output is:

Author_ID | Author_Name | Paper_ID | CoAuthor_ID | Venue_ID | Year  
------------------------------------------------------------------
677       | Nuno Vasc   | 812229   | 901706      | 64309    | 2005
677       | Nuno Vasc   | 812486   | 901706      | 65182    | 2005
677       | Nuno Vasc   | 818273   | 901706      | 185787   | 2007  
1359      | Peng Sui    | 818373   | 457348      | 18534    | 2005  
1359      | Peng Sui    | 868273   | 542321      | 184222   | 2006  
...       | ...         | ...      | ...         | ...      | ...  

I want to have only 10 distinct Author_ID to be returned by the query, whereas I tried by adding

WHERE  COUNT(DISTINCT(AC.Author_ID)) <= 10  

this WHERE clause into the same query but still I'm getting same number of rows i.e. all the data which normally query returns.

Why my WHERE clause not working here?

5
  • 1
    Which 10 author IDs Top 10? Random 10? Top Sequential? Commented Jun 18, 2016 at 19:44
  • Top Sequential IDs Commented Jun 18, 2016 at 19:47
  • Then Matjedi got ot Commented Jun 18, 2016 at 19:48
  • He posted an answer Commented Jun 18, 2016 at 19:54
  • Sorry ... on my phone and don't have my glasses. Sucks getting old Commented Jun 18, 2016 at 19:59

1 Answer 1

1

To get 10 Distinct Author_ID, you can use GROUP BY with TOP 10. Then join the result (10 rows here) with other tables to show columns you want, in the select list.

You can try this:

SELECT AC.Author_ID, A.Author_Name, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID, AC.Year
FROM
(
   SELECT TOP (10) AC.Author_ID
   FROM AuthorCoAuthor AC
   GROUP BY AC.Author_ID
   ORDER BY AC.Author_ID
) DS
JOIN AuthorCoAuthor  AC ON AC.Author_ID = DS.Author_ID
JOIN    AuthorPaper     AP ON AP.Author_ID  = AC.Author_ID AND 
                    AP.Paper_ID = AC.Paper_ID
JOIN    Author          A  ON A.Author_ID   = AC.Author_ID
ORDER BY AC.Author_ID, AC.Year, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID 

UPDATE

By the Previous Answer, You can get less than 10 Distinct Author if both AuthorPaper & Author table doesn't have one of those Top 10 Sequential Author. For that reason, you need to check those condition when you get 10 Distinct Author so that it always give you 10 Distinct Author if and only if AuthorCoAuthor has at least 10 Distinct Author. So, To make sure, you always get 10 Distinct Author, you can use this one:

SELECT AC.Author_ID, A.Author_Name, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID, AC.Year
FROM
(
   SELECT TOP (10) AC.Author_ID
   FROM AuthorCoAuthor AC
   JOIN AuthorPaper AP 
      ON AP.Author_ID  = AC.Author_ID 
         AND AP.Paper_ID = AC.Paper_ID
   JOIN Author A  ON A.Author_ID = AC.Author_ID
   GROUP BY AC.Author_ID
   ORDER BY AC.Author_ID
) DS
JOIN AuthorCoAuthor  AC ON AC.Author_ID = DS.Author_ID
JOIN AuthorPaper AP 
   ON AP.Author_ID  = AC.Author_ID 
      AND AP.Paper_ID = AC.Paper_ID
JOIN Author A  ON A.Author_ID = AC.Author_ID
ORDER BY AC.Author_ID, AC.Year, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID 
Sign up to request clarification or add additional context in comments.

2 Comments

Not allowing me to use DISTINCT keyword with TOP(10) here
Updated the Answer. Check Now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.