I have a SQL query
Select
temp1.domainname,
temp1.employeeid,
temp1.alletec_plantcode,
temp1.name,
temp1.alletec_customerengineer1name,
temp1.alletec_cityname,
temp1.alletec_regionname,
temp1.alletec_ce1name,
temp1.alletec_casecalltypename,
count(temp1.alletec_ce1name) as TOTALMIFASSIGN ,
(select count( MIF.alletec_ce1name) from temp1 where temp1.alletec_casecalltypename='aa') as CMMIFASSIGN
from (
Select
User1.domainname,
User1.employeeid,
User1.alletec_plantcode,
BU.name,
MIF.alletec_customerengineer1name,
MIF.alletec_cityname,
MIF.alletec_regionname,
MIF.alletec_ce1name,
Incident.alletec_casecalltypename
From
FilteredSystemUser As User1 Inner Join FilteredBusinessUnit As BU ON User1.businessunitid=BU.businessunitid
Inner join Filteredalletec_mif As MIF ON MIF.alletec_ce1=User1.systemuserid
Inner join FilteredIncident As Incident On Incident.alletec_serialnomif=MIF.alletec_mifid
where MIF.alletec_ce1name='Amit Chauhan' AND MIF.alletec_cityname='Gurgaon' and MIF.alletec_regionname='North' and Incident.statecodename='Resolved'
group by User1.domainname,
User1.employeeid,
User1.alletec_plantcode,
BU.name,
MIF.alletec_cityname,
MIF.alletec_regionname,
MIF.alletec_ce1name,
MIF.alletec_customerengineer1name,
Incident.alletec_casecalltypename
) As temp1
group by
temp1.domainname,
temp1.employeeid,
temp1.alletec_plantcode,
temp1.name,
temp1.alletec_customerengineer1name,
temp1.alletec_cityname,
temp1.alletec_regionname,
temp1.alletec_ce1name,
temp1.alletec_casecalltypename
The particular query above showing the temp1 as Invalid Object in the count query as i am require to place further filtration on it. Cant we use the above query in Aggregate function. Kindly suggest the an alternative to it.
Thanks.
The problem seem to be that the table alias temp1 isn't available when the query processor tries to resolve it. One solution that should work would be to wrap the query in a common table expression (cte). I believe this will work.
Try this:
;with temp1 (
domainname, employeeid,
alletec_plantcode, name,
alletec_customerengineer1name,
alletec_cityname, alletec_regionname,
alletec_ce1name, alletec_casecalltypename
)
as (
Select
User1.domainname,
User1.employeeid,
User1.alletec_plantcode,
BU.name,
MIF.alletec_customerengineer1name,
MIF.alletec_cityname,
MIF.alletec_regionname,
MIF.alletec_ce1name,
Incident.alletec_casecalltypename
From
FilteredSystemUser As User1 Inner Join FilteredBusinessUnit As BU ON User1.businessunitid=BU.businessunitid
Inner join Filteredalletec_mif As MIF ON MIF.alletec_ce1=User1.systemuserid
Inner join FilteredIncident As Incident On Incident.alletec_serialnomif=MIF.alletec_mifid
where MIF.alletec_ce1name='Amit Chauhan' AND MIF.alletec_cityname='Gurgaon' and MIF.alletec_regionname='North' and Incident.statecodename='Resolved'
group by User1.domainname,
User1.employeeid,
User1.alletec_plantcode,
BU.name,
MIF.alletec_cityname,
MIF.alletec_regionname,
MIF.alletec_ce1name,
MIF.alletec_customerengineer1name,
Incident.alletec_casecalltypename
)
Select
temp1.domainname,
temp1.employeeid,
temp1.alletec_plantcode,
temp1.name,
temp1.alletec_customerengineer1name,
temp1.alletec_cityname,
temp1.alletec_regionname,
temp1.alletec_ce1name,
temp1.alletec_casecalltypename,
(select count(temp1.alletec_ce1name) from temp1) as TOTALMIFASSIGN
from temp1
Related
EDIT:
The result supposed to be like this:
desired result
I have this query:
SELECT DISTINCT mitarbeiter.mitarbnr, mitarbeiter.login, mitarbeiter.name1, mitarbeiter.name2
FROM vertragspos
left join vertrag_ek_vk_zuord ON vertragspos.id = vertrag_ek_vk_zuord.ek_vertragspos_id
left join mitarbeiter ON vertrag_ek_vk_zuord.anlage_mitarbnr = mitarbeiter.mitarbnr
left join vertragskopf ON vertragskopf.id = vertragspos.vertrag_id
left join
(
SELECT wkurse.*, fremdwaehrung.wsymbol
FROM wkurse
INNER join
(
SELECT lfdnr, Max(tag) AS maxTag
FROM wkurse
WHERE tag < SYSDATE
GROUP BY lfdnr
) t1
ON wkurse.lfdnr = t1.lfdnr AND wkurse.Tag = t1.maxTag
INNER JOIN fremdwaehrung ON wkurse.lfdnr = fremdwaehrung.lfdnr
) wkurse ON vertragskopf.blfdwaehrung = wkurse.lfdnr
left join
(
SELECT vertrag_ID, Sum (preis) preis, Sum (menge) menge, Sum (preis * menge / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
GROUP BY vertrag_ID
) s ON vertragskopf.id = s.vertrag_id
But I always get an error on line 21 Pos 145:
ORA-00904 WKURSE.KURS invalid identifier
The WKURSE table is supposed be joined already above, but why do I still get error?
How can I do join with all these tables?
I need to join all these tables:
Mitarbeiter, Vertragspos, vertrag_ek_vk_zuord, wkurse, fremdwaehrung, vertragskopf.
What is the right syntax? I'm using SQL Tool 1,8 b38
Thank you.
Because LEFT JOIN is executed on entire dataset, and not in row-by-row manner. So there's no wkurse.kurs available in the execution context of subquery. Since you join that tables, you can place the calculation in the top-most select statement.
EDIT:
After you edited the statement, it became clear where does vertragskopf.zahlintervall came from. But I don't know where are you going to use calculated vertragswert (now it is absent in the query), so I've put it in the result. As I'm not a SQL parser and have no idea of your tables, so I cannot check the code, but calculation now can be resolved (all the values are available in calculation context).
SELECT DISTINCT mitarbeiter.mitarbnr, mitarbeiter.login, mitarbeiter.name1, mitarbeiter.name2, s.amount / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
left join vertrag_ek_vk_zuord ON vertragspos.id = vertrag_ek_vk_zuord.ek_vertragspos_id
left join mitarbeiter ON vertrag_ek_vk_zuord.anlage_mitarbnr = mitarbeiter.mitarbnr
left join vertragskopf ON vertragskopf.id = vertragspos.vertrag_id
left join (
SELECT wkurse.*, fremdwaehrung.wsymbol
FROM wkurse
INNER join (
SELECT lfdnr, Max(tag) AS maxTag
FROM wkurse
WHERE tag < SYSDATE
GROUP BY lfdnr
) t1
ON wkurse.lfdnr = t1.lfdnr AND wkurse.Tag = t1.maxTag
INNER JOIN fremdwaehrung ON wkurse.lfdnr = fremdwaehrung.lfdnr
) wkurse ON vertragskopf.blfdwaehrung = wkurse.lfdnr
left join (
SELECT vertrag_ID, Sum (preis) preis, Sum (menge) menge, Sum (preis * menge) as amount
FROM vertragspos
GROUP BY vertrag_ID
) s ON vertragskopf.id = s.vertrag_id
Rewriting the code using WITH clause makes it much clearer than select from select.
Also get the rate on last day before today in oracle is as simple as
select wkurse.lfdnr
, max(wkurse.kurs) keep (dense_rank first order by wkurse.tag desc) as rate
from wkurse
where tag < sysdate
group by wkurse.lfdnr
One option is a lateral join:
left join lateral
(SELECT vertrag_ID, Sum(preis) as preis, Sum(menge) as menge,
Sum (preis * menge / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
GROUP BY vertrag_ID
) s
ON vertragskopf.id = s.vertrag_id
I'm working on a SQL query where the user's maximum Audit_Date is not in a range, as in they haven't used the system for a long time. I tried it this way:
SELECT DISTINCT
UserID
,max(Audit_Date)
FROM RV_USERS RV_USERS
INNER JOIN RV_AUDIT RV_AUDIT ON
RV_USERS.UserID=RV_AUDIT.UserID
group BY RV_USERS.UserID
AND --it doesn't like the "and" here
not exists(
select *
FROM RV_USERS RV_USERS
INNER JOIN RV_AUDIT RV_AUDIT ON ON RV_USERS.UserID=RV_AUDIT.UserID
where
Audit_Date not between '2019-05-29 00:00:00' and '10/29/2019'
)
I tried to use not exists, like the example but it's not working in this case. I get incorrect syntax near the keyword 'AND', right before the not exists. I need to make a view out of this so I think variables and temp tables are out. It's going to be used in a crystal report and scheduled in Central Management Console.
**Update1: Tried this per answer:
SELECT DISTINCT
"RV_USERS"."UserID"
,"RV_AUDIT"."Audit_Date"
FROM "RV_USERS" "RV_USERS"
INNER JOIN "RV_AUDIT" "RV_AUDIT" ON "RV_USERS"."UserID"="RV_AUDIT"."UserID"
group BY "RV_USERS"."UserID", "RV_AUDIT"."Audit_Date"
HAVING
max("RV_AUDIT"."Audit_Date") < '2019-05-29 00:00:00'
and
"RV_USERS".UserID='me'
This returns me with dates in may, even though I have used the system since May. I checked that by removing the max date part and see my dates go to today.
**Update2: Tried this per other answer:
SELECT DISTINCT
"RV_USERS"."UserID"
,max("RV_AUDIT"."Audit_Date")
FROM "RV_USERS" "RV_USERS"
INNER JOIN "RV_AUDIT" "RV_AUDIT" ON "RV_USERS"."UserID"="RV_AUDIT"."UserID"
WHERE
not exists(
select *
FROM "RV_USERS" u2
INNER JOIN "RV_AUDIT" a2 ON u2."UserID"=a2."UserID"
where
a2."Audit_Date" not between '2019-05-27 00:00:00' and '10/31/2019'
)
group BY "RV_USERS"."UserID"
This is not returning anything, but we know there are managers that haven't used the system.
**Update 3 per answer:
SELECT DISTINCT
u."UserID"
,max(a."Audit_Date")
FROM "RV_USERS" u
INNER JOIN "RV_AUDIT" a ON u."UserID"=a."UserID"
WHERE
u.UserID not in(
select u2.UserID
FROM "RV_USERS" u2
INNER JOIN "RV_AUDIT" a2 ON u2."UserID"=a2."UserID"
where
a2."Audit_Date" between '2019-05-27 00:00:00' and '10/31/2019'
)
group BY u."UserID"
You have more than one mistakes, here is corrected code:
SELECT DISTINCT
RV_U.UserID
, max(RV_U.Audit_Date)
FROM RV_USERS RV_U
INNER JOIN RV_AUDIT RV_A ON RV_U.UserID=RV_A.UserID
WHERE
NOT EXISTS(
SELECT *
FROM RV_USERS RV_USERS
INNER JOIN RV_AUDIT RV_AUDIT ON RV_USERS.UserID=RV_AUDIT.UserID
WHERE Audit_Date not between '2019-05-29 00:00:00' and '10/29/2019'
)
GROUP BY RV_U.UserID;
Your line NNER JOIN RV_AUDIT RV_AUDIT ON ON
RV_USERS.UserID=RV_AUDIT.UserID has ON two times.
GROUP BY should go on the end
AND should be replaced with WHERE
Try to implement this changes.
I would suggest a having clause:
SELECT UserID, max(Audit_Date)
FROM RV_USERS RV_USERS INNER JOIN
RV_AUDIT RV_AUDIT
ON RV_USERS.UserID = RV_AUDIT.UserID
GROUP BY RV_USERS.UserID
HAVING max(Audit_Date) < '2019-05-29';
Your query seems much more complicated than necessary.
Replace and with where clause and put the group by at the last to make the above query run atleast
I have design one query such as
Select Temp3.*
From (
(Select temp1.alletec_ce1name,temp1.employeeid, count (temp1.alletec_mifid) AS MifAssign,alletec_mifid
from (select MIF.alletec_ce1name,User1.employeeid,MIF.alletec_mifid from Filteredalletec_mif as MIF FULL OUTER JOIN FilteredSystemUser As User1 on MIF.alletec_ce1=User1.systemuserid
FULL OUTER JOIN FilteredBusinessUnit as BU ON User1.businessunitid=BU.businessunitid
where MIF.alletec_organisationname='Konica Minolta India' AND MIF.alletec_cityname ='Delhi' AND
MIF.alletec_regionname ='North' ) as temp1
Group by temp1.alletec_mifid,temp1.alletec_ce1name,temp1.employeeid ) as Temp2 Inner Join FilteredIncident As Incident On Incident.alletec_serialnomif=Temp2.alletec_mifid ) as temp3
Now the issue is it is showing the Syntax error near the first from. What can be the possible reason for that. Thanks in advance.
I have one more query, such as
with temp2 (
alletec_ce1name,
employeeid,
alletec_mifid,
alletec_cityname,
alletec_regionname
)
as(
Select temp1.alletec_ce1name,temp1.employeeid, temp1.alletec_mifid, temp1.alletec_cityname,temp1.alletec_regionname
from (select MIF.alletec_ce1name,User1.employeeid,MIF.alletec_mifid, MIF.alletec_regionname, MIF.alletec_cityname from Filteredalletec_mif as MIF FULL OUTER JOIN FilteredSystemUser As User1 on MIF.alletec_ce1=User1.systemuserid
FULL OUTER JOIN FilteredBusinessUnit as BU ON User1.businessunitid=BU.businessunitid
where MIF.alletec_organisationname='Konica Minolta India' AND (MIF.alletec_cityname ='Delhi') AND
(MIF.alletec_regionname ='North') ) as temp1
)
select temp2.alletec_ce1name,temp2.employeeid, temp2.alletec_regionname,temp2.alletec_cityname, count(alletec_mifid) as MIFASSIGN,
Incident.alletec_casecalltypename
from temp2 Left Outer join FilteredIncident As Incident On Incident.alletec_serialnomif=temp2.alletec_mifid
group by temp2.alletec_ce1name,temp2.employeeid,temp2.alletec_mifid,temp2.alletec_regionname,temp2.alletec_cityname,
Incident.alletec_casecalltypename
now as with i got one temporary table. i wish to have one more temporary table so that can incorporate my last executable table values from temp2. do have a look if u can help for either. Thanks
Count up your parentheses, I think they are mismatched.
Your query amounts to
select
*
from
(table) as temp3
This isn't valid, you can't put the parentheses there. The extra level of nesting is broken. You either need to do something like:
select
*
from
table
or something like
select
*
from (
select
*
from
table
) as temp3
Your first query can probably be simplified to:
Select
*
From (
select
MIF.alletec_ce1name,
User1.employeeid,
MIF.alletec_mifid,
count (MIF.alltec_mifid) as MifAssign
from
Filteredalletec_mif as MIF
full outer join
FilteredSystemUser As User1
on MIF.alletec_ce1 = User1.systemuserid
full outer join
FilteredBusinessUnit as BU
on User1.businessunitid=BU.businessunitid
where
MIF.alletec_organisationname = 'Konica Minolta India' AND
MIF.alletec_cityname ='Delhi' AND
MIF.alletec_regionname ='North'
group by
MIF.alletec_mifid,
MIF.alletec_ce1name,
User1.employeeid
) as temp1
Inner Join
FilteredIncident As Incident
On Incident.alletec_serialnomif = Temp1.alletec_mifid;
I'm trying to create a materialized view in Oracle 11.1, which apparently does not support nested selects in a materialized view (Why this is, I haven't been able to figure out).. Is there a way to write this query to work as a materialized view? Thanks!
CREATE MATERIALIZED VIEW MV_Area90DayReport
NOLOGGING
CACHE
BUILD IMMEDIATE
REFRESH FAST ON COMMIT
AS
select
T.TASKID,
V.PROJECTID,
V.VERSIONID,
T.GOLDDATE,
P.BUSINESSLAUNCHDATE,
V.NAME as ProjectName,
T.NAME as TaskName,
T.COURSECODE,
DT.NAME as DeliveryMethod,
T.DELIVERABLELENGTHHOUR,
T.DELIVERABLELENGTHMINUTE,
V.PRIORITY,
(SELECT MIN(STARTDATE) FROM TPM_TRAININGPLAN WHERE PROJECTID=V.PROJECTID AND TRAININGPLANTYPE='prescribed') as TrainingDeliveryDate,
(SELECT wm_concat(WORKGROUPID) FROM TPM_PROJECTWORKGROUPS WHERE PROJECTID=V.PROJECTID GROUP BY PROJECTID) as Workgroups,
from TPM_TASK T
inner join TPM_PROJECTVERSION V ON (V.PROJECTID = T.PROJECTID AND V.VERSIONID = T.VERSIONID)
inner join TPM_PROJECT P ON (P.PROJECTID = T.PROJECTID)
inner join TPM_DOCUMENTTYPE DT ON (DT.DOCUMENTTYPEID = T.DOCUMENTTYPEID);
The error I get is:
>[Error] Script lines: 1-25 -------------------------
ORA-22818: subquery expressions not allowed here
Script line 20, statement line 20, column 115
I believe this is a limitation (that was raised as a bug sometime back), documented here on the Oracle site - http://download.oracle.com/docs/cd/B12037_01/server.101/b10736/basicmv.htm#sthref431
To resolve, you should use JOINS rather than subqueries.
Try the following query:
select
T.TASKID,
V.PROJECTID,
V.VERSIONID,
T.GOLDDATE,
P.BUSINESSLAUNCHDATE,
V.NAME as ProjectName,
T.NAME as TaskName,
T.COURSECODE,
DT.NAME as DeliveryMethod,
T.DELIVERABLELENGTHHOUR,
T.DELIVERABLELENGTHMINUTE,
V.PRIORITY,
TP.TrainingDeliveryDate,
WG.Workgroups,
from TPM_TASK T
inner join TPM_PROJECTVERSION V ON (V.PROJECTID = T.PROJECTID AND V.VERSIONID = T.VERSIONID)
inner join TPM_PROJECT P ON (P.PROJECTID = T.PROJECTID)
inner join TPM_DOCUMENTTYPE DT ON (DT.DOCUMENTTYPEID = T.DOCUMENTTYPEID)
left join (
SELECT PROJECTID, MIN(STARTDATE) as TrainingDeliveryDate
FROM TPM_TRAININGPLAN
WHERE TRAININGPLANTYPE='prescribed'
GROUP BY PROJECTID
) TP on TP.PROJECTID=V.PROJECTID
left join (
SELECT PROJECTID, wm_concat(WORKGROUPID) as Workgroups
FROM TPM_PROJECTWORKGROUPS
GROUP BY PROJECTID
) WG on WG.PROJECTID=V.PROJECTID
i have some complex select and result need to place in the table.
I try this:
INSERT INTO SAMGUPS_STATISTIC_STANTION
(SAMGUPS_STATISTIC_STANTION_SEQ.nextval)
(
with PRG as (
select
ID_OBJ
from PEREGON where ID_STAN1=&arrival
),
STN_OBJ as (
select
distinct ID_OBJ_P as ID_OBJ
from TMO start with ID_OBJ=&dispatch
connect by prior ID_OBJ_P=ID_OBJ
),
STAN as (
select
A_TOP.ID_POEZD
,A_TOP.vrsvop
from STN_OBJ inner join A_TOP on A_TOP.ID_OBJ=STN_OBJ.ID_OBJ and A_TOP.KODOP_P in ('01','07')
left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
WHERE ATR.NOM_POEZD LIKE '____'
),
DATA_RESULT as
(
select
/*count(*) over() as TotalCount*/
to_char(&dispatch) as dispatch
,to_char(&arrival) as arrival
...
,ATR.PR_N_V_PZ
from PRG inner join A_TOP on A_TOP.ID_OBJ=PRG.ID_OBJ and A_TOP.KODOP_P in ('03','07')
left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
inner join STAN STN on STN.ID_POEZD = ATR.ID_POEZD
WHERE ATR.NOM_POEZD LIKE '____'
order by A_TOP.ID_POEZD
)
SELECT * FROM DATA_RESULT);
i have error:
Error at Command Line:71 Column:25
Error report:
SQL Error: ORA-32034: unsupported use of WITH clause
32034. 00000 - "unsupported use of WITH clause"
*Cause: Inproper use of WITH clause because one of the following two reasons
1. nesting of WITH clause within WITH clause not supported yet
2. For a set query, WITH clause can't be specified for a branch.
3. WITH clause can't sepecified within parentheses.
*Action: correct query and retry
Whether there is to circumvent these restrictions?
May be possible result of select place into variable and then using variable insert to table?
WITH clause can't sepecified within parentheses.
try rewrite smth like this
INSERT INTO SAMGUPS_STATISTIC_STANTION
with PRG as (
select
ID_OBJ
from PEREGON where ID_STAN1=&arrival
),
STN_OBJ as (
select
distinct ID_OBJ_P as ID_OBJ
from TMO start with ID_OBJ=&dispatch
connect by prior ID_OBJ_P=ID_OBJ
),
STAN as (
select
A_TOP.ID_POEZD
,A_TOP.vrsvop
from STN_OBJ inner join A_TOP on A_TOP.ID_OBJ=STN_OBJ.ID_OBJ and A_TOP.KODOP_P in ('01','07')
left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
WHERE ATR.NOM_POEZD LIKE '____'
),
DATA_RESULT as
(
select
/*count(*) over() as TotalCount*/
to_char(&dispatch) as dispatch
,to_char(&arrival) as arrival
...
,ATR.PR_N_V_PZ
from PRG inner join A_TOP on A_TOP.ID_OBJ=PRG.ID_OBJ and A_TOP.KODOP_P in ('03','07')
left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
inner join STAN STN on STN.ID_POEZD = ATR.ID_POEZD
WHERE ATR.NOM_POEZD LIKE '____'
order by A_TOP.ID_POEZD
)
SELECT SAMGUPS_STATISTIC_STANTION_SEQ.nextval, DATA_RESULT.* FROM DATA_RESULT;
No need for an ORDER BY on an INSERT.
An INSERT is coded like this: INSERT INTO mytable (mycolumn, ...) SELECT ...
Try something like this:
INSERT INTO SAMGUPS_STATISTIC_STANTION
(dispatch, arrival, ..., PR_N_V_PZ)
select
to_char(&dispatch) as dispatch
,to_char(&arrival) as arrival
...
,ATR.PR_N_V_PZ
from (select
ID_OBJ
from PEREGON where ID_STAN1=&arrival
)
inner join A_TOP on A_TOP.ID_OBJ=PRG.ID_OBJ and A_TOP.KODOP_P in ('03','07')
left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
inner join (select
A_TOP.ID_POEZD
,A_TOP.vrsvop
from (select
distinct ID_OBJ_P as ID_OBJ
from TMO start with ID_OBJ=&dispatch
connect by prior ID_OBJ_P=ID_OBJ
) inner join A_TOP on A_TOP.ID_OBJ=STN_OBJ.ID_OBJ and A_TOP.KODOP_P in ('01','07')
left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
WHERE ATR.NOM_POEZD LIKE '____') STN
on STN.ID_POEZD = ATR.ID_POEZD
WHERE ATR.NOM_POEZD LIKE '____';