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 product in each month each year Expand / Collapse
Author
Message
Posted Monday, January 07, 2013 7:19 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 21, 2013 12:01 PM
Points: 149, Visits: 346
hi

i have two tables

table A

main_table

id fill_date

1 09/04/2003

2 12/31/2005

3 01/05/1985

product

id name

1 oxygen

2 detox

3 carbo

what i want is top 10 product for each month in each year without cte.

output

in year 2007

january top 10 productname (calculate by count(productname) desc)

feb ----

....

dec

in year 2008

january top 10 productname (calculate by count(productname) desc)

feb ----

....

dec

please help me
Post #1403592
Posted Monday, January 07, 2013 7:22 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 9:29 AM
Points: 4,240, Visits: 9,487
You've been here long enough to know the score. Please provide DDL, sample data and desired output in readily consumable form, as per the link in my signature.

____________________________________________________________________________________________

Help us to help you. For better, quicker and more focused answers to your questions, consider following the advice in this link:

http://www.sqlservercentral.com/articles/Best+Practices/61537/

If you are asking for help and your post does not contain a question, you should expect responses which do not contain any answers. Put a question mark in there somewhere - it's not rocket science.
Post #1403593
Posted Monday, January 07, 2013 7:22 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:07 AM
Points: 21,604, Visits: 27,434
Why the requirement that CTEs cannot be used?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1403594
Posted Monday, January 07, 2013 8:12 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 21, 2013 12:01 PM
Points: 149, Visits: 346
i need to run in sql 2000,so cte is nto working over there.i need select statement.


create table A
(id int primary key,
date1 date)


create table tableb
(id int references tableA(id),
productname varchar(40)


insert into tableA values(1,'01/01/2008')
insert into tableA values(2,'12/31/2005')
insert into tableA values(3,23/03/2004')


insert into tableb(1,'ordfy')
insert into tableb(2,'detox')
insert into tablec(3,'lokhj')

my output should be


year 2005
month drugname drugname drugname
january top1drugname top2drugname top10drugname
february top1drugname top2drugname top10drugname
march



dec

year 2006

month drugname drugname drugname
january top1drugname top2drugname top10drugname
february top1drugname top2drugname top10drugname
march



dec


till year 2012

what i want is top 10 product in each month in each year



Post #1403637
Posted Monday, January 07, 2013 8:26 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 9:59 AM
Points: 8,599, Visits: 8,238
Your sample data is obviously not complete. It is totally useless in it's current form. You have two tables, one with a date and one with a varchar. There is nothing tie them together. From your vague post it seems that you want to use a cross tab with some grouping. You have a couple paths to get from where you to a solution. You can read the link in my signature about cross tabs and figure it out on your own. Your second option is to read the article at the first link in my signature and post ddl and sample with enough information to solve your problem and we can help. The path you chose is entirely up to you.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1403650
Posted Tuesday, January 08, 2013 7:01 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:07 AM
Points: 21,604, Visits: 27,434
harri.reddy (1/7/2013)
i need to run in sql 2000,so cte is nto working over there.i need select statement.


create table A
(id int primary key,
date1 date)


create table tableb
(id int references tableA(id),
productname varchar(40)


insert into tableA values(1,'01/01/2008')
insert into tableA values(2,'12/31/2005')
insert into tableA values(3,23/03/2004')


insert into tableb(1,'ordfy')
insert into tableb(2,'detox')
insert into tablec(3,'lokhj')

my output should be


year 2005
month drugname drugname drugname
january top1drugname top2drugname top10drugname
february top1drugname top2drugname top10drugname
march



dec

year 2006

month drugname drugname drugname
january top1drugname top2drugname top10drugname
february top1drugname top2drugname top10drugname
march



dec


till year 2012

what i want is top 10 product in each month in each year





Few things. First, you really should have posted this in the SQL Server 7/2000 forums (and no, do repost it now). Second, you should have simply stated that you are using SQL Server 2000 up front instead of saying no CTEs. The third, Sean already mentioned. You really haven't given us enough to really help you yet. Please read the second article he suggested (it also happens to be the first one I reference below in my signature block as well) and follow the instructions in that article about what you should post and how to post it.

The more you do for us up front, the better answers you will get in return.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1404223
Posted Tuesday, January 08, 2013 10:08 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, April 11, 2013 2:09 PM
Points: 14, Visits: 97
Just follow the sample code, hope it will help you to get start.............................................

declare @index integer
set @index = 1
DECLARE @C_YEAR VARCHAR(4)
SET @C_YEAR = 1995

DECLARE @TEMP_TOTAL TABLE
(
TERM_REPORTING_YEAR decimal(4),
TERMS_ID varchar(7)

)

while @index <= (SELECT count (distinct CATALOGS) FROM PROGRAMS )
begin

set @C_YEAR = @C_YEAR + 1

INSERT @TEMP_TOTAL
SELECT top 3 TERM_REPORTING_YEAR, TERMS_ID
FROM TERMS WHERE TERM_REPORTING_YEAR = @C_YEAR

set @index = @index + 1
end

SELECT * FROM @TEMP_TOTAL
Post #1404361
Posted Tuesday, January 08, 2013 10:23 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 9:59 AM
Points: 8,599, Visits: 8,238
mdsharif532 (1/8/2013)
Just follow the sample code, hope it will help you to get start.............................................

declare @index integer
set @index = 1
DECLARE @C_YEAR VARCHAR(4)
SET @C_YEAR = 1995

DECLARE @TEMP_TOTAL TABLE
(
TERM_REPORTING_YEAR decimal(4),
TERMS_ID varchar(7)

)

while @index <= (SELECT count (distinct CATALOGS) FROM PROGRAMS )
begin

set @C_YEAR = @C_YEAR + 1

INSERT @TEMP_TOTAL
SELECT top 3 TERM_REPORTING_YEAR, TERMS_ID
FROM TERMS WHERE TERM_REPORTING_YEAR = @C_YEAR

set @index = @index + 1
end

SELECT * FROM @TEMP_TOTAL


That would probably work but it will be slower than molasses on the Alaskan tundra in January. This can absolutely be done with a single insert statement but until we have details to work with there is no way to help write the query.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1404368
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse