Aggregate functions in query against view in Oracle - sql

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;

Related

show the sum of a specific variety to another table sql

Table Inventory
|Variety|Weight|Quantity|
|-------|--------|------|
|Native Chicken|1.6kg|10|
|Native Chicken|1.3|20|
|Chicken Broiler|2.1|30|
|Chicken Broiler|2.3|10|
|Duck|2.1|30|
|Duck|2.3|15|
|Turkey|2.1|30|
|Turkey|2.3|15|
How to get this desired Output for table Reports using the SELECT WHERE, Join and Sum statement in SQL. I've been trying to solve this for a few days now but I haven't seen any codes to answer for this one. Can you help your girl out? Thanks!
Table Reports
|Variety|Qty|
|-------|---|
|Native Chicken|30|
|Chicken Broiler|40|
|Duck|45|
|Turkey|45|
this is the code that I've tried but it's not working.
INSERT INTO reports(qty)
SELECT SUM(qty) FROM (SELECT Inventory.Quantity = "Native Chicken" FROM InventoryTable);
You just need to sum the quantity and group by the column that represents each quantity.
The group by "groups" all like values together where you then sum the quantites for each into a single value
insert into Reports (Variety, Qty)
select Variety, sum(Quantity)
from Inventory
group by Variety
Do a GROUP BY:
insert into Reports (Variety, Qty)
select Variety, sum(Quantity) as Qty
from Inventory
group by Variety
However, I'd consider a view instead of a Reports table, because a view will always have up-to-date data. A separate table requires being emptied and inserted into over and over again.
INSERT INTO reports
SELECT Variety, SUM(Quantity) FROM Inventory GROUP BY Variety;
See https://www.w3schools.com/sql/sql_insert_into_select.asp

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.

Whats the difference between these two SQL queries?

Question: Select the item and per unit price for each item in the items_ordered table. Hint: Divide the price by the quantity.
1.
select item, sum(price)/sum(quantity)
from items_ordered
group by item;
2.
select item, price/quantity
from items_ordered
group by item;
Have a look at the resultis for flashlights. First one shows average price correctly but 2nd one only takes 28/4 and shows 7, ignoring the 4.5 few rows down. Someone please explain why this is the case.
The used table data from an external website.
SUM() is a group function - so that essentially says go get me all the price and quantities by item, and add them all up to return them in one row.
MySQL is quite forgiving when grouping things and will try to retrieve a rowset (which is why your second example returns something - albeit wrong).
Generally, if you are GROUPing columns (items in your exmaple), you need to return one row per column (item).
Try running the SQL below to see what that looks like.
SELECT item
, SUM(price) AS sum_price
, SUM(quantity) AS sum_quantity
, COUNT(*) AS item_count
, SUM(price) / SUM(quantity) AS avg_price_per_quant
FROM items_ordered
GROUP BY item
ORDER BY item ASC
The first query returns the average price for that item, the second query returns the price for the first item it encounters. This only works in MySQL, the second query would error in SQL Server as no aggegrate function is used. See this post for more details Why does MySQL allow "group by" queries WITHOUT aggregate functions?.

why the following code shows the error? and what was the alternate?

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)

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

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.