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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 selecting previous 3 months

Author  Topic 

Champinco
Yak Posting Veteran

54 Posts

Posted - 2006-11-27 : 01:59:41
Could I please get some assistance with selecting/showing records for each product_category_name from the last 3 months. That is for each Product_Category_Name return the last 3 months worth of rows. In this example i want to return the rows in bold:

PRODUCT_CATEGORY_ID PRODUCT_CATEGORY_NAME PRODUCT_CATEGORY_AMOUNT COMMISSION_MONTH
1 Product A 54985 1/10/2004
1 Product A 349050 1/09/2004
1 Product A 167645 1/08/2004
1 Product A 189585 1/07/2004
2 Product B 65206.94 1/08/2006
2 Product B 258028.73 1/07/2006
2 Product B 246538.22 1/06/2006
2 Product B 72015.92 1/05/2006
7 LINE 283506.69 1/08/2006
7 LINE 314186.78 1/07/2006
7 LINE 260485.38 1/06/2006
7 LINE 296916.33 1/05/2006
7 LINE 319169.2 1/04/2006
7 LINE 332763.64 1/03/2006
7 LINE 329983.35 1/08/2004
7 LINE 319307.68 1/07/2004
9 CONTRACT 25950 1/08/2006
9 CONTRACT 20350 1/07/2006
9 CONTRACT 4550 1/06/2006
9 CONTRACT 48200 1/05/2006
9 CONTRACT 248000 1/04/2006

Here is the code that i got to get the above result:

select product_list.PRODUCT_CATEGORY_ID,
product_list.[NAME] AS PRODUCT_CATEGORY_NAME,
sum(amount) as PRODUCT_CATEGORY_AMOUNT,
commission_month
from table1 LEFT OUTER JOIN product_list
ON COMMISSION_TXN.PRODUCT_CODE = product_list.PCMSCODE
group by commission_month, product_list.PRODUCT_CATEGORY_ID, product_list.[NAME]
order by product_list.PRODUCT_CATEGORY_ID, product_list.[NAME] desc, commission_month desc


ps: How do i post a topic with proper formatting, I tried in several text editors but it seems to post my topic all jumbled up.
Cheers
GK

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-27 : 02:01:23
Use tags [ code ] and [ /code ] (but without spaces in the tag)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-27 : 02:17:19
Contract SUM is 50850, not 48200! Brush up your basic math skills.
-- prepare test data

declare @t table (PRODUCT_CATEGORY_ID tinyint, PRODUCT_CATEGORY_NAME varchar(9), PRODUCT_CATEGORY_AMOUNT money, COMMISSION_MONTH datetime)

set dateformat dmy

insert @t
select 1, 'Product A', 54985, '1/10/2004' union all
select 1, 'Product A', 349050, '1/09/2004' union all
select 1, 'Product A', 167645, '1/08/2004' union all
select 2, 'Product B', 65206.94, '1/08/2006' union all
select 2, 'Product B', 258028.73, '1/07/2006' union all
select 2, 'Product B', 246538.22, '1/06/2006' union all
select 7, 'LINE', 283506.69, '1/08/2006' union all
select 7, 'LINE', 314186.78, '1/07/2006' union all
select 7, 'LINE', 260485.38, '1/06/2006' union all
select 7, 'LINE', 319169.2, '1/04/2006' union all
select 7, 'LINE', 332763.64, '1/03/2006' union all
select 7, 'LINE', 329983.35, '1/08/2004' union all
select 7, 'LINE', 319307.68, '1/07/2004' union all
select 9, 'CONTRACT', 25950, '1/08/2006' union all
select 9, 'CONTRACT', 20350, '1/07/2006' union all
select 9, 'CONTRACT', 4550, '1/06/2006' union all
select 9, 'CONTRACT', 248000, '1/04/2006'

-- do the work
select t.PRODUCT_CATEGORY_ID,
t.PRODUCT_CATEGORY_NAME,
t.PRODUCT_CATEGORY_AMOUNT,
t.COMMISSION_MONTH,
case
when w.product_category_id is null then ''
else cast(t.PRODUCT_CATEGORY_ID as varchar) + ' ' + t.PRODUCT_CATEGORY_NAME + ' ' + cast((select sum(r.PRODUCT_CATEGORY_AMOUNT) from @t r where r.product_category_id = t.product_category_id and r.commission_month >= w.peso) as varchar) end
from @t t
left join (
select product_category_id,
dateadd(month, -2, max(COMMISSION_MONTH)) peso
from @t
group by product_category_id
) w on w.product_category_id = t.product_category_id and w.peso = t.COMMISSION_MONTH


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -