Oracle SQL COUNT in an EXISTS SELECT - sql

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)

Related

Syntax error to combine left join and select

I'm getting a syntax error at Left Join. So in trying to combine the two, i used the left join and the brackets. I'm not sure where the problem is:
SELECT DISTINCT a.order_id
FROM fact.outbound AS a
ORDER BY Rand()
LIMIT 5
LEFT JOIN (
SELECT
outbound.marketplace_name,
outbound.product_type,
outbound.mpid,
outbound.order_id,
outbound.sku,
pbdh.mpid,
pbdh.product_type,
pbdh.validated_exp_reach,
pbdh.ultimate_sales_rank_de,
pbdh.ultimate_sales_rank_fr,
(
pbdh.very_good_stock_count + good_stock_count + new_Stock_count
) as total_stock
FROM
fact.outbound AS outbound
LEFT JOIN reporting_layer.pricing_bi_data_historisation AS pbdh ON outbound.mpid = pbdh.mpid
AND trunc(outbound.ordered_date) = trunc(pbdh.importdate)
WHERE
outbound.ordered_date > '2022-01-01'
AND pbdh.importdate > '2022-01-01'
LIMIT
5
) AS b ON a.orderid = b.order_id
Error:
You have an error in your SQL syntax; it seems the error is around: 'LEFT JOIN ( SELECT outbound.marketplace_name, outbound.product_t' at line 9
What could be the reason?
Place the first limit logic into a separate subquery, and then join the two subqueries:
SELECT DISTINCT a.order_id
FROM
(
SELECT order_id
FROM fact.outbound
ORDER BY Rand()
LIMIT 5
) a
LEFT JOIN
(
SELECT
outbound.marketplace_name,
outbound.product_type,
outbound.mpid,
outbound.order_id,
outbound.sku,
pbdh.mpid,
pbdh.product_type,
pbdh.validated_exp_reach,
pbdh.ultimate_sales_rank_de,
pbdh.ultimate_sales_rank_fr,
(pbdh.very_good_stock_count +
good_stock_count + new_Stock_count) AS total_stock
FROM fact.outbound AS outbound
LEFT JOIN reporting_layer.pricing_bi_data_historisation AS pbdh
ON outbound.mpid = pbdh.mpid AND
TRUNC(outbound.ordered_date) = TRUNC(pbdh.importdate)
WHERE outbound.ordered_date > '2022-01-01' AND
pbdh.importdate > '2022-01-01'
-- there should be an ORDER BY clause here...
LIMIT 5
) AS b
ON a.orderid = b.order_id;
Note that the select clause of the b subquery can be reduced to just the order_id, as no values from this subquery are actually selected in the end.
You can skip the LEFT JOIN, since no b columns are selected. (And SELECT DISTINCT makes sure any duplicates are eliminated.)
SELECT DISTINCT order_id
FROM fact.outbound
ORDER BY Rand()
LIMIT 5

LEFt JOIN LATERAL showing error with SELECT

I am trying to run below query :
SELECT
tc.ID_NUMBER AS AFC_RPP_Number,
hc.BUSINESS AS Business,
hc.DIRECTOR AS Director,
tc.REASON_FOR_REVISION AS Description_of_Change
FROM alo_gg.AWS_PIM tc
left join lateral(
select BUSINESS,DIRECTOR
FROM alo_ggg.tracker
WHERE START_DATE <= tc.DATE AND SO = tc.SO
ORDER BY START_DATE DESC
LIMIT 1
) hc;
Above query is showing error:
ERROR: syntax error at or near "SELECT"
left join lateral (SELECT BUSINESS,DIRECTOR...
If I run the subquery separately it is giving me a result, but with lateral it is giving me an error.
You need to add ON TRUE and remove comma:
SELECT
tc.ID_NUMBER AS AFC_RPP_Number,
hc.BUSINESS AS Business,
hc.DIRECTOR AS Director,
tc.REASON_FOR_REVISION AS Description_of_Change
FROM alo_gg.AWS_PIM tc -- removing comma
left join lateral(
select BUSINESS,DIRECTOR
FROM alo_ggg.tracker
WHERE START_DATE <= tc.DATE AND SO = tc.SO
ORDER BY START_DATE DESC
LIMIT 1
) hc ON TRUE; -- adding `ON` clause

How to join table to table created from query

I query to get the top 5 results of IPs and then i want to get for each IP, the countries and others fields related to it by join.
select actual_ip, actual_country_code, actual_country_name, organization FROM "public"."bus_request" inner join (
select top 5 actual_ip, count(*) FROM "public"."bus_request"
where app_name = 'xxxxx' and request_score>0 and date >= '2019-06-07' and event_type <> 'browser_js'
group by actual_ip order by count desc ) as temp on actual_ip = temp.actual_ip
SQL Error [500310] [42702]: [Amazon](500310) Invalid operation: column reference "actual_ip" is ambiguous;
Not sure exactly which DBMS you're using, but try resolving the ambiguity by specifying the table, e.g.:
select busreq.actual_ip, actual_country_code, actual_country_name, organization FROM "public"."bus_request" as busreq inner join (
select top 5 actual_ip, count(*) FROM "public"."bus_request"
where app_name = 'xxxxx' and request_score>0 and date >= '2019-06-07' and event_type <> 'browser_js'
group by actual_ip order by count desc ) as temp on busreq.actual_ip = temp.actual_ip
(you may need to disambiguate the inner use of actual_ip - but hopefully not.)
You should always qualify all column references in a query that references more than one table. In addition, you should use table aliases so the query is easier to write and to read:
select br.actual_ip, br.actual_country_code, br.actual_country_name, br.organization
from "public"."bus_request" br inner join
(select top 5 br2.actual_ip, count(*)
from "public"."bus_request" br2
where br2.app_name = 'xxxxx' and
br2.request_score > 0 and
br2.date >= '2019-06-07' and
br2.event_type <> 'browser_js'
group by br2.actual_ip
order by count(*) desc
) br2
on br2.actual_ip = br.actual_ip;

Why do I get ORA-00907 in my SQL query?

I have this SQL query which a partner has done for a little project at university (this is the first time we use SQL), but we get the ora-00907 error and both of us don't know why.
I have checked the parenthesis and they seem to be ok, so the problem must be another.
select
persona.nombre,
anyo,
t2.total
from persona join
(
select
t1.idPersona,
count(produccion.anyo) as total,
anyo
from
(
select *
from produccion
join pelicula
on produccion.id = pelicula.id
) as pel
join
(
select *
from participa
where idPapel = 8
) as t1
on t1.idProduccion = pel.id
)
group by t1.idPersona
) as t2
on persona.id = t2.idPersona
where t2.total > 2
order by t2.total desc;
You are selecting * and doing group by on one column which is creating problem. Either you select only respective column under group by condition OR you remove group by.
select *
from (produccion join pelicula on produccion.id=pelicula.id) as pel
join
(select *
from participa
where idPapel=8) as t1
on t1.idProduccion=pel.id)
group by t1.idPersona
Above code section is unallowed use of group by.
If group by is so much needed, i would suggest you to use it later on in the end. Another option is to use analytical function and filter out rest un-wanted records in upper nesting of query which you already have.
You have lots of nested views, which makes your query rather hard to debug. You have lots of brackets, which need to match.
Anyway this is wrong: select t1.idPersona, count(produccion.anyo) as total, anyo. You'll need to include anyo in the GROUP BY clause, which will probably change the result set you want.
select persona.nombre,
t2.anyo,
t2.total
from persona join
(select t1.idPersona,
count(produccion.anyo) as total,
anyo
from (select *
from produccion
join pelicula
on produccion.id=pelicula.id) pel
join
(select *
from participa
where idPapel=8) t1
on t1.idProduccion=pel.id
group by t1.idPersona, t1.anyo) t2
on persona.id=t2.idPersona
where t2.total>2
order by t2.total desc;
I think your query can be simplified/corrected like this:
select persona.nombre,
anyo,
t2.total
from persona
join (
select par.idPersona,
count(produccion.anyo) as total,
anyo
from produccion
join pelicula
on produccion.id = pelicula.id
left join participa par
on par.idProduccion = pelicula.id -- or produccion.id,
-- this was also an error in the original query,
-- since the subquery selected both
and par.idPapel = 8
group by t1.idPersona
, anyo -- Was missing, but it also doesn't make sense, as this is what you count, so you'll just get 1's here. What do you want with this?
) as t2
on persona.id = t2.idPersona
where t2.total > 2
order by t2.total desc;

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