Hi all I'm new to this board, before I always viewed anonymously but now I have a specific Problem I cannot find the answer.
I have two statements which do it individually very good
Statement 1: Select Sites with most Problems
WITH ordered_query AS
(SELECT debitorid, objectid, count(incident) as CTINCI
FROM ssrs_tblx_sla STSLA
WHERE Debitorid = :Debitor
group by debitorid, objectid
ORDER BY count(Incident) DESC, debitorid)
SELECT debitorid, objectid, CTINCI
FROM ordered_query
WHERE rownum <= 5
Statement 2: Most Errorcodes for a Site
WITH ordered_query AS
(SELECT debitorid, objectid, errorcode, count(incident) as CTINCI
FROM ssrs_tblx_sla STSLA
WHERE Debitorid = :Debitor
and objectid = :Objectid
group by debitorid, objectid, errorcode
ORDER BY count(Incident) DESC, debitorid)
SELECT debitorid, objectid, errorcode, CTINCI
FROM ordered_query
WHERE rownum <= 5
In both Statements I use oracle parameters in the where statement.
Now I want to comibe them and tried this by a LEFT JOIN Statement, but it seems I cannot use values of Statement 1 in the where clause in Statement 2.
GOAL: I want to get the Top 5 Sites with the highest amount Incidents and per site of them the Top 5 of hightst amount Incidents per errorcode
Anyone an Idea what I do wrong ? I'm a little bit confused
select T5S.Debitorid
, T5S.Objectid
, T5S.CTINCI as SiteTotal
, T5P.ERRORCODE
, T5P.CTINI as ERCTotal
FROM
(WITH ordered_query AS
(SELECT debitorid, objectid, count(incident) as CTINCI
FROM ssrs_tblx_sla STSLA
WHERE Debitorid = :Debitor
group by debitorid, objectid
ORDER BY count(Incident) DESC, debitorid)
SELECT debitorid, objectid, CTINCI
FROM ordered_query
WHERE rownum <= 5
) T5S
LEFT JOIN
(WITH ordered_query AS
(SELECT debitorid, objectid, errorcode, count(incident) as CTINCI
FROM ssrs_tblx_sla STSLA
WHERE Debitorid = T5S.Debitorid
and objectid = T5S.Objectid
group by debitorid, objectid, errorcode
ORDER BY count(Incident) DESC, debitorid)
SELECT debitorid, objectid, errorcode, CTINCI
FROM ordered_query
WHERE rownum <= 5
) T5P
ON T5S.Debitorid = T5P.DebitorId and T5S.objectid=T5P.objectid
The error code oracle give me is:
ORA-00904: "T5S"."OBJECTID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Fehler in Zeile: 20 Spalte: 56
To me, it looks as this (but I might be wrong; can't test it, I don't have your tables):
with
ordered_query as
(select debitorid, objectid, count(incident) as ctinci
from ssrs_tblx_sla stsla
where debitorid = :debitor
group by debitorid, objectid
order by count(incident) desc, debitorid
),
t5s as
(select debitorid, objectid, ctinci
from ordered_query
where rownum <= 5
),
ordered_query_2 as
(select debitorid, objectid, errorcode, count(incident) as ctinci
from ssrs_tblx_sla stsla join t5s on t5s.debitorid = stsla.debitorid
and t5s.objectid = stsla.objectid
group by debitorid, objectid, errorcode
order by count(incident) desc, debitorid)
),
t5p as
(select debitorid, objectid, errorcode, ctinci
from ordered_query_2
where rownum <= 5
)
--
select t5s.debitorid,
t5s.objectid,
t5s.ctinci as sitetotal,
t5p.errorcode,
t5p.ctini as erctotal,
from t5s left join t5p t5s.debitorid = t5p.debitorid and t5s.objectid=t5p.objectid;
Related
Background:
Every order is connected to a load. Each order can only be associated to one load.
Loads may be made up of multiple orders.
When an load is shipped it creates an attachment for each address associated with the load.
The problem: When two orders with the same address are on the same load only one of the orders gets an attachment. (3rd Party system limitation)
The goal: write a oracle query to return the orderid of each order missing attachments and return the attachment found on other orders within the same load where the addresses are the same.
I was able two write two separate oracle statements:
Identifies all orders missing attachments:
SELECT orderid,loadno, shiptoaddr1 FROM ORDERHDR WHERE SHIPDATE >= (select sysdate-9 from dual) and ORDERTYPE = 'O' AND ORDERSTATUS = '9' AND CUSTID <> 'TACO' AND ORDERID NOT IN (select orderid from orderattach where filePath like 'S:%APPLE%prod%ATTACH%')
Provides attachment if found on any order within the same load:
SELECT orderattach.filePath as attachment, ORDERHDR.shiptoaddr1 as address, ORDERHDR.loadno as loady
FROM orderattach, ORDERHDR
where orderattach.filePath like 'S:%APPLE%prod%ATTACH%'
and ORDERHDR.ORDERID = orderattach.ORDERID
and ORDERHDR.loadno in (select loadno from ORDERHDR where orderid in ( SELECT orderid FROM ORDERHDR WHERE SHIPDATE >= (select sysdate-9 from dual) and ORDERTYPE = 'O' AND ORDERSTATUS = '9' AND CUSTID <> 'TACO' AND ORDERID NOT IN (select orderid from orderattach where filePath like 'S:%APPLE%prod%ATTACH%')))
Help Needed: To combine the two select statements when the loadno and shiptoaddr1 are the equal.
Progress:
I attempted to write a join where both select statements are joined where the addresses and load numbers are equal:
select orderid, attachment
from (
SELECT orderid,loadno, shiptoaddr1 FROM ORDERHDR WHERE SHIPDATE >= (select sysdate-9 from dual) and ORDERTYPE = 'O' AND ORDERSTATUS = '9' AND CUSTID <> 'TACO' AND ORDERID NOT IN (select orderid from orderattach where filePath like 'S:%APPLE%prod%ATTACH%') )as sub1x
join
(
SELECT orderattach.filePath as attachment, ORDERHDR.shiptoaddr1 as address, ORDERHDR.loadno as loady
FROM orderattach, ORDERHDR
where orderattach.filePath like 'S:%APPLE%prod%ATTACH%'
and ORDERHDR.ORDERID = orderattach.ORDERID
and ORDERHDR.loadno in (select loadno from ORDERHDR where orderid in ( SELECT orderid FROM ORDERHDR WHERE SHIPDATE >= (select sysdate-9 from dual) and ORDERTYPE = 'O' AND ORDERSTATUS = '9' AND CUSTID <> 'TACO' AND ORDERID NOT IN (select orderid from orderattach where filePath like 'S:%APPLE%prod%ATTACH%')))
) sub2
where sub1.shiptoaddr1 = sub2.address and sub1.loadno = sub2.loady`
Result: I am receiving an Oracle error: ORA-00933: SQL command not properly ended.
Question: Should I be using more subqueries or another method to achieve this?
I have these two queries:
SELECT salesconsultant AS emp_names,
lotid AS lot_id,
communityid AS lot_community,
lotnumber AS lot_no,
phasenumber AS lot_phase,
clientid AS lot_buyer,
lotstatusid AS lot_status,
lotaddress AS address,
pkg AS pack_id,
pkg_abbr AS pack_abbre,
deposit AS depst,
currentprice AS contract_price,
salesconsultant AS agent_name FROM `iv_reports_all_wms`
WHERE is_active_report = 0
AND lotid <> ''
AND rescined = 0 AND region IN ("SL")
ORDER BY `dig` ASC,
`agreedate` ASC
AND
(SELECT
id,
NAME,
lastname AS l_name
FROM
`iv_employee`
WHERE id = 6588)
UNION
(SELECT
userId AS id,
firstname AS NAME,
lastname AS l_name
FROM
`ivhusersdeleted`
WHERE userId = 6588)
I want to avoid to run the second query in foreach after the first query.
i want to use the second one in the left join of first in laravel. Can anyone help
Tables needed -
Habits(conditionId, name)
Patient(patientId, name, gender, DoB, address, state,postcode, homePhone, businessPhone, maritalStatus, occupation, duration,unit, race, registrationDate , GPNo, NaturopathNo)
PatientMetabolic (functionNo, patientId, score)
The Question Is -
Question - Display the details of the patient (i.e. Name, Gender, Address, Postcode, DOB) who smokes and has the highest (most severe) total of metabolic functions.
(conditionid for smoke is H1 in Habit table)
(metabolic function are in patientbetabolic table functionNo)
(To find the highest most severe total of metabolic function we need to create a sum of score which tells who has the most metabolic functions)
My query -
SELECT *
FROM patient
where patientid IN (SELECT patientid,SUM(score) as totalscore
from PATIENTMETABOLIC
where patientid IN (SELECT patientid
from patienthabit
where conditionid = 'H1')
group by patientid
order by totalscore desc);
Error:
ORA-00907: missing right parenthesis
An alternative way to do this is by using joins.
select * from (select p.patientid,p.name,sum(pm.score) as total from patient p join patienthabit ph on p.patientid = ph.patientid
and ph.conditionid = 'H1' Left join patientmetabolic pm
on p.patientid = pm.patientid group by p.patientid,p.name order by 3 desc) where ROWNUM = 1;
Try this:
SELECT *
FROM PATIENT
WHERE PATIENTID = (SELECT PATIENTID
FROM (SELECT patientid, SUM(score)
from PATIENTMETABOLIC
where patientid IN (SELECT patientid
from patienthabit
where conditionid = 'H1')
group by patientid
order by SUM(score) desc)
WHERE ROWNUM = 1);
SQLFiddle here
Share and enjoy.
Since the first inner query returns patientid and totalscore, you can't use it as a list against IN operator.
The result of this query might be similar to that of your query:
SELECT p.*
FROM patient p JOIN
patienthabit ph ON p.patientid=ph.patientid
WHERE ph.conditionid='H1'
hi i am running the follwoing query to identify the duplicate records.
SELECT *
FROM unique2 P WHERE EXISTS(SELECT 1 FROM unique2 C
WHERE ( (C.surname) = (P.surname))
AND ( (C.postcode) = (P.postcode))
AND ((( (C.forename) IS NULL OR (P.forename) IS NULL)
AND (C.initials) = (P.initials))
OR (C.forename) = (P.forename))
AND ( (C.sex) = (P.sex)
OR (C.title) = (P.title))
AND (( (C.address1))=( (P.address1))
OR ( (C.address1))=( (P.address2))
OR ( (C.address2))=( (P.address1))
OR instr(C.address1_notrim, P.address1_notrim) > 0
OR instr(P.address1_notrim, C.address1_notrim) > 0)
AND C.rowid < P.rowid);
But with this query i can't identify the unique record id which is matched to the duplicate records. Is there a way to identify the
duplicates as well as the unique record id(my table has unique key) to which those duplicates are matched?
select id
from promolog
where surname, postcode, dob in (
select surname, postcode,dob
from (
select surname, postcode, dob, count(1)
from promolog
group by surname,postcode,dob
having count(1) > 1
)
)
You can also do this with analytic functions:
select id, num_of_ids, first_id, surname, postcode, dob
from (
select id,
count(*) over (partition by surname, postcode, dob) as num_of_ids,
first_value(id)
over (partition by surname, postcode, dob order by id) as first_id,
surname,
postcode,
dob
from promolog
)
where num_of_ids > 1;
Based on your update, I think you can just do a self-join, which you can make as complicated as you like:
select dup.*, master.id as duplicate_of
from promolog dup
join promolog master
on master.surname = dup.surname
and master.postcode = dup.postcode
and master.dob = dup.dob
... and <address checks etc. > ...
and master.rowid < dup.rowid;
But maybe I'm still missing something. As the name suggests, exists is for testing the existence of a matching record; if you want to retrieve any of the data from the matched record then you'll need to join to it at some point.
I'm getting an error in a query. This query is OK and returning data (the selects and rownums are for pagination):
select *
from (select a.*, rownum rnum
from (select id_edition, id_document, name, extension, creation_date, url,
(select inscription_date from edition_student_d0 where id_edition = 12345 and id_third = 12345) inscription_date
from upd_edition_doc_d0
where id_edition = 1071591
order by creation_date desc) a
where rownum <= 10 )
where rnum >= 1
Now I'm trying to include a "case when" and get the url only in some circumstances, so I make these modifications, including a case block:
select *
from (select a.*, rownum rnum
from (select id_edition, id_document, name, extension, creation_date,
(select inscription_date from edition_student_d0 where id_edition = 12345 and id_third = 12345) inscription_date,
case
when trunc(inscription_date + 90) <= trunc(sysdate) then null
else url
end url
from upd_edition_doc_d0
where id_edition = 1071591
order by creation_date desc) a
where rownum <= 10 )
where rnum >= 1
Oracle launches this error:
ORA-00904: "INSCRIPTION_DATE": invalid identifier
I suppose that's because I'm asking for inscription_date and using it at the same query level, but I don't know how to deal with this.
Also, how can I make what I'm trying to make? I mean, getting url only under a certain condition.
Anybody can help?
Thank you in advance.
You can't refer an alias in the same level of the query.
You can replace the subquery...
select *
from (select a.*, rownum rnum
from (select id_edition, id_document, name, extension, creation_date,
(select inscription_date from edition_student_d0 where id_edition = 12345 and id_third = 12345) inscription_date,
case when trunc((select inscription_date from edition_student_d0 where id_edition = 12345 and id_third = 12345) + 90) <= trunc(sysdate) then null
else url
end as url
from upd_edition_doc_d0
where id_edition = 1071591
order by creation_date desc) a
where rownum <= 10 )
where rnum >= 1
OR move the case one level up.
select *
from (select a.*,
case when trunc(inscription_date + 90) <= trunc(sysdate) then null
else url
end as url,
rownum rnum
from (select id_edition, id_document, name, extension, creation_date,
(select inscription_date from edition_student_d0 where id_edition = 12345 and id_third = 12345) inscription_date
from upd_edition_doc_d0
where id_edition = 1071591
order by creation_date desc) a
where rownum <= 10 )
where rnum >= 1