Solved

Pivot Data Of Table By Month/Or Week

Posted on 2014-11-08
9
282 Views
Last Modified: 2014-11-15
hi
i simply have table My_Trans
Trans_ID number
Branch_No Number
Trans_Amount Number
Trans_Date Date

how i can generate pivot query from the above table like this :
Branch        Jan       Feb     Mar   Apr
01                1500   1300   2000 3000
02                2000   1000   2000 4000
03                3000    1500  3000  4500 

Open in new window


same requirement for week number of the year
note : database i'm connection to is 9i
0
Comment
Question by:NiceMan331
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 40430330
select * from (
 select branch_no, trans_amount, to_char(trans_date,'MON') month
 from my_trans)
pivot
(
 sum(trans_amount)
 for month in ('JAN','FEB','MAR','APR')
)
order by branch_no
;
0
 

Author Comment

by:NiceMan331
ID: 40430342
what about week number of the year ?
0
 
LVL 32

Expert Comment

by:awking00
ID: 40430378
Same way -
select * from (
 select branch_no, trans_amount, to_char(trans_date,'WW') week
 from my_trans)
pivot
(
 sum(trans_amount)
 for week in ('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18')
)
order by branch_no
;
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:NiceMan331
ID: 40430400
sorry , the pivot not working with me , i'm using oracle 9i
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40431682
Try the code below.  You'll just need to add the rest of the months and weeks:
select branch_no,
	sum(case when to_char(trans_date,'Mon') = 'Jan' then trans_amount else 0 end) Jan,
	sum(case when to_char(trans_date,'Mon') = 'Feb' then trans_amount else 0 end) Feb,
	sum(case when to_char(trans_date,'Mon') = 'Mar' then trans_amount else 0 end) Mar,
	sum(case when to_char(trans_date,'Mon') = 'Apr' then trans_amount else 0 end) Apr
from my_trans
group by branch_no
/

select branch_no,
	sum(case when to_number(to_char(trans_date,'IW')) = 1 then trans_amount else 0 end) week_1,
	sum(case when to_number(to_char(trans_date,'IW')) = 2 then trans_amount else 0 end) week_2,
	sum(case when to_number(to_char(trans_date,'IW')) = 3 then trans_amount else 0 end) week_3,
	sum(case when to_number(to_char(trans_date,'IW')) = 4 then trans_amount else 0 end) week_4
from my_trans
group by branch_no
/

Open in new window

0
 

Author Comment

by:NiceMan331
ID: 40434360
yes slightw it is ok for months
but regarding weeks , it will be very smart if you adjust the code to add start week and end week
to the beginning of the sql , then to add the weeks by for loop if you could do it
thanx
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40435896
I do not understand what you mean by start week and end week.

Please post sample results.
0
 

Author Comment

by:NiceMan331
ID: 40435916
i mean the year has 52 weeks
instead of repeating the code 52 times
if i need only weeks between 40 and 48 for example
let the code begin loop between 40 and 48
then to add the code 8 times with increment of week number
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40436479
If you want 8 weeks from the current date's week number try this (just keep adding weeks).

Just grab the week number from sysdate:
select branch_no,
	sum(case when to_number(to_char(trans_date,'IW')) = to_number(to_char(sysdate,'IW')) then trans_amount else 0 end) week_1,
	sum(case when to_number(to_char(trans_date,'IW')) = to_number(to_char(sysdate+7,'IW')) then trans_amount else 0 end) week_2,
	sum(case when to_number(to_char(trans_date,'IW')) = to_number(to_char(sysdate+14,'IW')) then trans_amount else 0 end) week_3,
	sum(case when to_number(to_char(trans_date,'IW')) = to_number(to_char(sysdate+21,'IW')) then trans_amount else 0 end) week_4
from my_trans
group by branch_no
/

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Character matching different date formats for dates between 6 80
Read XML values 8 67
Oracle - BLOB Extract Line 2 13
Oracle perfomance tuneing. 3 45
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question