SQL Error: ORA-00979: not a GROUP BY expression [duplicate] - sql

This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 8 years ago.
Can anyone please help me figure this out,
I have 3 tables: Customer, Products and Products_ordered and I am trying to find customers who have ordered more than 1 product. Here is my query:
SELECT customer_id, product_id
FROM product_ordered
GROUP BY customer_id
HAVING COUNT (customer_id)>1;
I am getting this error:
Error report:
SQL Error: ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
Thanks for helping

try
select customer_id, product_id from product_ordered group by customer_id,product_id having count (customer_id)>1;

Try:
SELECT customer_id
FROM product_ordered
GROUP BY customer_id
HAVING COUNT (customer_id)>1;
The issue is that product_id is not part of the group by. If you do a group by, you can only select columns in the group by or use an aggregate function. That query will return the customer_id's that occur more then once. I don't know your table structure, but if you want more data then just the id let us know what sql version you are using, SQL Sever, MYSQL, or Oracle, and I can try to write something with windowing functions.

Don't you really want to select Customers who ordered more than one product?
More than one order line, or more than one product, or more than one unique product?
If you run as an inline query
(select customer_id from product_ordered group by customer_id having count (customer_id) > 1)
You will see all customers who placed more than one order line. But there could be multiple lines in an order, or multiple orders of one line, yada yada...
Try select customer_id from product_ordered group by customer_id having count(distinct product_id)>1 which will let you actually see customers who bought more than one unique product.

Related

Unable to select column from other table while with COUNT and GROUP BY

I have two tables in a SQL Server database - IT_Order and Product. I want to group by the product_id from IT_Order but also select the product name from Product. But I get an error for this query. Please help...
The tables:
The query:
SELECT
Product.product_name, IT_Order.product_id,
COUNT(IT_Order.it_order_id) AS Product_volume
FROM
IT_Order, Product
WHERE
IT_Order.product_id = Product.product_id
GROUP BY
IT_Order.product_id;
I get this error:
Column 'Product.product_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
The error message is clear, you can use column without aggregation, in a GROUP BY, but as every product has only one name(i guess) you can make
SELECT Product.product_name, IT_Order.product_id, COUNT(IT_Order.it_order_id) as
Product_volume
FROM IT_Order JOIN Product
ON IT_Order.product_id = Product.product_id
GROUP BY IT_Order.product_id,Product.product_name;
also please use in future JOIN for your table, as they are around for 30 years now.

Query GROUP BY and COUNT

I'm new to SQL and taking COURSERA's "SQL for Data Science" course.I have the following question in a summary assignment:
Show the number of orders placed by each customer and sort the result by the number of orders in descending order.
Having failed to write the correct code, the answer would be as follows (of course one of several options):
SELECT *
,COUNT (InvoiceId) AS number_of_orders
FROM Invoices
GROUP BY CustomerId
ORDER BY number_of_orders DESC
I am still having trouble understanding the query logic. I would appreciate your assistance in understanding this query.
I seriously hope that Coursera isn't giving you the query you cited above as the recommended answer. It won't run on most databases, and even in cases such as MySQL where it might run, it is not completely correct. You should be using this version:
SELECT CustomerId, COUNT (InvoiceId) AS number_of_orders
FROM Invoices
GROUP BY CustomerId
ORDER BY number_of_orders DESC;
A basic rule of GROUP BY is that the only columns available for selection are those which appear in the GROUP BY clause. In addition to these columns, aggregates of any column(s) may also appear in the select. The version I gave you above follows these rules, and is ANSI compliant, meaning it would run on any database.
When you say SELECT * it represents ALL COLUMNS. But you are grouping by only CustomerId which is wrong in SQL.
Specify the other columns in the group section that you want to show
The script should be something like
SELECT CustomerName, DateEntered
,COUNT (InvoiceId) AS number_of_orders
FROM Invoices
GROUP BY CustomerId, CustomerName, DateEntered
ORDER BY number_of_orders DESC

Aggregate function error while using group by clause in SQL [duplicate]

This question already has answers here:
Reason for Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause [duplicate]
(4 answers)
Closed 3 years ago.
I have table named purchase. It has columns billno, billdate, qty, amount. When I run group by query, it is throwing an error.
Query I used
SELECT
BILLNO,
BILLDATE,
SUM(QTY) AS SUMQTY,
SUM(AMOUNT) AS SUMAMOUNT
FROM
PURCHASE
GROUP BY
BILLNO
This is the error I'm getting - how to get bill wise total amount?
Column 'PURCHASE.BILLDATE' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
The error is pretty obvious. The unaggregated columns in the SELECT of an aggregation query need to match the keys. In your query, BILLDATE is not aggregated and it is not a key.
The simple fix is:
SELECT BILLNO, BILLDATE,
SUM(QTY) AS SUMQTY,
SUM(AMOUNT) AS SUMAMOUNT
FROM PURCHASE
GROUP BY BILLNO, BILLDATE;
If you want only one row per BILLNO -- or if you know that BILLDATE is the same for all BILLNO -- then you can use an aggregation function instead:
SELECT BILLNO, MAX(BILLDATE) as BILLDATE,
SUM(QTY) AS SUMQTY,
SUM(AMOUNT) AS SUMAMOUNT
FROM PURCHASE
GROUP BY BILLNO;
According to the official documentation,‘The column must appear in the FROM clause of the SELECT statement, but is not required to appear in the SELECT list. However, each table or view column in any nonaggregate expression in the list must be included in the GROUP BY list’, it will indicate the cause of your error.
For more details , you can refer to this link and you will see some examples about your issue: https://learn.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-2017#arguments
There is a great example to explain for you : https://www.codeproject.com/Articles/1110163/%2FArticles%2F1110163%2FSQL-GROUP-By-and-the-Column-name-is-invalid-in-the
Here we have to revise the Concept of using the Group By and Order By in A SQL Query.
Remember One thing the name of column that has been came in Order By cannot be used inside aggregate function such as SUM, Average or MAX.
So don't put Same Column Name in Aggregate Function and with Order By too.

Aggregate functions in query against view in Oracle

I am utilizing some aggregate in a query against a column PRICE in a view called ORDERS.
Here how my ORDERS view was created
CREATE VIEW ORDERS as
SELECT CUSTOMER, SUM(total) PRICE
FROM RECORDS
GROUP BY CUSTOMER;
Here is my query:
SELECT PRICE, AVG(PRICE), MIN(PRICE), MAX(PRICE)
FROM ORDERS;
My error is:
Error report -
SQL Error: ORA-00979: not a GROUP BY expression
I'm really unfamiliar with the error and can't find a lot on the error beyond connection issues. How do I solve this?
Your query seems wrong as PRICE column is different and you are not grouping by it, I believe this is a typo and this is what causing your error... Exclude the price column like this:(although I haven't seen this error before, I assume its the reason)
SELECT AVG(PRICE), MIN(PRICE), MAX(PRICE)
FROM ORDERS;

SQL grouping results in a select

My SQL table "offers" contains offers users make for products (product_ID, customer_ID, offer).
In my admin page, I want to list the products for which at least one offer exists and show the total offers existing for it.
For example,
PRODUCT #324 Total offers: 42
PRODUCT #99 Total offers: 1
etc.
My guess would be to combine a
SELECT DISTINCT product_ID FROM offers...
And in a second query, to SELECT COUNT(*) FROM offers WHERE product_ID=...
Is it the most efficient way to achieve this, or is there a way to make it inside a single query?
You can do this in one query which will get the count by grouping by the product_id:
SELECT product_ID, COUNT(*)
FROM offers
GROUP BY product_ID
As bluefeet already answered, you achieve it in single query by using group by.
(group by demo)
Another thing to mention is the order by,
select
product_id as id,
count(*) as totals
from
t
group by product_id
order by totals;
If you want to sort with the totals of hits, or if you want to sort by product_id etc.
sqlfiddle