In this post I am just putting some helpful SQL queries which will be useful at some point in your SQL / PLSQL Programming life. Most of these are taken from the OTN Forum answers or from my project experiences.
Refer:
PIVOT and UNPIVOT Operators in Oracle Database 11g Release
Pivot and Unpivot
#3. How to get the first 100 Numbers in a query.
#4. How to get all the dates between 2 dates.
I have used 01-Jan-2015 as the start date and 31-Jan-2015 as the end date.
#5. How to remove duplicates from one column.
Source Data
Expected Output
Normally this is job of the client / front end tool. But some reason you want this output in the query itself, you can use the below query.
#6. How to get the cumulative total.
Source Data
Expected Output
The data has to be sorted with the ITEM_NO and then with the Date and the Cumulative sum for each Item.
#1. How to convert Rows into Columns.
Source Data
Expected Output
Using Pivot:
#2. How to convert Columns into Rows.
Source Data
For versions prior to 11g. If you want null values also, then remove the where clause.
Source Data
Expected Output
Using Pivot:
with t as
(select 1 id,'MONDAY' day,'10:00' start_time,'12:00' end_time from dual
union
select 1,'TUESDAY','11:00','12:00' from dual
union
select 1,'WEDNESDAY','09:00','12:00' from dual
union
select 2,'MONDAY','08:00','10:00' from dual
union
select 3,'TUESDAY','06:00','09:00' from dual)
select * from t
pivot
(
max(start_time) as start_time, max(end_time) as end_time
for day in ('MONDAY' "Mon",'TUESDAY' "Tue",'WEDNESDAY' "Wed")
)
order by 1;
#2. How to convert Columns into Rows.
Source Data
Expected output
with t as
(select '1' column1, '2' column2, null column3, '4' column4, null column5
from dual)
select decode(val,1,column1,2,column2,3,column3,4,column4,column5) display_val
from t
cross join (select level val from dual connect by level <= 5)
where decode(val,1,column1,2,column2,3,column3,4,column4,column5) is not null;
For 11g or above - using PIVOT/UNPIVOT. If you don't want the null values, remove 'include nulls'.
with t as
(select '1' column1, '2' column2, null column3, '4' column4, null column5
from dual)
select val disp_val
from t
unpivot include nulls (val for col in(column1, column2, column3,column4,column5));
Refer:
PIVOT and UNPIVOT Operators in Oracle Database 11g Release
Pivot and Unpivot
#3. How to get the first 100 Numbers in a query.
selectrownumvalfromdualconnect by rownum<= 100;
#4. How to get all the dates between 2 dates.
I have used 01-Jan-2015 as the start date and 31-Jan-2015 as the end date.
select(to_date('01-Jan-2015','DD-MON-YYYY') +level- 1) date_valfromdualconnect by level<= (to_date('31-Jan-2015','DD-MON-YYYY') -to_date('01-Jan-2015','DD-MON-YYYY') + 1);
#5. How to remove duplicates from one column.
Source Data
Expected Output
Normally this is job of the client / front end tool. But some reason you want this output in the query itself, you can use the below query.
withtas(select'ABC Company'company,'India'branch,'11111'contactfromdualunion allselect'ABC Company','Australia','22222'fromdualunion allselect'ABC Company','America','33333'fromdualunion allselect'XYZ Company','Australia','000000'fromdualunion allselect'XYZ Company','America','111111'fromdual )select(casewhenrow_number()over(partition bycompanyorder bybranchdesc) = 1thencompanyend) company ,branch ,contactfromt
#6. How to get the cumulative total.
Source Data
Expected Output
The data has to be sorted with the ITEM_NO and then with the Date and the Cumulative sum for each Item.
withtas(select1 item_no,to_date('2015-10-30','YYYY-MM-DD') inv_date ,30 amtfromdualunionselect2 item_no,to_date('2015-10-23','YYYY-MM-DD') inv_date ,100 amtfromdualunionselect2 item_no,to_date('2014-10-23','YYYY-MM-DD') inv_date ,70 amtfromdualunionselect1 item_no,to_date('2014-09-10','YYYY-MM-DD') inv_date ,50 amtfromdualunionselect1 item_no,to_date('2013-08-17','YYYY-MM-DD') inv_date ,40 amtfromdual )selectitem_no,to_char(inv_date,'DD-Mon-YYYY') invoice_date,amt amount ,sum(amt)over(partition byitem_noorder byitem_no,inv_date)ascumulative_amountfromtorder byitem_no, inv_date;
Feel free to point out if anything is missing/wrong in this blog.