ERROR SQL Server ONLY one expression can be specified in the select list when the subquery is not introduced with exists - sql

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
);

Related

ParseException line cannot recognize input near '(' 'select' 'from' in joinSource

I am trying to execute a query in hive and getting the error. I check over and over again but I cannot see any problem.
select
a.phone_no,
a.app_name
from
(select * from (select app_name,phone_no from lc_app_flag) ) a
inner join
(select * from (select phone_no,city_id_day,city_id_night,lat_day,lng_day,lat_night,lat_night from TW_FEATS_FIN_LCEXT01 where month_id='202205' ) b where city_id_day='440100' or city_id_night='440100') c
on a.phone_no=c.phone_no
You missed aliasing first innermost subquery. Correct SQL below -
select
a.phone_no,
a.app_name
from
(select * from (select app_name,phone_no from lc_app_flag) subq) a --inner subquery as subq
inner join
(select * from (select phone_no,city_id_day,city_id_night,lat_day,lng_day,lat_night,lat_night from TW_FEATS_FIN_LCEXT01 where month_id='202205' ) b where city_id_day='440100' or city_id_night='440100') c
on a.phone_no=c.phone_no

Why is my query inserting the same values when I have added a 'not exists' parameter that should avoid this from happening?

My query should stop inserting values, as the not exists statement is satisfied (I have checked both tables) and matching incidents exist in both tables, any ideas why values are still being returned?
Here is the code:
INSERT INTO
odwh_system.ead_incident_credit_control_s
(
incident
)
SELECT DISTINCT
tp.incident
FROM
odwh_data.ead_incident_status_audit_s ei
INNER JOIN odwh_data.ead_incident_s tp ON ei.incident=tp.incident
WHERE
ei.status = 6
OR
ei.status = 7
AND NOT EXISTS
(
SELECT
true
FROM
odwh_system.ead_incident_credit_control_s ead
WHERE
ead.incident = tp.incident
)
AND EXISTS
(
SELECT
true
FROM
odwh_work.ead_incident_tp_s tp
WHERE
tp.incident = ei.incident
);
dont reuse table aliases
use sane aliases
avoid AND/OR conflicts; prefer IN()
INSERT INTO odwh_system.ead_incident_credit_control_s (incident)
SELECT -- DISTINCT
tp.incident
FROM odwh_data.ead_incident_s dtp
WHERE NOT EXISTS (
SELECT *
FROM odwh_system.ead_incident_credit_control_s sic
WHERE sic.incident = dtp.incident
)
AND EXISTS (
SELECT *
FROM odwh_work.ead_incident_tp_s wtp
JOIN odwh_data.ead_incident_status_audit_s dis ON wtp.incident = dis.incident AND dis.status IN (6 ,7)
WHERE wtp.incident = dtp.incident
);

How to solve Invalid object name when naming a select/

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.

INNER JOINING THE TABLE ITSELF GIVES No column name was specified for column 2

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

DB2 Alternate to EXISTS Function

I have the below query in my application which was running on DB2:
SELECT COD.POST_CD,CLS.CLASS,COD2.STATUS_CD
FROM DC01.POSTAL_CODES COD
INNER JOIN DC02.STATUS_CODES COD2
ON COD.ORDER=COD2.ORDER
INNER JOIN DC02.VALID_ORDERS ORD
ON ORD.ORDER=COD.ORDER
WHERE
(
( EXISTS (SELECT 1 FROM DC00.PROCESS_ORDER PRD
WHERE PRD.ORDER=COD.ORDER
AND PRD.IDNUM=COD.IDNUM
)
) OR
( EXISTS (SELECT 1 FROM DC00.PENDING_ORDER PND
WHERE PND.ORDER=COD.ORDER
AND PND.IDNUM=COD.IDNUM
)
)
)
AND EXISTS (SELECT 1 FROM DC00.CUSTOM_ORDER CRD
WHERE CRD.ORDER=COD.ORDER
)
;
When we changed to UDB (LUW v9.5) we are getting the below warning:
IWAQ0003W SQL warnings were found
SQLState=01602 Performance of this complex query might be sub-optimal.
Reason code: "3".. SQLCODE=437, SQLSTATE=01602, DRIVER=4.13.111
I know this warning is due to the EXISTS () OR EXISTS statements. But I am not sure any other way I can write this query to replace. If it is AND, I could have made an INNER JOIN, but I am not able to change this condition as it is OR. Can any one suggest better way to replace these EXISTS Statements?
SELECT COD.POST_CD,CLS.CLASS,COD2.STATUS_CD
FROM DC01.POSTAL_CODES COD
INNER JOIN DC02.STATUS_CODES COD2
ON COD.ORDER=COD2.ORDER
INNER JOIN DC02.VALID_ORDERS ORD
ON ORD.ORDER=COD.ORDER
WHERE
(
EXISTS SELECT 1 FROM
(SELECT ORDER,IDNUM FROM DC00.PROCESS_ORDER PRD UNION
SELECT ORDER,IDNUM FROM DC00.PENDING_ORDER PND) PD
WHERE PD.ORDER=COD.ORDER
AND PD.IDNUM=COD.IDNUM
)
AND EXISTS (SELECT 1 FROM DC00.CUSTOM_ORDER CRD
WHERE CRD.ORDER=COD.ORDER
)
;