SQL query - Impossible to repeat a sum on different lines - sql

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

Related

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

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

SQL Statement for displaying sum column in resulted query

I have three tables - the first table describes the project works and sub-works as the next :
PROJECT_ID
WORK_ID
MAIN_WORK_ID
WORK_NAME
1
10
1
Building-01
1
11
1
Building-01
The second table describes the work activities:
ACTIVITY_ID
PROJECT_ID
WORK_ID
ACTIVITY_NAME
1
1
10
Tiling
2
1
10
Metal Works
3
1
11
Wood Works
And the third table includes the activities cost:
ACTIVITY_ID
PROJECT_ID
ACTIVITY_COST
1
1
500
1
1
750
2
1
350
3
1
150
I have created this query to order the first table by work & sub-works arrangement:
SELECT
a.WORK_ID, a.MAIN_WORK_ID, a.WORK_NAME
FROM
PROJECTSWORKS a
WHERE
a.PROJECT_ID = 1
ORDER BY
CASE
WHEN a.WORK_ID = a.MAIN_WORK_ID THEN a.MAIN_WORK_ID
WHEN a.WORK_ID < a.MAIN_WORK_ID THEN a.WORK_ID
WHEN a.WORK_ID > a.MAIN_WORK_ID THEN a.MAIN_WORK_ID
END
Now I need the resulting table (From my query) to have addition column that contains the total cost for each sub-work, I know that I should use sub query or JOIN statements, but I don't know how to do it inside my query.
The query should return a result like this:
WORK_ID
Total_Cost
10
1600
11
150
You need to join the other two tables to take this result and then to sum(activity_cost) group by the others. Something like this:
SELECT distinct
a.WORK_ID, a.MAIN_WORK_ID, a.WORK_NAME,sum(c.activity_cost) total_cost
FROM
PROJECTSWORKS a join activities b on a.project_id=b.project_id
and a.work_id=b.work_id
join activities_cost c on c.activity_id=b.activity_id
WHERE
a.PROJEcT_ID = 1
group by a.WORK_ID, a.MAIN_WORK_ID, a.WORK_NAME
here is an example example

How to limit SQL query which uses join with Many-to-Many relationships? [duplicate]

I'm implementing pagination on my BD. My problem is when I want limit the SELECT statement but not the JOIN. Example, a product can got many prices:
SELECT * FROM product
LEFT JOIN price ON product.id == price.id_product
LIMIT 20
But I want to get 20 products with each one with their prices. How I can limit the statement SELECT, but not LEFT JOIN.
Example:
product price.id price.id_pruct price.price
1 1 1 50
2 2 1 30
3 3 1 40
4 1 20
5 2 30
SELECT * FROM product
LEFT JOIN price ON product.id == price.id_product
LIMIT 3
Return:
product price.id id_prodcut price
1 1 1 50
1 2 1 30
1 3 1 40
But I Want
product price.id id_prodcut price
1 1 1 50
1 2 1 30
1 3 1 40
1 4 1 20
2 5 2 30
3 . . .
Three products (limit 3)
Thanks. I hope you can help me.
Modify your query to limit the number of product rows before joining it to the price table. This means we want to to join the results of a query to a table, or in other words, we write a query containing a subquery:
SELECT *
FROM (
SELECT *
FROM product
ORDER BY id_product
LIMIT 3
) p
LEFT JOIN price ON p.id = price.id_product
Hope that helps.
I would write a subquery to get the three first products (or whatever condition you choose) like this:
SELECT id
FROM product
ORDER BY id
LIMIT 3;
Once I have that, I can select everything from the price table as long as the id is in that subquery. You can do this using a join:
SELECT p.*
FROM price p
JOIN(
SELECT id
FROM product
ORDER BY id
LIMIT 3) tmp ON tmp.id = p.product_id;
Here is an SQL Fiddle example using your sample data, and I also added a row that won't be returned so you can see that it works.

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;

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