Oracle pivot with dynamic data

2019-04-16 23:31发布

I am new to the oracle database and I am trying to use PIVOT to convert rows into columns. I have following tables..

table 1
solid       solname
--------------
1        xxxxxx
2        yyyyyyy
table2
id      name           abbrv 
----------------------------------
1        test db          tdb
2        Prdocuiton db     pdb

table3
id     solId
-------------
1   1
1   2
1   3
1   4
1   5
1   7
1   8
1   9
1   22
1   23
1   24
1   25
2   26
2   27
1   28
1   29
1   32
1   33
1   34
1   35
1   36
1   37
3   38
1   39
1   40
1   43
1   44

table 3 is mapper table for table 1 and table 3.

I need to create a view with the columns in table2 and extra column for each solname's. So the view looks like

id      name           abbrv   xxxxxxx    yyyyyyy
--------------------------------------------------

So is there a way to do this using PIVOT in oracle database?

3条回答
一纸荒年 Trace。
2楼-- · 2019-04-16 23:37
TableName - **tblItem**
Id  ItemName    RecipeName  Quantity
1   Sugar       mutter paneer   200
2   Tea     mutter paneer   100
3   Tomato      mutter paneer   500
4   Onion       mutter paneer   300
5   Ginger      mutter paneer   300
6   Capsicum    mutter paneer   300
7   Sugar       mutter paneer   200
8   Tea     mutter paneer   100
9   Onion       mutter paneer   500
10  Sugar       mutter paneer   200

V_VALUES varchar2(4000);
sql_query varchar2(4000);

SELECT
LISTAGG(''''||ITEMNAME||'''',',')WITHIN GROUP (ORDER BY ITEMNAME)as ITEMNAME
INTO V_LIST
FROM(SELECT DISTINCT ITEMNAME FROM tblItem);

sql_query : = 'SELECT * FROM (
              SELECT ItemName,RecipeName,Sum(Quantity) as Quantity from tblItem group by ItemName,RecipeName)
              PIVOT ( 
        sum(Quantity) for ItemName in (' ||V_LIST|| ') 
        )';

  OPEN p_cursor
     FOR sql_query;
查看更多
Summer. ? 凉城
3楼-- · 2019-04-16 23:40

For Dynamic SQL Pivoting you need to do something similar :

create or replace view sol_view
as
select 
    t1.solname, 
    t2.name, 
    count(t3.abbrv),
from 
    table1 t1, 
    table2 t2, 
    table3 t3
where 
    t1.solid = t3.solid 
    and t2.id = t3.id
group by
    t1.solname,
    t3.name

select * from table( pivot('select * from sol_view') )

Caveat: I have never tried this but understood the logic from here: http://technology.amis.nl/2006/05/24/dynamic-sql-pivoting-stealing-antons-thunder/

For Static SQL Pivoting, try something roughly along these lines. Never tried or tested though:

with pivot_data as (
    select t1.solname, t2.name, t3.abbrv
from table1 t1, table2 t2, table3 t3
where t1.solid = t3.solid 
and t2.id = t3.id
)
select * 
from pivot_data
pivot ( 
    count(abbrv) 
    for solname 
    in ('xxxxxx','yyyyyyy') 
);
查看更多
Emotional °昔
4楼-- · 2019-04-17 00:00

It was not really defined what you want to store in the xxxx and yyyy columns, maybe 1/blank, Y/N, ... ? However, your query might look close to something like this:

SELECT * FROM (
  SELECT *
  FROM table3 t3
  JOIN table2 t2 USING (id)
  JOIN table1 t1 USING (solid)
) PIVOT (
  COUNT(*) FOR (solname) IN (
    ('xxx') AS "XXX",
    ('yyy') AS "YYY"
  )
)

You can find more information and additional references on My Blog

查看更多
登录 后发表回答