How to query for column to primary key relation? [duplicate] - sql

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

Related

SQL query - Impossible to repeat a sum on different lines

I'm new to SQL and I can't solve this problem :
I have 2 tables in input :
The table "ORDER" :
Parcel_num
Order_num
A
1
B
1
C
1
D
2
The table "PARCEL" :
Parcel_num
Weight
A
1000
B
500
C
1500
D
1000
I have to display this tab in output :
Parcel_num
Order_num
Weight/order
A
1
3000
B
1
3000
C
1
3000
D
2
1000
I'm able to get the weight/order using this query :
> SELECT ORDER.Order_num, SUM(PARCEL.Weight) AS SUM_WEIGHT FROM PARCEL
> INNER JOIN ORDER ON PARCEL.Parcel_num=ORDER.Order_num GROUP BY
> ORDER.Order_num
But i'm not able to repeat this sum for each parcel.
I've tried using subqueries but I get errors anyway...
Try this:
SELECT PARCEL.Parcel_num, SUM(PARCEL.Weight) AS SUM_WEIGHT
FROM PARCEL
GROUP BY PARCEL.Parcel_num

How can I have Sub Sum column in sql query?

I have the following table in Sqlite
Person1 Person2 Amount
A X 1000
A Y 2000
A Z 5000
B X 3000
B Y 4000
how can I write a query to add an extra column that its value is the sum of Amount for each Person in Column Person1.
I need the following result:
Person1 Person2 Amount SumAmountPerson1
A X 1000 8000
A Y 2000 8000
A Z 5000 8000
B X 3000 5000
B Y 2000 5000
my query is:
select *, Sum(Amount) from MyTable
GROUP by Person1
But it returns the following result set in Sqlite:
Person1 Person2 Amount SumAmountPerson1
A X 1000 8000
B X 3000 5000
But I need all rows
SQLite does not support window functions, so one option to get your result would be to use a subquery to aggregate amounts by the first person, and then join this back to your original table.
SELECT t1.Person1,
t1.Person2,
t1.Amount,
t2.SumAmountPerson1
FROM yourTable t1
INNER JOIN
(
SELECT Person1, SUM(Amount) AS SumAmountPerson1
FROM yourTable
GROUP BY Person1
) t2
ON t1.Person1 = t2.Person1

Looping with SQL statements

I am trying to implement a small logic in SQL:
For example : I have two tables A and B
A B
ID Qnt ID Qnt Value
1 50 1 100 1000
2 130 2 200 1000
3 180 3 300 1000
4 320 4 400 2000
5 500 5 500 2000
6 600 2000
7 700 2000
I would to loop through each value of Qnt in TABLE A and check if the value lie between the range of the values in Qnt of TABLE B and get the corresponding value.
I know how I could achieve this with using While loop. But I don't want to do this since looping affects my query performance significantly. I would like to do this with only SQL statements.
Can anyone suggest an idea how I could go with this? just an idea would be great! Any sql would be fine, I would like to know just the logic.
The output would look like :
Output
ID Qnt Value
1 50 1000
2 130 1000
3 180 1000
4 320 2000
5 500 2000
Thanks
This is a lookup. You can do it with a correlated subquery, although the syntax is a bit different in the two databases. Here is the MySQL version:
select a.*,
(select b.value
from b
where b.qnt <= a.qnt
order by b.qnt desc
limit 1
) as value
from a;
Here is the SQL Server version:
select a.*,
(select top 1 b.value
from b
where b.qnt <= a.qnt
order by b.qnt desc
) as value
from a;

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

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

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