Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 14th, 2004, 02:46 PM
Registered User
 
Join Date: May 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help with TOP 5 and self join

I created a view in my database from a query which returns Week #, Week Started On, Media Type, Media Type Report Count. Usually I have about 10-15 rows for each week.

What I need to do is select the top 5 for each Week # but if I use top 5 it obviously only returns one week and I need it for all weeks. I know I need to use a self join somewhere but I'm new to SQL and need help!

 
Old May 16th, 2004, 07:48 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

Can you post your code for the view created. It sounds like you may need a group by clause with ties

 
Old May 17th, 2004, 11:36 AM
Registered User
 
Join Date: May 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

CREATE VIEW dbo.media_type_count_by_week
AS
SELECT TOP 100 PERCENT [Week #], [Week Starting On], name AS [Media Type], SUM(enrollment_count) AS [Media Type Report Count]
FROM
(SELECT datediff(d, '6/22/2003', date_created) / 7 AS [Week #], dateadd(d, floor(datediff(d, '6/22/2003', date_created) / 7) * 7, '6/22/2003')
AS [Week Starting On], media_type.name, COUNT(dbo.enrollment_info_real.id) AS enrollment_count
FROM dbo.enrollment_info_real
JOIN dbo.enrollment_info_media_type ON (enrollment_info_id = dbo.enrollment_info_real.id)
JOIN
dbo.media_type ON (media_type_id = dbo.media_type.id)
GROUP BY datediff(d, '6/22/2003', date_created) / 7, dateadd(d, floor(datediff(d, '6/22/2003', date_created) / 7) * 7, '6/22/2003'),
dbo.media_type.name

UNION

SELECT datediff(d, '6/22/2003', date_created) / 7 AS [Week #], dateadd(d, floor(datediff(d, '6/22/2003', date_created) / 7) * 7, '6/22/2003')
AS [Week Starting On], media_type_other, COUNT(dbo.enrollment_info_real.id)
FROM dbo.enrollment_info_real
WHERE (media_type_other IS NOT NULL) AND media_type_other NOT IN ('`')
GROUP BY datediff(d, '6/22/2003', date_created) / 7, dateadd(d, floor(datediff(d, '6/22/2003', date_created) / 7) * 7, '6/22/2003'), media_type_other), media_type_count
WHERE (name IS NOT NULL) AND (name <> 'null') AND (name <> 'Other')
GROUP BY [Week #], [Week Starting On], name
ORDER BY [Week #] DESC, [Media Type Report Count] DESC, name






Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT TOP n NOT SELECTING TOP n! ibi SQL Language 8 March 30th, 2005 08:08 PM
Join Query Distinct and Top 1 ahanson SQL Language 4 November 30th, 2004 01:29 PM
SELECT TOP 1 with INNER JOIN spinout SQL Language 14 November 22nd, 2004 08:36 PM
SELECT TOP FROM HAVING khatfield29 SQL Language 1 August 23rd, 2004 02:41 PM
Oracle 8i inner join and left join problem puteri_84 Oracle 2 August 19th, 2004 07:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.