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.
Related
This question already has answers here:
Why no windowed functions in where clauses?
(8 answers)
Closed 5 years ago.
I have a table that consists of an order, items on that order, and then the quantity of the item ordered.
What I would like to do is create an additional column for 'Order quantity' which is the sum of item quantities grouped by order (see graphic of table below... order B has 30 total quantity split across three lines)
I can easily accomplish this using sum and partition:
SUM(quantity) OVER (PARTITION BY order_id) order_qty
However, what I need to is then filter to only those orders having quantity > 20. When I try to add that criteria to the WHERE or HAVING clauses, I get this error:
ORA-30483: window functions are not allowed here
One solution appears to be to wrap the whole SQL block inside of another SELECT FROM statement, and then add a WHERE clause to filter by order_qty. Overall that seems sloppy and non-intuitive... Is there a better solution to filter based on an aggregated value that is partitioned at a higher level?
Replace with
SUM(quantity) OVER (PARTITION BY order_id order by 1) order_qty
This question already has answers here:
"You tried to execute a query that does not include the specified aggregate function"
(3 answers)
Closed 6 years ago.
select Customers.cust_id, count(Orders.cust_id)
from Customers left outer join Orders
on Customers.cust_id=Orders.cust_id
group by Customers.cust_id
This correctly displays everything.
select Customers.cust_id, ***Customers.cust_name***, count(Orders.cust_id)
from Customers left outer join Orders
on Customers.cust_id=Orders.cust_id
group by Customers.cust_id
,,Your query does not include the specified expression 'cust_name' as port of an aggregate function."
Why is that? Each cust_id in Customers has a name in cust_name. Why do I get this error message?
When you use an aggregate function count() all other fields (that aren't used with an aggregate function) must appear in the Group By clause.
Here is my explanation as to why:
Aggregate functions operate across groups.
(That is, unless no groups or other fields are specified, in which case they operate across the whole recordset by default. For example, SELECT Sum(Salary) FROM Staff works.)
If you group by cust_id then it knows what to output, a count for each cust_id. But what would it do with the cust_name's? Which cust_name would it, or should it, display for each cust_id output? What if there are several cust_name's for a cust_id? It will only display one row for each cust_id, so what name should it display alongside it? It won't make the assumption that there is exactly one cust_name to correspond to one cust_id.
If there is one cust_name per cust_id then grouping by both will produce the same number of rows (as for cust_id alone) and provide consistent, and reliable, behaviour.
select Customers.cust_id, Customers.cust_name, count(Orders.cust_id)
from Customers left outer join Orders
on Customers.cust_id=Orders.cust_id
group by Customers.cust_id, Customers.cust_name
Do I have to select the column I want to group by?
I want to select sales numbers and group them by the month they were in, but I don't actually want the month in my results, just the sales numbers. Can I do this? I cant seem to get it to work:
SELECT Line_Item_Total
FROM CUSTOMER
GROUP BY MONTH(Actual_Setup_Date), YEAR(Actual_Setup_Date)
I should add this is for a delimited data chart in Filemaker.
You need an aggregation function on the rest of the columns, that is all:
SELECT SUM(Line_Item_Total )
FROM CUSTOMER
GROUP BY MONTH(Actual_Setup_Date), YEAR(Actual_Setup_Date)
You do not need to include the expressions in the group by in the select.
my Question is---> Each Sale made by an employee will get him 10% commission of each total sale amount, find out which employee has so far earned more commission with very less number of orders [Refer Northwind database]
i create a column 'com' to calculate commission percentage.
select distinct(EmployeeID) from orders where MIN(count(employeeid))and max(com)
when i run the sqlserver query it shows the error like
"An expression of non-boolean type specified in a context where a condition is expected, near 'and'."
There are several issues with your query. Briefly, you cannot leave the max() function without comparing it to some other expression. You also cannot specify the aggregation functions inside the where clause: for that purpose you will need to enclose them into (select max()....) subquery. But for your purpose, where you need to check the sum() and max() over the queried table, you need to specify them in the having as below:
SELECT EmployeeID
FROM orders
GROUP BY EmployeeID
HAVING SUM(debet)=
(SELECT MAX (sum(com)) from orders GROUP BY EmployeeID)
AND COUNT(ledger_id)=
(SELECT MIN(COUNT(EmployeeID)) FROM orders GROUP BY EmployeeID)
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.