SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Top 10 products by Month


Top 10 products by Month

Author
Message
Bulelani M
Bulelani M
SSC-Enthusiastic
SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)

Group: General Forum Members
Points: 112 Visits: 399
Hello
Can anyone help me with this below.

Jan 2010 2011 2012
course name course name course name
course name course name course name
course name course name course name
course name course name course name
course name course name course name

Feb 2010 2011 2012
course name course name course name
course name course name course name
course name course name course name
course name course name course name
course name course name course name
Jason-299789
Jason-299789
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2065 Visits: 3232
If you want the Top 10 Items per month then a Ranking function (RANK(), ROW_NUMBER()) is going to be the best way to move.

However without sample DDL and Input data as well as out put data its difficult to advise on the best solution.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Bulelani M
Bulelani M
SSC-Enthusiastic
SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)

Group: General Forum Members
Points: 112 Visits: 399
I got what I wanted but now I also want it by year.
Here is the code below

;WITH MonthCTE(m) AS
(SELECT 1 m
UNION ALL
SELECT m+1
FROM monthCTE
WHERE m < 12
)
SELECT m[Month]
,t.*
FROM MonthCTE
CROSS Apply
(

SELECT TOP 20
YEAR(ah.enrllmnt_prcssd_dt) [Year]
,p.code
,p.full_name
,CAST(SUM(CASE
WHEN e.payment_type = 1 THEN e.cash / 100
ELSE (1.00 * IsNull(e.installment, 0)
/ 100.00 * IsNull(e.payment_months, 0)) + (1.00 *
IsNull(e.deposit,
0) / 100.00)
END) AS decimal) AS Gev
FROM enrollment AS e
INNER JOIN action_history AS ah
ON ah.action_history_id = e.action_history_id
INNER JOIN product AS p
ON p.product_id = e.product_id
WHERE (
month(ah.enrllmnt_prcssd_dt) BETWEEN 1 AND 12
)
AND (ah.enrllmnt_prcssd_dt >= '2010-01-01' )
AND (ah.enrllmnt_prcssd_dt < '2011-01-01' )--AND (p.code IN (@Prod_code) )
AND (e.status = 2 )
AND (e.brand_id IN (2) )
GROUP BY
YEAR(ah.enrllmnt_prcssd_dt)
,--month(ah.enrllmnt_prcssd_dt),
p.code
,p.full_name
ORDER BY
--YEAR(ah.enrllmnt_prcssd_dt),
--month(ah.enrllmnt_prcssd_dt),
Gev
DESC
) t
anthony.green
anthony.green
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10052 Visits: 6324
Please post table definitions, sample data and expected outcome based on the sample data as per the second link in my signature.

With that, we will be able to help you better with your query.



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search