Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to Return First Record child record and count

Author  Topic 

Jeremy Wadsworth
Starting Member

2 Posts

Posted - 2006-01-31 : 11:15:40
I've been looking for examples online to write a SPROC to get some data. Here are the tables.

Album_Category
AlbumCategoryID (PK, int, not null)
Caption (nvarchar(max), not null)
IsPublic (bit, not null)


Albums
AlbumID (PK, int, not null)
AlbumCategoryID (int, null)
Caption (nvarchar(max), not null)
IsPublic (bit, not null)


I need to return:
-[Album_Category].[AlbumCategoryID]
-[Album_Category].[Caption]
-[Albums].[Single AlubmID for each AlbumCategoryID]
-[Count of Albums in each AlbumCategory]


I hope I was fairly clear in what I'm trying to do. Any tips or help would be appreciated. Thanks.

JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2006-01-31 : 11:34:21
This should work...

select ac.AlbumCategoryID, ac.Caption, min(a.AlbumID), count(a.AlbumID)
from Album_Category ac inner join Album a
on ac.AlbumCategoryID = a.AlbumCategoryID
group by ac.AlbumCategoryID, ac.Caption

"Hit me with a shovel 'cause I can't believe I dug you."
Go to Top of Page

Jeremy Wadsworth
Starting Member

2 Posts

Posted - 2006-01-31 : 13:02:57
Thanks Justin. This worked great. I thought the SQL would be more complex than that.

Thanks again.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-01 : 01:10:45
Learn SQL
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -