Sub query brain freeze - sql

Its been a while since I've done sub queries and for the life of me I cant see whats wrong with my query.
The error message I get when executing is:
ORA-00904: "SUB"."PRO_REFNO": invalid identifier
This is my query. I'm obviously doing something wrong but I just cant see it.
SELECT
prop.PRO_ADR_1_LINE,
ele.POE_START_DATE,
ele.POE_ELEMENT_DESCR,
ele.POE_VALUE,
ele.POE_ATTRIBUTE,
ele.POE_FURTHER_ATTRIBUTE,
ele.POE_FURTHER_ATTRIBUTE_DESCR,
prop.PRO_SCHEME,
prop.PRO_SCHEME_DESCR,
GEO.GEO_BUS_UNIT,
GEO.GEO_REGION,
GEO.GEO_REGION_DESCR,
prop.PRO_NEIGHBOURHOOD_DESCR
--sub.pro_refno
FROM property prop
--inner join
left join GEO on prop.PRO_GEO_PATCH=GEO.GEO_PATCH
left join PROPERTY_OTHER_ELEMENT ele on ele.POE_PRO_REFNO =prop.PRO_REFNO
inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO)
FROM
PROPERTY
WHERE
property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO
where
ele.POE_START_DATE BETWEEN '01-APR-2016' AND sysdate
AND
ele.POE_ELEMENT LIKE 'EST%'
AND
ele.POE_ELEMENT_DESCR <> 'Estate Walkabout - Would you live in this neighbourhood ?'
AND
ele.POE_VALUE IN ( '1','2','3','4','5','6','7','8','9','10' )
Both the outer query and sub query run fine separately. Like I said its been a while so I'm guessing its something stupid I've done/not done.
Thanks
Adam

You didn't give a name to the aggregate column:
inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO) -- No name!!!
FROM
PROPERTY
WHERE
property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO
Change that to:
inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO) As PRO_REFNO
FROM
PROPERTY
WHERE
property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO

Your subquery doesn't select PRO_REFNO, so the outer query cannot match it in the JOIN predicate. Try this for the subquery:
SELECT
property.PRO_SCHEME,
property.PRO_REFNO,
count(distinct property.PRO_REFNO)
FROM PROPERTY
WHERE property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME,
property.PRO_REFNO
Also, your COUNT(DISTINCT ...) isn't given an alias, which you'll need if it's ever going to be used in the outer select.

Related

postgres: LEFT JOIN table and field does not exist

This is my query
SELECT org.id,
org.name,
org.type,
org.company_logo,
(SELECT org_profile.logo_url FROM org_profile WHERE org_profile.org_id = org.id AND org_profile.status = 'active' ORDER BY org_profile.id DESC LIMIT 1) as logo_url,
org.short_description,
org_profile.value_prop,
count(*) OVER () AS total
FROM org
LEFT JOIN user_info ON user_info.id = org.approved_by
INNER JOIN (select distinct org_profile.org_id from org_profile) org_profile ON org_profile.org_id = org.id
WHERE
org.type = 'Fintech'
AND org.status = 'APPROVED'
AND org.draft != TRUE
ORDER BY org.id DESC
I am using LEFT JOIN query with my org_profile table. I used distinct for unique org id but the problem is org_profile.value_prop column does not work. The error is showing column org_profile.value_prop does not exist
I'm trying to solve this issue. But I don't figure out this issue.
basically, the error informs that you try to get the value_prop field from org_profile subquery, which basically doesn't exist.
It's difficult to give a working query by writting just on the paper, but I think that:
it's worth to apply the handy aliasses for each subquery
deduplication, if required, should be done in the subquery. When multiple fields used DISTINCT may be insufficient - RANK function may be required.
you make some operations to get the logo_url by a scalar subquery - it seems a bit strange, especially the same table is used in JOIN - I would suggest to move all logic related to org_profile to the subquery. Scalar expressions would throw an error in case multiple values would be found in output.
SELECT
org.id,
org.name,
org.type,
org.company_logo,
prof.logo_url,
org.short_description,
prof.value_prop,
count(*) OVER () AS total
FROM org
JOIN (
select distinct org_id, logo_url, value_prop -- other deduplication type (RANK) may be required
from org_profile
where status = 'active' -- ?
) prof ON org.id = prof.org_id
LEFT JOIN user_info usr ON usr.id = org.approved_by
WHERE
org.type = 'Fintech'
AND org.status = 'APPROVED'
AND org.draft != TRUE
ORDER BY org.id DESC

Oracle SQL - how to NOT SHOW athlete name that apears only once

created a view called winners, it contains the columns: athlete_name,year,medal_won
its basicly athletes that won olympic medal and the year,
it look like that,
data base is in live sql: https://livesql.oracle.com/apex/f?p=590:1000:0
select distinct year,athlete_name,medal
from olym.olym_medals
join olym.olym_athlete_games on olym_athlete_games.id = olym_medals.athlete_game_id
join olym.olym_nations on olym_nations.id = olym_athlete_games.nation_id
join olym.olym_games on olym_games.id = Olym_athlete_games.game_id
join olym.olym_athletes on olym_athletes.id = olym_athlete_games.athlete_id
order by athlete_name
as you can see some name show only once and some names are showing more than once, i want to get rid off all lines of those who show ONLY ONCE, please help me.
thank you!
if i have understand your problem, must group your data,
select year,athlete_name,medal, count(*) "number of Medals"
from olym.olym_medals
join olym.olym_athlete_games on olym_athlete_games.id = olym_medals.athlete_game_id
join olym.olym_nations on olym_nations.id = olym_athlete_games.nation_id
join olym.olym_games on olym_games.id = Olym_athlete_games.game_id
join olym.olym_athletes on olym_athletes.id = olym_athlete_games.athlete_id
group by year,athlete_name,medal;
If I followed you correctly, you can use window functions:
select *
from (
select og.year, oa.athlete_name, om.medal, count(*) over(partition by oa.id) cnt
from olym.olym_medals om
join olym.olym_athlete_games oag on oag.id = om.athlete_game_id
join olym.olym_nations ona on ona.id = oag.nation_id
join olym.olym_games og on og.id = oag.game_id
join olym.olym_athletes oa on oa.id = oag.athlete_id
) t
where cnt > 1
order by athlete_name
Notes:
I am unsure why you were using distinct in the first place, so I removed it (I suspect it is actually not needed)
I added table aliases to shorten the query, and prefixed the columns in the select clause with the table they belong to (you might want to review that) - these are best practices when dealing with multi-table queries
Use GROUP BY and HAVING COUNT(*) > 1:
SELECT year,
athlete_name,
medal
FROM olym.olym_medals
INNER JOIN olym.olym_athlete_games
ON olym_athlete_games.id = olym_medals.athlete_game_id
INNER JOIN olym.olym_nations
ON olym_nations.id = olym_athlete_games.nation_id
INNER JOIN olym.olym_games
ON olym_games.id = Olym_athlete_games.game_id
INNER JOIN olym.olym_athletes
ON olym_athletes.id = olym_athlete_games.athlete_id
GROUP BY
year,
athlete_name,
medal
HAVING COUNT(*) > 1
ORDER BY athlete_name

Group by query error in multiple joins

I need to fetch each select item and its count in single query.Here is my query and expecting the output as single array.
select ordetable.order_id, count(ordetable.order_id), order_table.name,
count(order_table.na me), orderitem_table.itemname,
count(orderitem_table.itemname)
from order_table
left join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by ordetable.order_id
Am getting this error:
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Column
'orderitem_table.itemname' is invalid in the select list because it is
not contained in either an aggregate function or the GROUP BY clause.
The issue is evident from the error message, isn't it?
Modify your query like below.
select ordetable.order_id, count(ordetable.order_id), order_table.name,
count(order_table.name), orderitem_table.itemname,
count(orderitem_table.itemname)
from order_table
left join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by ordetable.order_id, order_table.name, orderitem_table.itemname
If you not like to group order_table.name and orderitem_table.itemname, then you can use 'min' condition.
select ordetable.order_id, count(ordetable.order_id), min(order_table.name),
count(order_table.name), min(orderitem_table.itemname),
count(orderitem_table.itemname)
from order_table
left join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by ordetable.order_id
Your wording and your query are not in sync. Each item and its count (over all the orders) will be
select orderitem_table.itemname, orderitem_table.itemnumber, count(*)
from order_table
join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by orderitem_table.itemname, orderitem_table.itemnumber

how to select count of rows an other table

i'm using this query to select count of comments on other table but it give me error
SELECT
dbo.tblEmails.id, dbo.tblEmails.eTitle, dbo.tblEmails.ePreDesc, dbo.tblEmails.eTags, dbo.tblEmails.eFaDate, dbo.tblEmails.eViewCount,
dbo.tblEmails.ePrice, dbo.tblEmails.eImg, COUNT(tblComments.id) AS cCount
FROM
dbo.tblEmails as tblEmails
INNER JOIN
dbo.tblComments AS tblComments ON dbo.tblEmails.id = dbo.tblComments.PostID
GROUP BY
tblEmails.id, tblEmails.eTitle, tblEmails.ePreDesc, tblEmails.eTags, tblEmails.eFaDate, tblEmails.eViewCount, tblEmails.ePrice, tblEmails.eImg
UPDATE:
error is this :
the text,ntext, and image data types cannot be compared or
stored,except when using IS NULL or LIKE operator.
but i have not image data type in my table
Well, you haven't specified what error text is... But in this particular case it is easy to deduce.
Your problem is incorrect usage of aliases in join and select.
It should be not
INNER JOIN dbo.tblComments AS tblComments ON dbo.tblEmails.id = dbo.tblComments.PostID
but
INNER JOIN dbo.tblComments AS tblComments ON tblEmails.id = tblComments.PostID
And the same story is about select - not dbo.tblEmails.id but tblEmails.id since you've specified alias.
But note - using exact table_name as alias to dbo.table_name looks like a bad idea and may lead to confusion (and in fact, it has lead in your case).
Instead consider using short acronyms for aliases, like this:
SELECT
E.id, E.eTitle, E.ePreDesc, E.eTags,
E.eFaDate, E.eViewCount,E.ePrice, E.eImg,
COUNT(C.id) AS cCount
FROM dbo.tblEmails as E
INNER JOIN dbo.tblComments AS C ON E.id = C.PostID
GROUP BY
E.id, E.eTitle, E.ePreDesc, E.eTags,
E.eFaDate, E.eViewCount,E.ePrice, E.eImg
Do a sub-select to get the count:
SELECT dbo.tblEmails.id, dbo.tblEmails.eTitle, dbo.tblEmails.ePreDesc, dbo.tblEmails.eTags, dbo.tblEmails.eFaDate, dbo.tblEmails.eViewCount,
dbo.tblEmails.ePrice, dbo.tblEmails.eImg,
(select COUNT(*) from dbo.tblComments
where dbo.tblEmails.id = dbo.tblComments.PostID) AS cCount
FROM dbo.tblEmails as tblEmails

What could create a syntax error if you take a SQL query and perform an UNION with itself?

I have this strange error in SQL Server 2005 where I take a working query, add the UNION keyword below it and then copy the query again. In my opinion, this should always be working, but it is not. I get the message 'Incorrect syntax near the keyword 'union'.
What could create this problem ?
To be more specific, here is the complete query :
select distinct deliveries.id, orders.id, 20 + sum(orders.mass1) as allowed_duration
from features_resources
inner join features on features.id = featureid
inner join orders on orders.id = features_resources.resourceid
inner join orderinformations on orders.id = orderinformations.orderid
inner join deliveries on orderinformations.deliveryid = deliveries.id
where features.name = 'O_FRAIS'
and (deliveries.ID IN
(SELECT ID
FROM dbo.DeliveriesInExportedSchedule))
group by deliveries.id, features.name ,orders.id order by deliveries.id
union
select distinct deliveries.id, orders.id, 20 + sum(orders.mass1) as allowed_duration
from features_resources
inner join features on features.id = featureid
inner join orders on orders.id = features_resources.resourceid
inner join orderinformations on orders.id = orderinformations.orderid
inner join deliveries on orderinformations.deliveryid = deliveries.id
where features.name = 'O_FRAIS'
and (deliveries.ID IN
(SELECT ID
FROM dbo.DeliveriesInExportedSchedule))
group by deliveries.id, features.name ,orders.id order by deliveries.id
I have tried to reproduce the error on a smaller query, by starting from a simple query and adding features one by one (inner join, nested queryes, group by, sum,....) but failed to reproduce the error again.
Any idea ?
It is actually the order by deliveries.id in the top half that causes the problem.
The order by needs to apply to the whole query.
Example Syntax
SELECT v1.number
FROM master.dbo.spt_values v1
WHERE v1.number > 2000
UNION
SELECT v2.number
FROM master.dbo.spt_values v2
WHERE v2.number < 10
ORDER BY v1.number
Try putting the individual SELECTs in parentheses:
(SELECT ... )
UNION
(SELECT ... )
The way you have it now, the second WHERE and GROUP BY clauses are ambiguous - should that apply to the SELECT, or to the UNION? I don't have any way to tell, and neither has your DB server.