This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 5 years ago.
I tried to read about not a group by expression errors from some other posts, but all of them mention group functions such as MAX, MIN, etc. I'm not using any of it and it's a really simple query returning this sort of error.
SELECT *
FROM ad_voarnet_atendimento_pista
WHERE is_closed = 0
GROUP BY prefixo
ORDER BY prefixo
What am I doing wrong here?
Edit:
The expected result is the same that MySQL would give me with this query. It would exclude every duplicated value of the column PREFIXO. I want only 1 record of each value in the mentioned column.
The error message is this:
[Err] ORA-00979: not a GROUP BY expression
The GROUP BY is not useful outside of the context of an aggregate function like MIN() MAX() SUM() COUNT(), except perhaps to deduplicate rows. Just remove it. If you are looking to deduplicate results, use DISTINCT instead. If you use DISTINCT, it won't be of much value unless you are more specific about the columns in the SELECT list, excluding the primary key column.
SELECT
DISTINCT *
FROM
AD_VOARNET_ATENDIMENTO_PISTA
WHERE IS_CLOSED = 0
ORDER BY PREFIXO
GROUP BY is sometimes confused with ORDER BY. You already have an ORDER BY PREFIX0,
Related
This question already has answers here:
Column "invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"
(5 answers)
Closed 1 year ago.
I am writing a simple program using a database I've created but I have run into an issue, I am trying to determine the highest occurring referrer ID while also displaying the customer information
SELECT TOP (3) referrer_id,
COUNT(referrer_id) AS value_occurance,
customer_id,
CONCAT(first_name, ' ', last_name) AS full_name
FROM customer
GROUP BY referrer_id
ORDER BY value_occurance DESC
This is my query.
Column 'customer.customer_id' is invalid in the select list because it
is not contained in either an aggregate function or the GROUP BY
clause.
This is the error I am receiving.
One way to fix this is to add customer_id into your GROUP BY,
So GROUP BY referrer_id, customer_id
Since you use GROUPBY, you need the column to either be one of the columns you are grouping by, or the columns you are aggregating by. Otherwise, it is invalid to select this column.
To fix, either add this column to the GROUPBY, or use an aggregation function using the customer column
This question already has answers here:
SQL - WHERE Condition on SUM()
(4 answers)
Closed 5 years ago.
For a simplified example of my issue, why could I not do this?
select id_number, sum(revenue)
from table A
where sum(revenue)>1000
group by id_number
(In case this causes any confusion, why can I not only return the results that have over 1000 in revenue?)
Disclaimer, I'm somewhat new to SQL but couldn't find any documentation regarding this.
Thanks,
This is by design of SQL. By using WHERE You filter the source table. And the sequence of statement fragments is as written. That means You would like to filter the SUM which is applied on filtered table. That means You must use filter on already grouped result using HAVING clause. Use
select id_number, sum(revenue)
from table A
group by id_number
having sum(revenue) > 1000
Simple answer is because the WHERE clause is evaluated before the aggregation clause. Therefore, you are trying to filter based on something that doesn't exist yet. However, you can solve that problem by making it exist first. Write a subquery, then select from that:
WITH RevenueTotals AS (SELECT id_number, sum(revenue) AS Rev_Total
FROM table A
GROUP BY id_number)
SELECT id_number, Rev_Total
FROM RevenueTotals
WHERE Rev_Total > 1000
This question already has answers here:
Using an Alias column in the where clause in Postgresql
(6 answers)
Closed 6 years ago.
SELECT nmemail as order_email,
dtorder,
vlOrder,
cohorts.cohortdate
FROM factorderline
JOIN (SELECT nmemail as cohort_email, Min(dtorder) AS cohortDate FROM factorderline GROUP BY cohort_email limit 5) cohorts
ON order_email= cohort_email limit 5;
ERROR: column "order_email" does not exist
What is the problem with this query?
The problem is most likely that the definition of the column alias hasn't been parsed at the time the join is evaluated; use the actual column name instead:
SELECT nmemail as order_email,
dtorder,
vlOrder,
cohorts.cohortdate
FROM factorderline
JOIN (
SELECT nmemail as cohort_email, Min(dtorder) AS cohortDate
FROM factorderline
GROUP BY cohort_email limit 5
) cohorts ON nmemail = cohort_email
limit 5;
Also, when using limit, you really should use an order by clause.
From the docs:
When using LIMIT, it is important to use an ORDER BY clause that
constrains the result rows into a unique order. Otherwise you will get
an unpredictable subset of the query's rows.
The problem is that output column names can't be used in joins.
From the documentation:
An output column's name can be used to refer to the column's value in ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses; there you must write out the expression instead.
This question already has answers here:
Is there any difference between GROUP BY and DISTINCT
(26 answers)
Closed 8 years ago.
I believe GROUP BY in SQL would make DISTINCT unnecessary because if you group by a column then there will only be one of each value in the result, but I want to make sure I am understanding the keywords correctly. Is it the case that you would not need to do this:
SELECT DISTINCT a_uuid
FROM table
GROUP BY a_uuid
HAVING NOT bool_or(type = 'Purchase')
because you could just drop the DISTINCT completely?
You do not need the distinct in this query. In general, you don't need distinct with group by. There are actually some queries where distinct and group by go together, but they are very rare.
You need group by in this query, because you are using an aggregation function in the having clause. So, use:
SELECT a_uuid
FROM table
GROUP BY a_uuid
HAVING NOT bool_or(type = 'Purchase')
As long as aggregate functions aren't involved you can use DISTINCT instead of GROUP BY.
Use either DISTINCT or GROUP BY - not both!
Use DISTINCT if you just want to remove duplicates. Use GROUPY BY if you want to apply aggregate operators (MAX, SUM, GROUP_CONCAT, ..., or a HAVING clause).
If you use DISTINCT with multiple columns, the result set won't be grouped as it will with GROUP BY, and you can't use aggregate functions with DISTINCT.
Overall, these two are different in functionality matter, however, it happens that these two return same output for the particular set of data.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
SQL: What's the difference between HAVING and WHERE?
What is the difference between using having clause and where clause. Could any one explain in detail.
HAVING filters grouped elements,
WHERE filters ungrouped elements.
Example 1:
SELECT col1, col2 FROM table
WHERE col1 = #id
Example 2:
SELECT SUM(col1), col2 FROM table
GROUP BY col2
HAVING SUM(col1) > 10
Because the HAVING condition can only be applied in the second example AFTER the grouping has occurred, you could not rewrite it as a WHERE clause.
Example 3:
SELECT SUM(col1), col2 FROM table
WHERE col1 = #id
GROUP BY col2
HAVING SUM(col1) > 10
demonstrates how you might use both WHERE and HAVING together:
The table data is first filtered by col1 = #id
then the filtered data is grouped
then the grouped data is filtered again by SUM(col1) > 10
WHERE filters rows before they are grouped in GROUP BY clause
while HAVING filters the aggregate values after GROUP BY takes place
HAVING specifies a search for something used in the SELECT statement.
In other words.
HAVING applies to groups.
WHERE applies to rows.
Without a GROUP BY, there is no difference (but HAVING looks strange then)
With a GROUP BY
HAVING is for testing condition on the aggregate (MAX, SUM, COUNT etc)
HAVING column = 1 is the same as WHERE column = 1 (no aggregate on column )
WHERE COUNT(*) = 1 is not allowed.
HAVING COUNT(*) = 1 is allowed
Having is for use with an aggregate such as Sum. Where is for all other cases.
They specify a search condition for a group or an aggregate. But the difference is that HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause. Having Clause is basically used only with the GROUP BY function in a query whereas WHERE Clause is applied to each row before they are part of the GROUP BY function in a query.
As other already said, having is used with group by. The reason is the order of execution - where is executed before group by, having is executed after it
Think of it as a matter of where the filtering happens.
When you specify a where clause you filter input rows to your aggregate function (ie: I only want to get the average age on persons living in a specific city.) When you specify a having constraint you specify that you only want a certain subset of the averages. (I only want to see cities with an average age of 70 years or above.)
Having is for aggregate functions, e.g.
SELECT *
FROM foo
GROUP BY baz
HAVING COUNT(*) > 8