FutureDBA-
asked on
Header / Detail in a single query.
I have this data here
Which i need to represent in this format
any help is appreciated
Chain Customer DelDate Route PO_Number ItemNumber QTY FLAG
85 23026 20130702 8282 451245 400 5 1
85 23026 20130702 8282 451245 401 3 1
85 23026 20130702 8282 451245 402 8 1
85 23026 20130702 8282 451245 406 3 1
85 23026 20130702 8282 451245 470 2 1
85 23026 20130702 8282 451245 471 1 1
85 23026 20130702 8282 451245 6050 9 1
85 23026 20130702 8282 451245 6080 13 1
85 23026 20130702 8282 451245 6081 15 1
85 23035 20130702 8282 989898 400 5 1
85 23035 20130702 8282 989898 401 3 1
85 23035 20130702 8282 989898 402 8 1
85 23035 20130702 8282 989898 406 3 1
85 23035 20130702 8282 989898 470 2 1
85 23035 20130702 8282 989898 471 1 1
85 23035 20130702 8282 989898 6050 9 1
85 23035 20130702 8282 989898 6080 13 1
85 23035 20130702 8282 989898 6081 15 1
Which i need to represent in this format
Chain Customer DelDate Route PO_Number ItemNumber QTY FLAG
85 23026 20130702 8282 451245 0 0 0
0 0 0 0 0 400 5 1
0 0 0 0 0 401 3 1
0 0 0 0 0 402 8 1
0 0 0 0 0 406 3 1
0 0 0 0 0 470 2 1
0 0 0 0 0 471 1 1
0 0 0 0 0 6050 9 1
0 0 0 0 0 6080 13 1
0 0 0 0 0 6081 15 1
85 23035 20130702 8282 989898 0 0 0
0 0 0 0 0 400 5 1
0 0 0 0 0 401 3 1
0 0 0 0 0 402 8 1
0 0 0 0 0 406 3 1
0 0 0 0 0 470 2 1
0 0 0 0 0 471 1 1
0 0 0 0 0 6050 9 1
0 0 0 0 0 6080 13 1
0 0 0 0 0 6081 15 1
any help is appreciated
ASKER
I have that data in another table.
I rewrote your query to the following
But i am getting the following error
ORA-01790: expression must have same datatype as corresponding expression
01790. 00000 - "expression must have same datatype as corresponding expression"
*Cause:
*Action:
Error at Line: 13 Column: 24
I rewrote your query to the following
With m as (select
CHAIN, CUSTOMER, to_char(to_date(SHIP_DATE, 'MM-DD-YYYY'), 'yyyymmdd') SHIP_DATE, ROUTE, PO, ITEM, QTY, '1' as FLAG from ordimp)
select -- format the 0 values
CASE typeHD WHEN 'H' THEN to_char(chain) ELSE '0' END chain
, CASE typeHD WHEN 'H' THEN to_char(customer) ELSE '0' END customer
, CASE typeHD WHEN 'H' THEN to_char(ship_date,'yyyymmdd') ELSE '0' END deldate
, CASE typeHD WHEN 'H' THEN to_char(PO) ELSE '0' END PO_Number
, CASE typeHD WHEN 'H' THEN to_char(route) ELSE '0' END route
, item
, qty
from
(select 'D' typeHD, m.* from m
union -- create the header record
select 'H' typeHD, chain, customer, ship_date, route, po, sum(0) item, sum(0) qty, avg(1) flag from m
group by chain, customer, ship_date, route, po
) s
order by s.chain, s.customer, s.ship_date, s.route, s.po, s.typeHD desc , s.item
But i am getting the following error
ORA-01790: expression must have same datatype as corresponding expression
01790. 00000 - "expression must have same datatype as corresponding expression"
*Cause:
*Action:
Error at Line: 13 Column: 24
I assumed that item, qty and flag are numbers and ship_date was a date.
You changed the ship_date to a char : I should't do that but I don't think it is the reason for this error (i expect that one later).
This is probably caused by the flag : you made it a char:
either leave it out (and omit the avg(1) flag too)
or
change it to number > ..., 1 as FLAG from ordimp
or
change avg(1) flag to max('1') flag
You changed the ship_date to a char : I should't do that but I don't think it is the reason for this error (i expect that one later).
This is probably caused by the flag : you made it a char:
either leave it out (and omit the avg(1) flag too)
or
change it to number > ..., 1 as FLAG from ordimp
or
change avg(1) flag to max('1') flag
may I ask why you are doing this? in particular, what is "the next step"?
ASKER
import the data to our ERP
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
found elsewhere outside this thread
as
(
select 85 chain, 23026 customer, to_date('20130702','yyyymm
select 85 chain, 23026 customer, to_date('20130702','yyyymm
select 85 chain, 23026 customer, to_date('20130702','yyyymm
select 85 chain, 23026 customer, to_date('20130702','yyyymm
select 85 chain, 23026 customer, to_date('20130702','yyyymm
select 85 chain, 23026 customer, to_date('20130702','yyyymm
select 85 chain, 23026 customer, to_date('20130702','yyyymm
select 85 chain, 23026 customer, to_date('20130702','yyyymm
select 85 chain, 23026 customer, to_date('20130702','yyyymm
select 85 chain, 23035 customer, to_date('20130702','yyyymm
select 85 chain, 23035 customer, to_date('20130702','yyyymm
select 85 chain, 23035 customer, to_date('20130702','yyyymm
select 85 chain, 23035 customer, to_date('20130702','yyyymm
select 85 chain, 23035 customer, to_date('20130702','yyyymm
select 85 chain, 23035 customer, to_date('20130702','yyyymm
select 85 chain, 23035 customer, to_date('20130702','yyyymm
select 85 chain, 23035 customer, to_date('20130702','yyyymm
select 85 chain, 23035 customer, to_date('20130702','yyyymm
)
select -- format the 0 values
CASE typeHD WHEN 'H' THEN to_char(chain) ELSE '0' END chain
, CASE typeHD WHEN 'H' THEN to_char(customer) ELSE '0' END customer
, CASE typeHD WHEN 'H' THEN to_char(deldate,'yyyymmdd'
, CASE typeHD WHEN 'H' THEN to_char(PO_Number) ELSE '0' END PO_Number
, CASE typeHD WHEN 'H' THEN to_char(route) ELSE '0' END route
, itemnumber
, qty
from
(select 'D' typeHD, data.* from data
union -- create the header record
select 'H' typeHD, chain, customer, deldate, route, po_number, sum(0) itemnumber, sum(0) qty, avg(1) flag from data
group by chain, customer, deldate, route, po_number
) s
order by s.chain, s.customer, s.deldate, s.route, s.po_number, s.typeHD desc , s.itemnumber
/