SQL - choose column if the sum of other columns is above total - sql

I have a table that looks like this:
id , name , price , date
1 a 100 1.3.12
2 b 230 1.3.12
3 a 300 1.3.12
4 c 1000 1.3.12
5 b 160 1.3.12
6 a 400 1.3.12
I want to display the names whose total price is above some value, let's say 500.
So if a has 100, 300, 400 then its total is 800 > 500 -> display.
But I want to do it with just one query. So far, I managed to get this query:
SELECT name SUM (price) AS total FROM table GROUP BY name
But then I need to do one more in order to select total > 500.
How do I do it? thank you in advance.

You should use HAVING clause to apply a condition to an aggregated function:
SELECT name
FROM yourtable
GROUP BY name
HAVING SUM(price)>500

Use having to filter
select name, sum(price) as total_price
from your_table
group by name
having total_price > 500

Related

How to get first row of each group without using window functions Oracle?

How can i get first row of each group with all attributes without using window functions like row_number etc.
Example Data is:
id amount
1 100
1 200
2 150
2 300
2 250
3 400
What I want as a result:
id amount
1 200
2 300
3 400
No need for a window function:
SELECT id, MAX( amount ) AS amount
FROM your_table
GROUP BY id

Multiple rows with Maximum Values in sql

Can anyone help me with the below?
I'm trying to group by the (PO) and by (line) to get the Max of (Quantity) regardless of the value in (type).
I tried to group by PO and line both and it didn't work and showing this
error: "not group by function".
the data look like:
po
line
type
quantity
100
1
4
23
100
1
5
20
100
2
1
40
200
1
1
10
200
2
2
15
and I would like to get the below result:
po
line
type
quantity
100
1
4
23
100
2
1
40
200
1
1
10
200
2
2
15
the code:
select
orno "PO NUMBER",
pono "Line",
oltp "Line Type",
max(qoor) "Quantity"
from
table
group by orno;
One method is a correlated subquery:
select t.*
from t
where t.quantity = (select max(t2.quantity)
from t t2
where t2.po = t.po and t2.line = t.line
);
In Oracle, another option is aggregation:
select po, line,
max(type) keep (dense_rank first order by quantity desc) as type,
max(quantity) as quantity
from t
group by po, line;
The keep syntax is Oracle's fancy way of implementing a "first" aggregation function.

How do I use group by on one column which is having many entries?

I want to use group by on one column which is having many entries
table_a
Name price
AAA 12
BBB 13
AAA 0
CCC 24
AAA 0
DDD 0
Now I want to find out Name which is having Price as 0
but as I'm having entries AAA 3 times I can't directly write simple sql with condition
NOT Equal to 0
Please help me I want to print result for above table_a should be
only D as it is having 0 as price.
I assumed price cannot be lower than zero:
SELECT Name
FROM TableName
GROUP BY Name
HAVING SUM(price) = 0
Or with additional condition of only one price, which is zero
SELECT Name
FROM TableName
GROUP BY Name
HAVING COUNT(*) = 1 AND SUM(price) = 0
You can achive this by using aggregate functions GROUP BY and HAVING clause as:
SQLFiddle Demo
SELECT name,SUM(price) as price
FROM table
GROUP BY name
HAVING SUM(price) = 0
select name,max(price) as price from table group by name having max(price) =0
select name,max(price)
group by name
having max(price)=0

Sum of different column in SQL

We have an existing table with columns:
ItemCode nvarchar
Supplier1Price nvarchar
Supplier2Price nvarchar
Supplier3Price nvarchar
SelectedSupplier int (1, 2 or 3)
The table is used for canvassing from different suppliers. What I want to do is to get the sum of same items from selected suppliers.
Example:
ItemCode Supplier1Price Supplier2Price Supplier3Price SelectedSupplier
item-00 100 200 300 1
item-00 200 100 300 2
item-00 200 100 300 2
item-01 200 300 100 3
item-01 200 100 300 2
Result should be:
ItemCode Total
item-00 300
item-01 200
What I did is this:
select
ItemCode,
sum(SupplierPrice) as Total
from
(select
ItemCode,
case SelectedSupplier
when 1 then Supplier1Price
when 2 then Supplier2Price
when 3 then Supplier3Price
end) as SupplierPrice
from CanvassTable)
group by ItemCode
Note: First, code above selects all itemcodes and corresponding price (from selected supplier). The result then will be processed in order to get the sum of prices of each item.
It's working already, the problem is, I used subquery and I worry that when the table data grows the query will have poor performance. My question is is there any way I can do this without subquery?
If your query works, why not just do:
select
ItemCode,
SUM(case SelectedSupplier
when 1 then Supplier1Price
when 2 then Supplier2Price
when 3 then Supplier3Price
end) as SupplierPrice
from CanvassTable
group by ItemCode

how to group by and return sum row in Postgres

I am stuck here. I have row in Postgres like this:
id amount
a 5000
a 1500
a 500
b 2000
b 1000
c 4000
How is the sql syntax to get result like this?
id amount
a 7000
b 3000
c 4000
SELECT id, SUM(amount) AS amount
FROM yourtable
GROUP BY id