Specified Expression not part of an aggregate function - sql

SELECT
Product_Line_ID=2 OR Product_Line_ID=3,
COUNT(Product_Finish), MIN(Standard_Price)
FROM Product_T
WHERE Product_Finish
GROUP BY Standard_Price
HAVING AVG(Standard_Price) <700
ORDER BY Product_FInish;
I keep getting this error: Your query does not include the specified expression 'Product_Line_ID=2 OR Product_Line_ID=3' as part of an aggregate function. Can anyone help me with this? Not sure how to select product line id that is 2 or 3.

What is confusing about the error message? Product_Line_ID=2 OR Product_Line_ID=3 is not valid SQL.
Your query basically makes no sense. You have boolean conditions in the SELECT, you have a WHERE clause with a column name and no conditions, you are ordering by the column you are counting.
I can guess that you intend something like this:
SELECT Product_Line_ID, COUNT(Product_Finish), MIN(Standard_Price)
FROM Product_T
WHERE Product_Line_ID IN (2, 3)
GROUP BY Product_Line_ID
HAVING AVG(Standard_Price) < 700
ORDER BY COUNT(Product_Finish);

Related

Column does not exist error when using Having clause

I'm following along the steps in this article and am having trouble with the following query:
SELECT Quantity, COUNT(*) AS Quantity_Counts
FROM sqlbank3
WHERE UnitPrice >= 5
GROUP BY Quantity
HAVING Quantity_Counts < 450
ORDER BY Quantity_Counts DESC
LIMIT 10;
ERROR: column "quantity_counts" does not exist
LINE 5: HAVING Quantity_Counts < 450
I put everything in lower case to see if that did anything but to no avail. I have created Quantity as an integer. The query runs when I remove the HAVING clause so it's certainly finding the Quantity_Counts column, just not with the HAVING clause.
Any help is appreciated.
The HAVING clause is supposed to be evaluated before the SELECT clause. You cannot use the alias defined in SELECT in HAVING for that reason.
You must use
HAVING COUNT(*) < 450
instead.
(Some DBMS allow it to use alias names in the HAVINGclause, though.)

any suggestions on how to overcome ORA-00937?

select s.S_FIRST||' '||s.S_LAST, sum(c.CREDITS) from enrollment e,student s,course c
where s.s_id=e.S_ID
and c.COURSE_NO=e.C_SEC_ID
group by s.S_ID
having sum(c.credits)>12 order by s.s_id;
Error report:
SQL Error: ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
It keeps coming back with an error, any suggestions ?
thanks for your cooperation
Actually there were a couple issues in the SQL. Try this...
select s.s_id, s.S_FIRST||' '||s.S_LAST name, sum(c.CREDITS) sum_credits
from enrollment e,student s,course c
where s.s_id=e.S_ID
and c.COURSE_NO=e.C_SEC_ID
group by 1, 2
having sum(c.credits) > 12
order by s.s_id;
You must group by all nonaggregate fields. Also I don't think you can order on a field not in your query (and hence would also need to be in the select list and group by). The error in the docs is ORA-00937.
Based on your invalid number comments, I think your join is wrong on course_no or maybe credits isn't a number or something, though.
You meed to include all your columns in select list to group by. See here http://www.dba-oracle.com/t_ora_00979_not_a_group_by_expression.htm
From documentation
Cause: The GROUP BY clause does not contain all the expressions in the
SELECT clause. SELECT expressions that are not included in a group
function, such as AVG, COUNT, MAX, MIN, SUM, STDDEV, or VARIANCE, must
be listed in the GROUP BY clause.
Action: Include in the GROUP BY clause all SELECT expressions that are
not group function arguments
Remedy, include all columns in select list to your group by clause. change your query like
select s.S_FIRST||' '||s.S_LAST as fullname, s.s_id,
sum(c.CREDITS) as total_credit from enrollment e,
student s,course c
where s.s_id=e.S_ID
and c.COURSE_NO=e.C_SEC_ID
group by s.S_FIRST||' '||s.S_LAST
having total_credit > 12
order by s.s_id;
Per Oracle spec error ORA-01722 means The attempted conversion of a character string to a number failed because the character string was not a valid numeric literal.
Make sure all your fields are of INT type or same type. IS c.CREDITS INT type?
are s.s_id and e.S_ID of same type?
are c.COURSE_NO and e.C_SEC_ID of same type?

Getting row count with other columns

I need to get some columns which are LinkID, ReplyCount and the most important one is TotalRowCount.
This is my code:
SELECT
TOP(10) link.LinkID, mesaj.ReplyCount
FROM
TBL_UserIcerikler AS link
INNER JOIN
TBL_UserMesajlar AS mesaj ON link.FromUserID = mesaj.UserID
WHERE
link.PublishDate >='2013-03-12 19:46:45.000'
ORDER BY
link.PublishDate DESC
It is not running anymore when I add Count(*) AS a".
I get this message instead. How can I get row count? Does anyone have any information about this topic?
Msg 208, Level 16, State 1, Line 1
Invalid object name 'TBL_UserIcerikler'
Count(*) is an aggregate function which returns the number of rows which have been summarised (not the number of rows returned by the query), so you must GROUP BY something and specify only the fields by which you group (or just return COUNT(*)).
It doesn't make a lot of sense to mix COUNT() and TOP().
For example :
SELECT link.LinkID, mesaj.ReplyCount, COUNT(*)
FROM TBL_UserIcerikler AS link
INNER JOIN TBL_UserMesajlar AS mesaj ON link.FromUserID = mesaj.UserID
WHERE link.PublishDate >='2013-03-12 19:46:45.000'
GROUP BY link.LinkID, mesaj.ReplyCount;
I know it's not quite what you want, but you haven't given quite enough explanation as to what you want to get out of your database.
That said, I think you might have forgotten a comma in the expression list.
Why not post your modified query.
Please read this MSDN explanation of group by, you will understand why you need it to get your total count.

SQL: How to return any Part that occurs more than Once

I have the following query which returns the following error:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
SELECT Part from Parts Where count(Part) > 1
How could i rewrite it to return the part that appears more than once.
You need to use a GROUP BY and HAVING clause like this:
SELECT part
FROM Parts
GROUP BY part
HAVING COUNT(*) > 1
A perfect opportunity for the rarely used HAVING clause:
SELECT Part, Count(Part) as PartCount
FROM Parts
GROUP BY Part
HAVING Count(Parts) > 1
try this:
select part from parts group by part having count(part) > 1

Group by SQL statement

So I got this statement, which works fine:
SELECT MAX(patient_history_date_bio) AS med_date, medication_name
FROM biological
WHERE patient_id = 12)
GROUP BY medication_name
But, I would like to have the corresponding medication_dose also. So I type this up
SELECT MAX(patient_history_date_bio) AS med_date, medication_name, medication_dose
FROM biological
WHERE (patient_id = 12)
GROUP BY medication_name
But, it gives me an error saying:
"coumn 'biological.medication_dose' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.".
So I try adding medication_dose to the GROUP BY clause, but then it gives me extra rows that I don't want.
I would like to get the latest row for each medication in my table. (The latest row is determined by the max function, getting the latest date).
How do I fix this problem?
Use:
SELECT b.medication_name,
b.patient_history_date_bio AS med_date,
b.medication_dose
FROM BIOLOGICAL b
JOIN (SELECT y.medication_name,
MAX(y.patient_history_date_bio) AS max_date
FROM BIOLOGICAL y
GROUP BY y.medication_name) x ON x.medication_name = b.medication_name
AND x.max_date = b.patient_history_date_bio
WHERE b.patient_id = ?
If you really have to, as one quick workaround, you can apply an aggregate function to your medication_dose such as MAX(medication_dose).
However note that this is normally an indication that you are either building the query incorrectly, or that you need to refactor/normalize your database schema. In your case, it looks like you are tackling the query incorrectly. The correct approach should the one suggested by OMG Poinies in another answer.
You may be interested in checking out the following interesting article which describes the reasons behind this error:
But WHY Must That Column Be Contained in an Aggregate Function or the GROUP BY clause?
You need to put max(medication_dose) in your select. Group by returns a result set that contains distinct values for fields in your group by clause, so apparently you have multiple records that have the same medication_name, but different doses, so you are getting two results.
By putting in max(medication_dose) it will return the maximum dose value for each medication_name. You can use any aggregate function on dose (max, min, avg, sum, etc.)