I named my query and when I tried use it I got: Invalid object name 'AssetsTenDays'. How do I solve that issue?
WITH AssetsTenDays AS(
SELECT DISTINCT top 2
a.name,
ir.number
FROM install i
INNER JOIN asset a ON a.pws_assetId = i.pws_AssetId
WHERE a.days = 10
)
when I try to use it I get the Invalid object name -
SELECT distinct *
from AssetsTenDays
Try using them together in a single SQL statement, as such:
WITH AssetsTenDays AS(
SELECT DISTINCT top 2
a.name,
ir.number
FROM install i
INNER JOIN asset a ON a.pws_assetId = i.pws_AssetId
WHERE a.days = 10
)
SELECT distinct *
from AssetsTenDays
Common Table Expression should be used immediate after declaration :
WITH AssetsTenDays AS(
SELECT DISTINCT top 2 a.name, ir.number
FROM install i INNER JOIN
asset a
ON a.pws_assetId = i.pws_AssetId
WHERE a.days = 10
)
SELECT ad.*
FROM AssetsTenDays ad;
You can't use other SELECT statement between declaration & calling common table expression. It should immediate called after declaration.
Related
I'm trying to write a nested TOP + DISTINCT query in Teradata SQL. My query looks like this:
SELECT TOP 5
*
FROM
(SELECT DISTINCT k_name.KUNDE_NAME1
FROM DB_DWH_MART_AKM_PLT.VW_F_EVENT f_ev
INNER JOIN DBX_DWH_SBX_AKM_PRD.TB_KUNDE_EKP_NAME_AKTUELL k_name ON f_ev.AUFTRAGGEBER_EKP = k_name.EKP
WHERE f_ev.PROCESS_NO = 1075)
I get an error:
Expected something like a name or a Unicode delimited identifier... between ) and ;".
I don't know what I did wrong.
The DISTINCT query would execute correctly on its own.
Why not use a group by?
SELECT TOP 5 k_name.KUNDE_NAME1
FROM DB_DWH_MART_AKM_PLT.VW_F_EVENT f_ev
INNER JOIN DBX_DWH_SBX_AKM_PRD.TB_KUNDE_EKP_NAME_AKTUELL k_name ON f_ev.AUFTRAGGEBER_EKP = k_name.EKP
WHERE f_ev.PROCESS_NO = 1075
GROUP BY k_name.KUNDE_NAME1 --instead of using distinct
ORDER BY k_name.KUNDE_NAME1; --remove or modify as needed
I want to create a query where I get all posts from my table INV where the INNUM exists more than 2 times in table INVS.
This is my query right now, but it fails with the typical "missing right parenthesis" error.
But when I run the EXISTS Query isolated, it works....
SELECT WO.WONUM, WO.DESCRIPTION, INV.INNUM, INV.STATUSDATE
FROM INV LEFT OUTER JOIN WO ON INV.WOID = WO.WOID
WHERE EXISTS (
SELECT COUNT(*) FROM INVS WHERE INVS.INNUM = INV.INNUM and INVS.SITEID='ARZ' GROUP BY INVS.INNUM
HAVING COUNT(*) > 2 ORDER BY INVS.INNUM
);
I dont really know why!?
Hmmm . . . use a scalar subquery to calculate the count and compare to "2" in the outer query:
SELECT WO.WONUM, WO.DESCRIPTION, INV.INNUM, INV.STATUSDATE
FROM INV LEFT OUTER JOIN
WO
ON INV.WOID = WO.WOID
WHERE (SELECT COUNT(*)
FROM INVS
WHERE INVS.INNUM = INV.INNUM AND
INVS.SITEID = 'ARZ'
) > 2;
Your query is relying on a doubly nested correlation clause which Oracle does not support.
You could also move the subquery to the FROM clause, but this version is more in the spirit of how you have written the query.
You get ORA-00907: missing right parenthesis while using the ORDER BYclause in the subquery.
Remove it and you get a valid syntax
Example
with tab as (select rownum id from dual connect by level <= 5
union all
select 3 from dual union all
select 5 from dual)
select * from tab t
where exists
(select count(*) from tab
where id = t.id
group by id
having count(*) > 1)
;
ID
----------
3
5
3
5
This is not a valid syntax --> ORA-00907: missing right parenthesis
select * from tab t
where exists
(select count(*) from tab
where id = t.id
group by id
having count(*) > 1 order by id)
I am trying to run my query but I get an error.
This is my query:
if exists (select CODE_ISIN
from cte
where code_ISIN not in (select [STATUT_TITRE], [CODE_ISIN]
from TT_TITRE A
inner join TT_STATUT_TITRE B on A.TITRE_ID = B.TITRE_ID))
begin
select 'ko'
end
begin
select 'ok'
end
Remove [STATUT_TITRE] from sub-query as it will accept only one expression :
select c.CODE_ISIN
from cte c
where code_ISIN not in (select [CODE_ISIN] -- only one expression needed
from TT_TITRE A inner join
TT_STATUT_TITRE B
on A.TITRE_ID = B.TITRE_ID
);
I would suggest to use NOT EXISTS instead :
where not exists (select 1
from TT_TITRE A inner join
TT_STATUT_TITRE B
on A.TITRE_ID=B.TITRE_ID
where CODE_ISIN = c.CODE_ISIN
);
SELECT *
FROM
construction AS T2
INNER JOIN
(
SELECT project,MAX(report_date)
FROM construction
GROUP BY project
) AS R
ON T2.project=R.project AND T2.report_date=R.report_date
getting this error. plz help
No column name was specified for column 2 of 'R'
You need to add alias for MAX(report_date):
SELECT *
FROM construction AS T2
INNER JOIN
(
SELECT project,MAX(report_date) AS report_date
FROM construction
GROUP BY project
) AS R
ON T2.project = R.project
AND T2.report_date = R.report_date;
In SQL Server you can use syntax:
SELECT *
FROM construction AS T2
INNER JOIN
(
SELECT project,MAX(report_date)
FROM construction
GROUP BY project
) AS R(project, report_date)
ON T2.project = R.project
AND T2.report_date = R.report_date;
You should specific the MAX(report_date) with an alias report_date.
Because your table R have two columns project,MAX(report_date).
You are getting this error because you have not specified column name for inner query
You have to write your query as
SELECT *
FROM construction
INNER JOIN
(
SELECT project,MAX(report_date)"Max_ReportDate"
FROM construction
GROUP BY project
) Max_construction
ON construction.project = Max_construction .project
AND construction.report_date = Max_construction .Max_ReportDate
I have two Tables as below..
tbPatientEncounter
tbVoucher
when i execute select query as below
Select EncounterIDP,EncounterNumber from tbPatientEncounter
it returens me 180 rows. and
Select VoucherIDP,EncounterIDF from tbVoucher
above query returns me 165 rows.
but i want to execute select query that returns me data like EncounterIDP not in tbVoucher, for that i have tried below Select query...
Select * from tbPatientEncounter pe
where pe.EncounterIDP not in
(Select v.EncounterIDF from tbVoucher v )
it doesn't returns any row. in first image it shows EncounterIDP 9 in tbPatientEncounter, but it not inserted in tbVoucher for that i have tried select Query like
Select * from tbVoucher where EncounterIDF = 9
it returns me 0 rows.
My question is what is wrong with my above Not In Query.?
In all likelihood, the problem is NULL values in tbVoucher. Try this:
Select *
from tbPatientEncounter pe
where pe.EncounterIDP not in (Select v.EncounterIDF
from tbVoucher v
where v.EncounterIDF is not NULL
)
Are you comparing the correct fields in tbVoucher?
Try using a left join
Select EncounterIDP,EncounterNumber from tbPatientEncounter
left join tbVoucher on EncounterIDP = EncounterIDF
where EncounterIDF is null
Call me a skeptic because I don't see anything wrong with your query. Is this really all in the query or did you simplify it for us?
Select * from tbPatientEncounter pe
where pe.EncounterIDP not in
(Select v.EncounterIDF from tbVoucher v )