Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Top 10 products by Month Expand / Collapse
Author
Message
Posted Wednesday, February 13, 2013 11:57 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, November 14, 2014 1:52 AM
Points: 74, Visits: 363
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
Post #1419827
Posted Thursday, February 14, 2013 12:12 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 7:43 AM
Points: 916, Visits: 2,502
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
Post #1419833
Posted Thursday, February 14, 2013 1:02 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, November 14, 2014 1:52 AM
Points: 74, Visits: 363
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
Post #1419853
Posted Thursday, February 14, 2013 1:28 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, November 1, 2014 6:54 AM
Points: 5,221, Visits: 5,118
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
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

Post #1419859
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse