Geeks Talk

Prepare for your Next Interview




How to get this o/p from the sql query

This is a discussion on How to get this o/p from the sql query within the SQL forums, part of the Databases category; Hi i have the data set like this --> with t as ( select 1 as invoice,5000 as tot_amount,'1/1/2008' as pay_date,100 as payment from dual union select ...


Go Back   Geeks Talk > Databases > SQL

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 06-12-2008
Contributing Member
 
Join Date: Apr 2006
Location: kolkata(now in noida)
Posts: 56
Thanks: 9
Thanked 3 Times in 2 Posts
bhaski is on a distinguished road
How to get this o/p from the sql query

Hi i have the data set like this -->


with t as
(
select 1 as invoice,5000 as tot_amount,'1/1/2008' as pay_date,100 as payment from dual
union
select 1 ,5000 ,'5/1/2008' ,200 from dual
union
select 2 ,5000 ,'10/1/2008' ,400 from dual
union
select 2 ,5000 ,'15/1/2008' ,500 from dual
union
select 3 ,5000 ,'19/1/2008' ,600 from dual
)
select
invoice,tot_amount,
max(decode(extract(day from to_date(pay_date,'dd/mm/yyyy')),1,payment))day_1,
max(decode(extract(day from to_date(pay_date,'dd/mm/yyyy')),5,payment))day_5,
max(decode(extract(day from to_date(pay_date,'dd/mm/yyyy')),10,payment))day_10,
max(decode(extract(day from to_date(pay_date,'dd/mm/yyyy')),15,payment))day_15,
max(decode(extract(day from to_date(pay_date,'dd/mm/yyyy')),19,payment))day_19
from t
group by
invoice,tot_amount
order by 1

it's o/p is


INVOICE TOT_AMOUNT DAY_1 DAY_5 DAY_10 DAY_15 DAY_19
-----------------------------------------------------------------------
1 5000 100 200
2 5000 400 500
3 5000 600

Now how i can replace the header like day_1 with it's corresponding month/year format?

INVOICE TOT_AMOUNT 1/2008 5/2008 10/2008 15/2008 19/2008
------------------------------------------------------------------------
1 5000 100 200
2 5000 400 500
3 5000 600

Thanks in advance
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-12-2008
Expert Member
 
Join Date: Sep 2007
Posts: 754
Thanks: 22
Thanked 64 Times in 63 Posts
krishnaindia2007 is on a distinguished road
Re: How to get this o/p from the sql query

set verify off

col day_1 new_value day_1
col day_5 new_value day_5
col day_10 new_value day_10
col day_15 new_value day_15
col day_19 new_value day_19

select to_Char(to_date('1/1/2008','DD/MM/YYYY'),'MM/YYYY') as day_1 from dual;
select to_Char(to_date('5/1/2008','DD/MM/YYYY'),'MM/YYYY') as day_5 from dual;
select to_Char(to_date('10/1/2008','DD/MM/YYYY'),'MM/YYYY') as day_10 from dual;
select to_Char(to_date('15/1/2008','DD/MM/YYYY'),'MM/YYYY') as day_15 from dual;
select to_Char(to_date('19/1/2008','DD/MM/YYYY'),'MM/YYYY') as day_19 from dual;

with t as
(
select 1 as invoice,5000 as tot_amount,'1/1/2008' as pay_date,100 as payment from dual
union
select 1 ,5000 ,'5/1/2008' ,200 from dual
union
select 2 ,5000 ,'10/1/2008' ,400 from dual
union
select 2 ,5000 ,'15/1/2008' ,500 from dual
union
select 3 ,5000 ,'19/1/2008' ,600 from dual
)
select
invoice,tot_amount,
max(decode(extract(day from to_date(pay_date,'dd/mm/yyyy')),1,payment)) "&day_1",
max(decode(extract(day from to_date(pay_date,'dd/mm/yyyy')),5,payment)) "&day_5",
max(decode(extract(day from to_date(pay_date,'dd/mm/yyyy')),10,payment)) "&day_10",
max(decode(extract(day from to_date(pay_date,'dd/mm/yyyy')),15,payment)) "&day_15",
max(decode(extract(day from to_date(pay_date,'dd/mm/yyyy')),19,payment)) "&day_19"
from t
group by
invoice,tot_amount
order by 1;
Code:
   INVOICE TOT_AMOUNT    01/2008    01/2008    01/2008    01/2008    01/2008
---------- ---------- ---------- ---------- ---------- ---------- ----------
         1       5000        100        200
         2       5000                              400        500
         3       5000                                                    600
Reply With Quote
  #3 (permalink)  
Old 06-13-2008
Contributing Member
 
Join Date: Apr 2006
Location: kolkata(now in noida)
Posts: 56
Thanks: 9
Thanked 3 Times in 2 Posts
bhaski is on a distinguished road
Re: How to get this o/p from the sql query

Thanx for reply.

I found a very very good link regarding this solution.This much similar as yr but little modified.

http://asktom.oracle.com/pls/asktom/...25000346503967
Reply With Quote
  #4 (permalink)  
Old 06-16-2008
Expert Member
 
Join Date: Dec 2007
Location: Pune
Posts: 139
Thanks: 0
Thanked 6 Times in 6 Posts
peeyush_jain is on a distinguished road
Re: How to get this o/p from the sql query

Thanks for soultion , I agree with you.
Reply With Quote
  #5 (permalink)  
Old 06-21-2008
Junior Member
 
Join Date: Jun 2008
Location: India
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
arvindsml is on a distinguished road
Re: How to get this o/p from the sql query

max(decode(extract(day from to_date(pay_date,'dd/mm/yyyy')),1,payment)) "&day_1",
Reply With Quote
Reply

  Geeks Talk > Databases > SQL


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
SQL query.. prafulkr SQL 2 02-14-2008 06:14 AM
DB2 query kanchhana DB2 2 12-28-2007 01:12 AM
Query sakshi_2801 SQL 4 08-06-2007 11:29 AM
regarding sql query psuresh1982 SQL 8 07-13-2007 04:20 AM
Query jescalante Oracle 4 06-25-2007 03:13 AM


All times are GMT -4. The time now is 02:56 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved