SQL Server 2008 Specified column was specified multiple times - sql

The below code throws the following exception. How can I fix this?
Msg 8156, Level 16, State 1, Line 17
The column 'id' was specified multiple times for 'QISproduct'.
Query:
SELECT
g.artcode
, sum(g.aantal)
, i.class_01
, i.Isstockitem
FROM
gbkmut AS g
INNER JOIN
items AS i ON i.itemcode = g.artcode
INNER JOIN
(SELECT
QISP.id
, QISprocess.nml
, QISeventlog.id
, QISeventlog.dtsample
, QISproduct.nms
, QISbatchlog.nm
, QIStestlog.idvariable
, QIStestlog.no
, QISshortnote.ds
, gewicht = CASE QIStestlog.IDvariable
WHEN '139'
THEN QIStestlog.no
END
, aantal = CASE QIStestlog.IDvariable
WHEN '234'
THEN QIStestlog.no
END
, siloleeg = CASE QIStestlog.idvariable
WHEN '23'
THEN CASE QIStestlog.no
WHEN '10'
THEN 'Ja'
ELSE 'Nee'
END
END
, QISvariable.nml
, gl.nm
, QISprocess.id
FROM
QIC.Vobra_new2.dbo.production AS QISP
INNER JOIN
QIC.Vobra_new2.dbo.process AS QISprocess ON QISP.idprocess = QISProcess.id
INNER JOIN
QIC.Vobra_new2.dbo.product AS QISproduct ON QISP.idproduct = QISproduct.id
INNER JOIN
QIC.Vobra_new2.dbo.batchlog AS QISbatchlog ON QISP.idbatch = QISbatchlog.id
INNER JOIN
QIC.Vobra_new2.dbo.eventlog AS QISeventlog ON QISeventlog.idproduction = QISP.id
AND QISeventlog.idbatch = QISbatchlog.id
INNER JOIN
QIC.Vobra_new2.dbo.testlog AS QIStestlog ON QIStestlog.idevent = QISeventlog.id
LEFT OUTER JOIN
QIC.Vobra_new2.dbo.shortnote AS QISshortnote ON QISshortnote.id = QIStestlog.no
AND QIStestlog.idvariable = '144'
INNER JOIN
QIC.Vobra_new2.dbo.variable AS QISvariable ON QISvariable.id = QIStestlog.idvariable
LEFT OUTER JOIN
QIC.Vobra_new2.dbo.vvarxproc AS vvp ON vvp.idvariable = QISvariable.id
AND vvp.idprocess = QISP.idprocess
LEFT OUTER JOIN
QIC.Vobra_new2.dbo.attribute AS QISattribute ON QISattribute.id = vvp.idattribute
LEFT OUTER JOIN
QIC.Vobra_new2.dbo.grade AS QISgrade ON QISgrade.id = QISattribute.idgrade
LEFT OUTER JOIN
QIC.Vobra_new2.dbo.gradelevel AS gl ON gl.idgrade = QISattribute.idgrade
AND gl.nlevel = QIStestlog.no
WHERE
QISbatchlog.nm NOT LIKE 'V%'
AND QISP.dtstart > '2017-01-01'
AND QISP.dtstart < '2017-01-19'
AND QISP.idprocess IN ('12', '13', '14', '15', '16', '17', '18', '41')
AND QIStestlog.idvariable IN ('234', '139', '128')
) QISproduct ON g.artcode = QISproduct.nms
WHERE
g.bkjrcode > '2015'
AND g.reknr IN (3000, 3010, 3020)
AND g.aantal > 0
AND g.warehouse IN ('1', '9')
AND g.datum >= '2017-01-01'
AND g.oorsprong = 'R'
AND g.kstplcode <> 'VPR'
GROUP BY
g.artcode, i.Class_01, i.IsStockItem

The computed query aliased to QISProduct contains id column from two tables i.e. QISEventLog and QISProcess. So rename those columns to different names. Updated query
SELECT g.artcode
,sum(g.aantal)
,i.class_01
,i.Isstockitem
FROM gbkmut AS g
INNER JOIN items AS i ON i.itemcode = g.artcode
INNER JOIN (
SELECT QISP.id
,QISprocess.nml AS Processnml
,QISeventlog.id AS EventLogId
,QISeventlog.dtsample
,QISproduct.nms
,QISbatchlog.nm AS batchnm
,QIStestlog.idvariable
,QIStestlog.no
,QISshortnote.ds
,gewicht = CASE QIStestlog.IDvariable
WHEN '139'
THEN QIStestlog.no
END
,aantal = CASE QIStestlog.IDvariable
WHEN '234'
THEN QIStestlog.no
END
,siloleeg = CASE QIStestlog.idvariable
WHEN '23'
THEN CASE QIStestlog.no
WHEN '10'
THEN 'Ja'
ELSE 'Nee'
END
END
,QISvariable.nml variablenml
,gl.nm AS glnm
,QISprocess.id AS ProcessId
FROM QIC.Vobra_new2.dbo.production AS QISP
INNER JOIN QIC.Vobra_new2.dbo.process AS QISprocess ON QISP.idprocess = QISProcess.id
INNER JOIN QIC.Vobra_new2.dbo.product AS QISproduct ON QISP.idproduct = QISproduct.id
INNER JOIN QIC.Vobra_new2.dbo.batchlog AS QISbatchlog ON QISP.idbatch = QISbatchlog.id
INNER JOIN QIC.Vobra_new2.dbo.eventlog AS QISeventlog ON QISeventlog.idproduction = QISP.id
AND QISeventlog.idbatch = QISbatchlog.id
INNER JOIN QIC.Vobra_new2.dbo.testlog AS QIStestlog ON QIStestlog.idevent = QISeventlog.id
LEFT JOIN QIC.Vobra_new2.dbo.shortnote AS QISshortnote ON QISshortnote.id = QIStestlog.no
AND QIStestlog.idvariable = '144'
INNER JOIN QIC.Vobra_new2.dbo.variable AS QISvariable ON QISvariable.id = QIStestlog.idvariable
LEFT JOIN QIC.Vobra_new2.dbo.vvarxproc AS vvp ON vvp.idvariable = QISvariable.id
AND vvp.idprocess = QISP.idprocess
LEFT JOIN QIC.Vobra_new2.dbo.attribute AS QISattribute ON QISattribute.id = vvp.idattribute
LEFT JOIN QIC.Vobra_new2.dbo.grade AS QISgrade ON QISgrade.id = QISattribute.idgrade
LEFT JOIN QIC.Vobra_new2.dbo.gradelevel AS gl ON gl.idgrade = QISattribute.idgrade
AND gl.nlevel = QIStestlog.no
WHERE QISbatchlog.nm NOT LIKE 'V%'
AND QISP.dtstart > '2017-01-01'
AND QISP.dtstart < '2017-01-19'
AND QISP.idprocess IN (
'12'
,'13'
,'14'
,'15'
,'16'
,'17'
,'18'
,'41'
)
AND QIStestlog.idvariable IN (
'234'
,'139'
,'128'
)
) QISproduct ON g.artcode = QISproduct.nms
WHERE g.bkjrcode > '2015'
AND g.reknr IN (
3000
,3010
,3020
)
AND g.aantal > 0
AND g.warehouse IN (
'1'
,'9'
)
AND g.datum >= '2017-01-01'
AND g.oorsprong = 'R'
AND g.kstplcode <> 'VPR'
GROUP BY g.artcode
,i.Class_01
,i.IsStockItem

, QISbatchlog.nm
And
, gl.nm
Have same column name
You can add as to change colum name
, gl.nm as col1

You have multiple issues in your query. You are populating following columns with same column name in your inner query. Use Unique name using AS alias in inner query.
QISP.id
QISeventlog.id
QISprocess.id
gl.nm
QISbatchlog.nm
QISprocess.nml
QISvariable.nml
It is mandatory to have unique column name return by select list in sql.

Related

Why does the subquery throw errors in a group by query?

Ok, I will try a 2nd attempt because the first one was not that intelligent
My Query:
SELECT distinct kg.datum,
sum(case when leis.code = 'Oph3001' then leis.anzahl END) as leis3001,
sum(case when leis.code = 'Oph3003' then leis.anzahl END) as leis3003,
(select nvl((select to_date(KG2.KURZTEXT, 'dd.mm.yyyy')from kg_eintraege kg2 where kg2.kgtitel_nr = 1003350007 and kg.fall_nr = kg2.fall_nr and kg.patient_nr = kg2.patient_nr and kg.kg_id = kg2.kontext),'') from dual) as real_datum
FROM kg_eintraege kg
INNER JOIN aufenthalte a
ON kg.patient_nr = a.patient_nr
and kg.fall_nr = a.fall_nr
INNER JOIN MF_LEIS_DIAG_OP_MD leis
on leis.aufenthalte_nr = a.nr
group by kg.datum, kg.kg_id
order by /*real_datum*/ kg.datum desc
The following subquery is causing the problem:
(select nvl((select to_date(KG2.KURZTEXT, 'dd.mm.yyyy')from kg_eintraege kg2 where kg2.kgtitel_nr = 1003350007 and kg.fall_nr = kg2.fall_nr and kg.patient_nr = kg2.patient_nr and kg.kg_id = kg2.kontext),'') from dual)
Puting it into the group by expression throws the ORA-22818 error ("subquery not allowed here").
Not puting it throws the ORA-00979 error ("not a group by expression").
Can someone help me?
You could use an outer join instead; something like:
SELECT distinct kg.datum,
sum(case when leis.code = 'Oph3001' then leis.anzahl END) as leis3001,
sum(case when leis.code = 'Oph3003' then leis.anzahl END) as leis3003,
to_date(KG2.KURZTEXT, 'dd.mm.yyyy') as real_datum
FROM kg_eintraege kg
INNER JOIN aufenthalte a
ON kg.patient_nr = a.patient_nr
and kg.fall_nr = a.fall_nr
INNER JOIN MF_LEIS_DIAG_OP_MD leis
on leis.aufenthalte_nr = a.nr
LEFT OUTER JOIN kg_eintraege kg2
on kg2.kgtitel_nr = 1003350007
and kg.fall_nr = kg2.fall_nr
and kg.patient_nr = kg2.patient_nr
and kg.kg_id = kg2.kontext
group by kg.datum, kg.kg_id, to_date(KG2.KURZTEXT, 'dd.mm.yyyy')
order by /*real_datum*/ kg.datum desc
you can use a subquery
select datum,
sum(case when code = 'Oph3001' then anzahl END) as leis3001,
sum(case when code = 'Oph3003' then anzahl END) as leis3003,
real_datum
from (
SELECT distinct kg.datum,
leis.code,
leis.anzahl,
(select nvl((select to_date(KG2.KURZTEXT, 'dd.mm.yyyy')from kg_eintraege kg2 where kg2.kgtitel_nr = 1003350007 and kg.fall_nr = kg2.fall_nr and kg.patient_nr = kg2.patient_nr and kg.kg_id = kg2.kontext),'') from dual) as real_datum
FROM kg_eintraege kg
INNER JOIN aufenthalte a ON kg.patient_nr = a.patient_nr
and kg.fall_nr = a.fall_nr
INNER JOIN MF_LEIS_DIAG_OP_MD leis on leis.aufenthalte_nr = a.nr
)
group by datum, kg_id, real_datum
order by /*real_datum*/ kg.datum desc

ORA-01799: a column may not be outer-joined to a subquery

hi good people please find the code below is running in db2 but fails in oracle.
select * from
(select distinct e.time_stamp,e.applicationid, e.processname,
e.stage, e.initiatingsource, e.status, e.start_time, i.consultant,
g.cifnumber, g.applicantfirstname, g.applicantlastname,
case when e.branch is not null
then e.branch
else case when g.branch is not null
then g.branch
else i.branch
end
end as branch
from (select c.time_stamp, b.applicationid, b.processname,
b.stage, b.branch, b.initiatingsource,
case when d.status is null
then c.status
else d.status
end as status,
c.time_stamp as START_TIME,
case when d.time_stamp is not null
then d.time_stamp
else current_timestamp
ens as END_TIME
from (select distinct f.applicationid, f.branch, f.initiatingsource, f.processname,
case when f.stage in ('START''END')
then 'APPLICATION'
else f.stage, f.stagecounter
from processmetric f) b
left join processmetric c on b.applicationid = c.applicationid and b.processname = c.processname and (b.stage = c.stage or (b.stage = 'APPLICATION' and c.stage = 'START')) and b.stagecounter = c.stagecounter and c.phase = 'START'
left join processmetric d on b.applicationid = d.applicationid and b.processname = d.processname and (b.stage = d.stage or (b.stage = 'APPLICATION' and d.stage = 'END')) and b.stagecounter = d.stagecounter and d.phase ='END')e
left join applicationcustomerdata g on g.applicationid = e.applicationid
and g.time_stamp in (select max(x.time_stamp)
from applicationcustomerdata x
where x.applicationid = g.applicationid
)
left join applicationdata i on i.applicationid = e.applicationid
and i.time_stamp in (select max(z.time_stamp)
from applicationdata z
where z.applicationid = i.applicationid
)
order by e.start_time
) a
where a.start_time is not null and a.stage not in ('APPLICATION') and a.status not in ('COMPLETE' , 'COMPLETED' , 'CANCEL', 'FRAUD' , 'DECLINE') and a.stage = 'VERIFICATION';
Oracle don't allow to make outer join with subquery. Following 2 joins are problematic ones:
left join applicationcustomerdata g on g.applicationid = e.applicationid
and g.time_stamp in (select max(x.time_stamp)
from applicationcustomerdata x
where x.applicationid = g.applicationid
)
left join applicationdata i on i.applicationid = e.applicationid
and i.time_stamp in (select max(z.time_stamp)
from applicationdata z
where z.applicationid = i.applicationid
)
You need to rewrite statement (if you need this all in one SQL) or write some PL/SQL loops through data.

Oracle SQL - connecting 2 dates

I have the following code.
It has 2 dates:
1.
(case
when not trunc(iv.dated) is null then trunc(iv.dated) else trunc(iv1.dated)
end) date_stock_received
2.
trunc(dh.actshpdate) SHIP_DATE
Is there a way to join the dates so they show in one column?
select unique li.catnr, li.av_part_no,
(select sum(pl.qty_onhand) from part_loc pl where li.av_part_no = pl.part_no) qty_onhand,
(case
when not trunc(iv.dated) is null then trunc(iv.dated) else trunc(iv1.dated)
end) date_stock_received,
(case
when not sum(iv.quantity) is null then sum(iv.quantity) else sum(iv1.quantity)
end) qty_received,
dp.delqty, od.ord_extordnr, trunc(dh.actshpdate) SHIP_DATE
from leos_item li
LEFT JOIN scm_packtyp sp
ON li.packtyp = sp.packtyp
LEFT JOIN invtran_view_oes iv
ON li.av_part_no = iv.part_no
and (iv.transaction = 'NREC' and iv.location_no = ' RETURNS W')
LEFT JOIN invtran_view_oes iv1
on li.av_part_no = iv1.part_no
and (iv1.transaction = 'CORR+' and iv1.remark like 'STOCK FROM SP PALLET%')
LEFT JOIN oes_delsegview od
ON od.catnr = li.catnr
and od.prodtyp = li.prodtyp
and od.packtyp = li.packtyp
LEFT JOIN oes_dpos dp
ON od.ordnr = dp.ordnr
and od.posnr = dp.posnr
and od.segnr = dp.segnr
LEFT JOIN oes_dhead dh
on dp.dheadnr = dh.dheadnr
and dh.shpfromloc = 'W'
where li.cunr = '816900'
and substr(li.catnr,1,5) in ('RGMCD','RGJCD')
and li.item_type = 'FP'
and li.catnr = 'RGJCD221'
group by li.catnr, li.av_part_no, iv.dated, iv.quantity, iv1.dated, iv1.quantity, dp.delqty,
dp.ordnr, dp.posnr, dp.segnr, od.ord_extordnr, dh.actshpdate
order by li.av_part_no
Current result is ...
... what I would like to see is ...
Is this possible ?
The coalesce function might be what you want.
select trunc(coalesce(iv.dated, iv1.dated, dh.actshpdate)) theDateYouMightWant

CURSOR is not displaying data/result set

I've tried everything I could and i still can't get my cursor to display data. here is my code:
DECLARE
CURSOR SL_Cur IS
select *
from (
select
cdav.bank_id,
ent.bank_desc Bank_Description,
cdav.sol_id,
sol.sol_desc SOL_Description,
cdav.gl_sub_head_code GLSH_Code,
decode(cdav.gl_sub_head_code,
'10301',1,
'10403',2, '60403',2, '10501',2, '60501',2, '10502',2,
'10503',2, '10504',2, '10505',2, '10507',2, '10509',2,
'60509',2, '10511',2, '10518',2, '60518',2, '10523',2,
'60523',2, '10551',2, '10552',2, '10553',2, '10554',2,
'10555',2, '10557',2, '10559',2, '10561',2, '10568',2,
'10573',2,
'12336',3, '62336',3, '10401',3, '60402',3, 4 ) GLSH_SET ,
gsh.gl_sub_head_desc GLSH_Name,
case
when (cast(substr(cdav.gl_sub_head_code,0,1) as int) >= 1
and cast(substr(cdav.gl_sub_head_code,0,1) as int) <= 5)
then 'R'
when cast(substr(cdav.gl_sub_head_code,0,1) as int) = 0
or (cast(substr(cdav.gl_sub_head_code,0,1) as int) >= 6
and cast(substr(cdav.gl_sub_head_code,0,1) as int) <= 9)
then 'F'
end book_type,
gam.foracid account_number,
gam.acct_name,
cdav.tran_crncy_code Tran_Currency,
cdav.value_date,
cdav.tran_date Transaction_Date,
cdav.gl_date,
cdav.tran_particular,
rank() over(partition by gam.foracid order by eab.eod_date desc) eod_date_rank,
eab.eod_date,
case when
(select tran_date_bal from tbaadm.eab
where eab.eod_date = (select max(eab.eod_date) from tbaadm.eab
where cdav.acid = eab.acid and eab.eod_date < '28-MAY-2013')
and cdav.acid = eab.acid
and cdav.bank_id = eab.bank_id) is not null
then (select tran_date_bal from tbaadm.eab
where eab.eod_date = (select max(eab.eod_date) from tbaadm.eab
where cdav.acid = eab.acid and eab.eod_date < '28-MAY-2013')
and cdav.acid = eab.acid
and cdav.bank_id = eab.bank_id)
else 0
end beg_tran_date_bal,
(select tran_date_bal from tbaadm.eab eab
where eod_date = (select max(eab.eod_date) from tbaadm.eab eab
where cdav.acid = eab.acid and eab.eod_date <= '28-MAY-2013')
and cdav.acid = eab.acid
and cdav.bank_id = eab.bank_id) end_tran_date_bal,
ott.ref_num OAP_Ref_No,
trim(cdav.tran_id) Transaction_ID,
--cdav.dth_init_sol_id Initiating_SOL_ID,
'PCC_Code',
cdav.tran_rmks Tran_Remarks,
case
when (cdav.part_tran_type = 'D')
then (cdav.tran_amt)
end dr_amount,
case
when (cdav.part_tran_type = 'C')
then (cdav.tran_amt)
end cr_amount
from tbaadm.ctd_dtd_acli_view cdav
left outer join tbaadm.gam
on cdav.bank_id = gam.bank_id and cdav.acid = gam.acid
left outer join tbaadm.gsh
on gam.bank_id = gsh.bank_id and gam.sol_id = gsh.sol_id
and gam.gl_sub_head_code = gsh.gl_sub_head_code
and gam.acct_crncy_code = gsh.crncy_code
left outer join tbaadm.sol
on cdav.bank_id = sol.bank_id and cdav.sol_id = sol.sol_id
left outer join tbaadm.eab
on gam.bank_id = eab.bank_id and gam.acid = eab.acid
left outer join tbaadm.cnc
on gam.bank_id = cnc.bank_id and gam.acct_crncy_code = cnc.crncy_code
left outer join crmuser.end ent
on cdav.bank_id = ent.bank_id
left outer join tbaadm.gct
on cdav.bank_id = gct.bank_id
left outer join tbaadm.ott
on cdav.tran_id = ott.tran_id and cdav.tran_date = ott.tran_date
and cdav.part_tran_srl_num = ott.part_tran_srl_num
and cdav.bank_id = ott.bank_id and cdav.acid = ott.acid
where
gam.acct_ownership = 'O'
and cdav.bank_id = 'CBC01'
and cdav.gl_date = '28-MAY-2013'
and (gam.gl_sub_head_code in ('10301','10403','60403',
'10501','60501','10502','10503','10504','10505',
'10507','10509','60509','10511','10518','60518',
'10523','60523','10551','10552','10553','10554',
'10555','10557','10559','10561','10568','10573',
'12336','62336','10401','60402')
or gam.acct_classification_flg in ('I','E')
)
and trim(cdav.del_flg) in ('N', null)
and trim(gam.del_flg) in ('N', null)
and trim(gsh.del_flg) in ('N', null)
and trim(sol.del_flg) in ('N', null)
and trim(cnc.del_flg) in ('N', null)
and trim(gct.del_flg) in ('N', null)
)
where
eod_date_rank = 1
and GLSH_SET = 1
order by
bank_id,
sol_id,
tran_currency,
glsh_code,
book_type desc,
account_number,
transaction_date;
in_sl_rec SL_Cur%ROWTYPE;
BEGIN
open SL_Cur;
LOOP
FETCH SL_Cur INTO in_sl_rec;
exit when SL_Cur%notfound;
END LOOP;
close SL_Cur;
END;
At first, I just need to make the CURSOR display the result set that I want to see. Next, I want to make the cursor into a sort of FOR LOOP, because there would be input parameters involved when executing the code in iReport, mainly a date range.
Cursors don't just "display" when you execute them - you have to write a little code to do that. Try adding the following lines in your loop after the exit when SL_Cur%notfound; and just before the END LOOP;
DBMS_OUTPUT.PUT_LINE('BANK_ID=' || in_sl_rec.BANK_ID ||
'Bank_Description=' || in_sl_rec.Bank_Description ||
'sol_id=' || in_sl_rec.sol_id ||
'SOL_Description=' || in_sl_rec.SOL_Description);
You can add more fields as you need to.
Share and enjoy.

How to change this 'Not In' Query to Left Join query

I've been trying to change this query. I don't want to use 'Not In' in this query. Can anybody help me how to change this query to left join query?
SELECT t.date,t.ticket,t.weight,t.Count, td.description
FROM tblticket t inner join tblticketdetails td on t.ticket = td.ticket
WHERE t.ticket NOT IN (SELECT r.Original_ticket from tblRenew R where isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid')
AND RTRIM(LTRIM(t.status)) = 'OPEN'
AND td.Type = 62
AND t.weight/t.Count >= 1000
AND t.date BETWEEN '2011-12-31' AND '2013-01-17'
Providing that you don't have multiple records in tblRenew for any ticket:
select
t.date, t.ticket, t.weight, t.Count, td.description
from
tblticket t
inner join tblticketdetails td on t.ticket = td.ticket
left join tblRenew R on isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid' and r.Original_ticket = t.ticket
where
r.Original_ticket is not null
and RTRIM(LTRIM(t.status)) = 'OPEN'
and td.Type = 62
and t.weight/t.Count >= 1000
and t.date BETWEEN '2011-12-31' AND '2013-01-17'
SELECT t.date, t.ticket,t.weight, t.Count, td.description
FROM tblticket t inner join
tblticketdetails td
on t.ticket = td.ticket left outer join
(SELECT r.Original_ticket
from tblRenew R
where isnull(r.new_ticket,'') = '' and
r.transaction_status = 'valid'
) v
on t.ticket = v.Original_ticket
WHERE t.ticket NOT IN (SELECT r.Original_ticket from tblRenew R where isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid')
AND RTRIM(LTRIM(t.status)) = 'OPEN'
AND td.Type = 62
AND t.weight/t.Count >= 1000
AND t.date BETWEEN '2011-12-31' AND '2013-01-17'
and v.original_tiket is null