Link to home
Create AccountLog in
Avatar of FutureDBA-
FutureDBA-

asked on

Header / Detail in a single query.

I have this data here

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

Open in new window




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

Open in new window




any help is appreciated
Avatar of flow01
flow01
Flag of Netherlands image

with data
as
(
select 85      chain, 23026      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      451245      PO_Number      ,400            itemnumber, 5       qty ,1  flag from dual union
select 85      chain, 23026      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      451245      PO_Number      ,401            itemnumber, 3       qty ,1  flag from dual union
select 85      chain, 23026      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      451245      PO_Number      ,402            itemnumber, 8       qty ,1  flag from dual union
select 85      chain, 23026      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      451245      PO_Number      ,406            itemnumber, 3       qty ,1  flag from dual union
select 85      chain, 23026      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      451245      PO_Number      ,470            itemnumber, 2       qty ,1  flag from dual union
select 85      chain, 23026      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      451245      PO_Number      ,471            itemnumber, 1       qty ,1  flag from dual union
select 85      chain, 23026      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      451245      PO_Number      ,6050      itemnumber, 9       qty ,1  flag from dual union
select 85      chain, 23026      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      451245      PO_Number      ,6080      itemnumber, 13 qty ,      1 flag from dual union
select 85      chain, 23026      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      451245      PO_Number      ,6081      itemnumber, 15 qty ,      1 flag from dual union
select 85      chain, 23035      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      989898      PO_Number      ,400            itemnumber, 15       qty ,1  flag from dual union
select 85      chain, 23035      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      989898      PO_Number      ,401            itemnumber, 13       qty ,1  flag from dual union
select 85      chain, 23035      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      989898      PO_Number      ,402            itemnumber, 18       qty ,1  flag from dual union
select 85      chain, 23035      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      989898      PO_Number      ,406            itemnumber, 13       qty ,1  flag from dual union
select 85      chain, 23035      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      989898      PO_Number      ,470            itemnumber, 12       qty ,1  flag from dual union
select 85      chain, 23035      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      989898      PO_Number      ,471            itemnumber, 11       qty ,1  flag from dual union
select 85      chain, 23035      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      989898      PO_Number      ,6050      itemnumber, 19       qty ,1  flag from dual union
select 85      chain, 23035      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      989898      PO_Number      ,6080      itemnumber, 113 qty ,      1 flag from dual union
select 85      chain, 23035      customer,       to_date('20130702','yyyymmdd') deldate,      8282 route,      989898      PO_Number      ,6081      itemnumber, 115 qty ,      1 flag from dual
)
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')           ELSE '0' END deldate
, 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
/
Avatar of FutureDBA-
FutureDBA-

ASKER

I have that data in another table.  

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

Open in new window



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
may I ask why you are doing this?  in particular, what is "the next step"?
import the data to our ERP
ASKER CERTIFIED SOLUTION
Avatar of FutureDBA-
FutureDBA-

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
found elsewhere outside this thread