Tuesday, March 8, 2016

code to Convert column to row

Another code to Convert column to row

 Run The Function and find the different of Each Query


create table retail_sale
(
 retail_sale_id number not null
,terminal_id number not null
,sale_date date not null
,currency_category_code varchar2(8) -- base, original
,ttl_amt number
,ttl_dis number
,ttl_tax number
)


select * from retail_sale

create unique index retail_sale_pk on retail_sale (retail_sale_id)
/
create unique index retail_sale_uk1 on retail_sale (terminal_id,sale_date,currency_category_code)
/

alter table retail_sale
   add constraint retail_sale_pk primary key (retail_sale_id)
   add constraint retail_sale_uk1 unique (terminal_id,sale_date,currency_category_code)
/

insert into retail_sale values (1,1,trunc(sysdate),'ORIGINAL',100,20,4.80);
insert into retail_sale values (2,1,trunc(sysdate),'BASE',100*1.17,20*1.17,round(4.8*1.17,2));



select *
from retail_sale
/

select
       terminal_id
      ,sale_date
      ,case when currency_category_code = 'BASE'     then ttl_amt end base_ttl_amt
      ,case when currency_category_code = 'BASE'     then ttl_dis end base_ttl_dis
      ,case when currency_category_code = 'BASE'     then ttl_tax end base_ttl_tax
      ,case when currency_category_code = 'ORIGINAL' then ttl_amt end original_ttl_amt
      ,case when currency_category_code = 'ORIGINAL' then ttl_dis end original_ttl_dis
      ,case when currency_category_code = 'ORIGINAL' then ttl_tax end original_ttl_tax
from retail_sale
/

select
       terminal_id
       ,sale_date
      ,sum(case when currency_category_code = 'BASE'     then ttl_amt end) base_ttl_amt
      ,sum(case when currency_category_code = 'BASE'     then ttl_dis end) base_ttl_dis
      ,sum(case when currency_category_code = 'BASE'     then ttl_tax end) base_ttl_tax
      ,sum(case when currency_category_code = 'ORIGINAL' then ttl_amt end) original_ttl_amt
      ,sum(case when currency_category_code = 'ORIGINAL' then ttl_dis end) original_ttl_dis
      ,sum(case when currency_category_code = 'ORIGINAL' then ttl_tax end) original_ttl_tax
from retail_sale
group by
       terminal_id
      ,sale_date
/


select retail_sale_id
      ,terminal_id
      ,sale_date
      ,currency_category_code
      ,case
           when dollar_type.rowno = 1 then 'TTL_AMT'
           when dollar_type.rowno = 2 then 'TTL_DIS'
           when dollar_type.rowno = 3 then 'TTL_TAX'
       end dollar_name
      ,case
           when dollar_type.rowno = 1 then ttl_amt
           when dollar_type.rowno = 2 then ttl_dis
           when dollar_type.rowno = 3 then ttl_tax
       end amt
from retail_sale
    ,(
      select rownum rowno
      from dual
      connect by level <=3
     ) dollar_type
order by 1,2,3,5



select terminal_id
      ,sale_date
      ,case
           when dollar_type.rowno = 1 then 'TTL_AMT'
           when dollar_type.rowno = 2 then 'TTL_DIS'
           when dollar_type.rowno = 3 then 'TTL_TAX'
       end dollar_name
      ,case
           when dollar_type.rowno = 1 then base_ttl_amt
           when dollar_type.rowno = 2 then base_ttl_dis
           when dollar_type.rowno = 3 then base_ttl_tax
       end base_amt
      ,case
           when dollar_type.rowno = 1 then original_ttl_amt
           when dollar_type.rowno = 2 then original_ttl_dis
           when dollar_type.rowno = 3 then original_ttl_tax
       end usd_amt
from (
      select
             terminal_id
            ,sale_date
            ,sum(case when currency_category_code = 'BASE'     then ttl_amt end) base_ttl_amt
            ,sum(case when currency_category_code = 'BASE'     then ttl_dis end) base_ttl_dis
            ,sum(case when currency_category_code = 'BASE'     then ttl_tax end) base_ttl_tax
            ,sum(case when currency_category_code = 'ORIGINAL' then ttl_amt end) original_ttl_amt
            ,sum(case when currency_category_code = 'ORIGINAL' then ttl_dis end) original_ttl_dis
            ,sum(case when currency_category_code = 'ORIGINAL' then ttl_tax end) original_ttl_tax
      from retail_sale
      group by
             terminal_id
            ,sale_date
     )
    ,(
      select rownum rowno
      from dual
      connect by level <= 3
     ) dollar_type
order by 1,2,3

No comments:

Post a Comment

Drilldown from GL to Inventory Transactions, Inv to gl Link Query R12,Inventory to General ledger links, r12 ,Inv to gl drill down

Drilldown from GL to Inventory Transactions, Inv to gl Link Query R12,Inventory to General ledger links, r12 ,Inv to gl drill down Link bet...