Imran Hemani
Imran Hemani

Reputation: 629

Oracle SQL weeks sales SUM

I have the sales data in terms of week:

ITEM   LOC      WEEK       SALES  
111    39    16/05/2015     10
222    39    16/05/2015     23
111    39    09/05/2015     13
222    39    09/05/2015     33

I want the sum of SALES column for the last 4 weeks.

So it comes like:

ITEM   LOC   4-WEEKS-SALES
111    39       23
222    39       56

Upvotes: 0

Views: 67

Answers (2)

dani herrera
dani herrera

Reputation: 51715

Just filter for last four weeks and agregate:

select ITEM, LOC,sum(SALES) 
  from theTable 
 where WEEK > SYSDATE - ( 7 * 4 )
 group by ITEM,LOC

Upvotes: 1

Mukesh Kalgude
Mukesh Kalgude

Reputation: 4844

Try to this

 select ITEM,LOC,sum(SALES) '4-WEEKS-SALES'
    from tablename
    where Datepart(wk, WEEK)>=(Datepart(wk, Getdate())-4)
    Group by ITEM,LOC,Datepart(wk, WEEK)

Upvotes: 0

Related Questions