SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 help with joining two tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Exir
Posting Yak Master

151 Posts

Posted - 03/05/2013 :  13:21:22  Show Profile  Reply with Quote
Hi
There is some tables :

Author ( AuthorID , AuthorFirstName , AuthorLastName )
BookTitle ( ISBN_Number , Title , PublisherID , Date_of_Published , Pages , Price , Comment )
Publisher ( PublisherID , PublisherName )
Category ( CategoryID , CategoryDescription )
Book_Author ( ISBN_Number , AuthorID )
Book_Category ( ISBN_Number,CategoryID )

I want to select the name of the Author who has the minimum compliation.
Please help me with the query

James K
Flowing Fount of Yak Knowledge

3562 Posts

Posted - 03/05/2013 :  13:34:26  Show Profile  Reply with Quote
SELECT TOP (1) WITH TIES
	a.AuthorID,
	a.AuthorFirstName,
	a.AuthorLastName,
	COUNT(*) AS Compilations
FROM
	Author a
	INNER JOIN Book_Author b ON
		a.AuthorID = b.AuthorID
GROUP BY
	a.AuthorID,
	a.AuthorFirstName,
	a.AuthorLastName
ORDER BY
	Compilations ASC;
Can there be authors with no publications at all? If there are, are those the ones you want to pick?
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 03/05/2013 :  13:47:02  Show Profile  Reply with Quote
Thank you so much for your answer

quote:
Originally posted by James K
Can there be authors with no publications at all? If there are, are those the ones you want to pick?



yes, if there is some authors with no publications, those authors should be chosen

Edited by - Exir on 03/05/2013 13:54:27
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3562 Posts

Posted - 03/05/2013 :  14:12:53  Show Profile  Reply with Quote
Ok - make the changes shown in red
SELECT TOP (1) WITH TIES
	a.AuthorID,
	a.AuthorFirstName,
	a.AuthorLastName,
	COUNT(b.ISBN_Number) AS Compilations
FROM
	Author a
	LEFT JOIN Book_Author b ON
		a.AuthorID = b.AuthorID
GROUP BY
	a.AuthorID,
	a.AuthorFirstName,
	a.AuthorLastName
ORDER BY
	Compilations ASC;
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000