I'm a SQL noob that's trying to solve this group by issue. When I run it it throws me "not a group by expression". I've tried omitting fields, removing column heading syntax and it still wont run. I'm at lost as to where the issue is. Let me know if you'd like me to reformat into something prettier!
SELECT owner.owner_first_name,
owner.owner_last_name AS NAME,
owner.owner_phone AS PHONE,
pet.pet_name,
Sum(res.reservation_end_date - res.reservation_start_date) AS NumDays
FROM hvk_pet pet
INNER JOIN hvk_dog dog
ON pet.pet_number = dog.pet_pet_number
INNER JOIN hvk_pet_reservation pRes
ON pRes.pet_pet_number = pet.pet_number
INNER JOIN hvk_reservation res
ON res.reservation_number = pRes.res_reservation_number
INNER JOIN hvk_owner owner
ON owner.owner_number = pet.own_owner_number
GROUP BY owner.owner_first_name,
owner.owner_last_name,
owner.owner_phone,
pet.pet_name
HAVING res.reservation_end_date - To_date('30-NOV-15', 'dd/mm/yy') <= 0
AND Sum(res.reservation_end_date - res.reservation_start_date) >= ALL
(SELECT Sum(res.reservation_end_date - res.reservation_start_date)
FROM
hvk_reservation res)
Well, your error is coming from the following:
HAVING res.reservation_end_date - To_date('30-NOV-15', 'dd/mm/yy') <= 0
res.reservation_end_date is not in the group by clause. Move that into the where criteria and your error should go away.
With that said, I'm not sure your query will ever return any results. You're checking to see if the sum of a subset of values is greater than the sum of all the values -- not likely.
SQL Fiddle Demo (with error)
SQL Fiddle Demo (without)
Related
I'm new to SQL and I'm not certain why I am getting this error. I am trying to left join a sub-query to another query in sql developer.
This is the first query,
SELECT DISTINCT
tl.species,
ag.age
FROM
age_list ag,
tree_list tl
WHERE
ag.tree_id = tl.tree_id
And then the sub-query I would like to left join where the tree_id = tree_number is,
SELECT DISTINCT
sl.tree_spon,
sl.tree_number
FROM spon_list sl
WHERE
sl.tree_spon < 10
When trying to do this I've tried to use,
SELECT DISTINCT
tl.species,
ag.age,
q1.tree_spon
FROM
age_list ag,
tree_list tl
LEFT OUTER JOIN (SELECT DISTINCT
sl.tree_spon,
sl.tree_number
FROM spon_list sl
WHERE sl.tree_spon < 10) q1 on q1.tree_number = tree_list.tree_id
WHERE
ag.tree_id = tl.tree_id
Whatever I change in terms of the alias' for the columns and tables I always get the error, "ORA-00904: invalid identifier error", and that "tree_list.tree_id is invalid identifier", though separately the queries run fine.
Can anyone help, is it an issue with both queries joining on the tl.tree_id?
You can use the ANSI join syntax throughout (rather than mixing in legacy comma joins), joining on ag.tree_id = sl.tree_number (or tl.tree_id = sl.tree_number but they're both equal given the previous join) and putting the filter on sl.tree_spon < 10 into the ON clause as well:
SELECT DISTINCT
tl.species,
ag.age,
sl.tree_spon,
sl.tree_number
FROM age_list ag
INNER JOIN tree_list tl
ON (ag.tree_id = tl.tree_id)
LEFT OUTER JOIN spon_list sl
ON (ag.tree_id = sl.tree_number AND sl.tree_spon < 10)
Change tree_list.tree_id to tl.tree_id
The query below works talking about its functionality, but I wanted to select the result of the multiplication too, as you can see:
SELECT
I.Nome, I.Cognome, I.Stipendio, GP
FROM
Impiegati I
WHERE
I.Stipendio >= ALL(SELECT (progetti.Budget * partecipazioni.Percentuale / 100) AS GP /*HERE*/
FROM Partecipazioni, Progetti
WHERE Partecipazioni.CodProgetto = Progetti.CodProgetto
AND I.ID = partecipazioni.ID)
ORDER BY
I.Nome ASC;
I get this error:
Unknown column 'GP' in 'field list'
You need to move the logic to the from clause in order to return the value. The equivalent logic is basically:
select I.Nome, I.Cognome, I.Stipendio, pp.GP
from Impiegati I join
(select id, max(pr.Budget * pa.Percentuale / 100) as GP
from Partecipazioni pa join
Progetti pr
on pa.CodProgetto = pr.CodProgetto
group by pa.id
) pp
on I.ID = pp.ID
where I.Stipendio >= pp.gp
The only difference is if there are no matches in the subquery (the query can be tweaked for this case if necessary).
Note the use of proper, explicit, standard, readable JOIN syntax. Never use commas in the FROM clause.
Edit: Fixed the displayed error.
I have 2 queries that give me counts. I want to divide one count by the other - I currently have the below SQL but throws out errors immediately:
SELECT tbl_CEAR_Name.CEAR_Team,
Q_Manager_Backlog_Total.Backlogs,
Q_Manager_CHS_Total.CHS_Total,
[Q_manager_CHS_Total].[CHS_Total]/[q_managers_backlog_total].[backlogs] AS Expr1
FROM Q_Manager_CHS_Total
INNER JOIN
(
Q_Manager_Backlog_Total
INNER JOIN tbl_CEAR_Name
ON Q_Manager_Backlog_Total.CEAR_Team = tbl_CEAR_Name.CEAR_Team
)
ON Q_Manager_CHS_Total.CEAR_Team = tbl_CEAR_Name.CEAR_Team
ORDER BY Q_Manager_Backlog_Total.Backlogs DESC;
You totally banjaxed your joins... Try this
SELECT tbl_CEAR_Name.CEAR_Team,
Q_Manager_Backlog_Total.Backlogs,
Q_Manager_CHS_Total.CHS_Total,
[Q_manager_CHS_Total].[CHS_Total]/[q_managers_backlog_total].[backlogs] AS Expr1
FROM Q_Manager_CHS_Total
INNER JOIN
(
select Q_Manager_Backlog_Total.Backlogs, tbl_CEAR_Name.CEAR_Team
FROM Q_Manager_Backlog_Total
INNER JOIN tbl_CEAR_Name
ON Q_Manager_Backlog_Total.CEAR_Team = tbl_CEAR_Name.CEAR_Team
) T1
ON Q_Manager_CHS_Total.CEAR_Team = T1.CEAR_Team
ORDER BY Q_Manager_Backlog_Total.Backlogs DESC;
I suspect the count field [backlogs] in query [q_managers_backlog_total] has 0 as a result somewhere. If you then try to divide by 0 you will evidently get an error. Catch a 0 value in the underlying query and you're good.
Through some research I've come to realize that Oracle SQL does not support AS and thus, my statement below results in "missing keyword"
SELECT IP.Company, IP.Copay, P1.Num_Patients
FROM Insurance_Plan IP
JOIN Patient P
ON IP.Plan_Name = P.Plan_Name
JOIN
(SELECT Plan_Name, COUNT(*) AS Num_Patients
FROM Patient
GROUP BY Plan_Name) AS P1
ON P.Plan_Name = N.Plan_Name
GROUP BY IP.Company, IP.Copay, P1.Num_Patients
HAVING P1.Num_Patients =
(SELECT MAX(Num_Patients) FROM
(SELECT Plan_Name, COUNT(Patient_ID) AS Num_Patients
FROM Patient
GROUP BY Pan_Name) P2);
Where I run in to trouble is on line 8 where I have "AS P1" and I cannot for the life of me figure out what it is I need to correct to get this to run. Appreciate the help!
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.