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

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.

Related

Merge row values based on other column value

I'm trying to merge the values of two rows based on the value of another row in a different column. Below is my based table
Customer ID
Property ID
Bookings per customer
Cancellations per customer
A
1
0
1
B
2
10
1
C
3
100
1
C
4
100
1
D
5
20
1
Here is the SQL query I used
select customer_id, property_id, bookings_per_customer, cancellations_per_customer
from table
And this is what I want to see. Any ideas the query to get this would be? We use presto SQL
Thanks!
Customer ID
Property ID
Bookings per customer
Cancellations per customer
A
1
0
1
B
2
10
1
C
3 , 4
100
1
D
5
20
1
We can try:
SELECT
customer_id,
ARRAY_JOIN(ARRAY_AGG(property_id), ',') AS properties,
bookings_per_customer,
cancellations_per_customer
FROM yourTable
GROUP BY
customer_id,
bookings_per_customer,
cancellations_per_customer;

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

Return Records that span over two tables in a 1 to many relationship with multiple criteria on the many side

I am trying to get a count of records of data that spans over three tables. I have a table of products, a table of groups and a table showing which groups a product is in. As below:
Product
Product_ID
Product_Name
1
A Product
2
Another Product
Group
Group_ID
Group_Name
10
Group A
20
Group B
30
Group C
40
Group D
Product Grouping
Product_ID
Group_ID
1
10
1
20
1
30
2
20
2
40
3
50
I am trying to write a query that will, given the condition of group ids of say for example (10,20,30), it will return product 1. And if I give group ids of say (20), it will return product 1 and product 2
I have tried:
select * from product_grouping
where group_id = 10 and group_id = 20 and group_id = 30
Use aggregation and having:
select pg.group_id
from product_grouping pg
where pg.group_id in (10, 20 30)
group by product_id
having count(*) = 3;
If the product groupings can be duplicated, use having count(distinct pg.group_id) = 3.

SQL join query for a view with sum of columns across 3 tables

I have 3 tables as below
Table - travel_requests
id industry_id travel_cost stay_cost other_cost
1 2 1000 500 200
2 4 4000 100 200
3 5 3000 0 400
4 1 3000 250 100
5 1 200 100 75
Table - industry_tech_region
id industry_name
1 Auto
2 Aero
3 Machinery
4 Education
5 MTV
Table - industry_allocation
id industry_id allocation
1 1 500000
2 2 300000
3 3 500000
4 4 300000
5 5 500000
6 1 200000
I want to create a view which has 3 columns
industry_name, total_costs, total_allocation
I created a view as below
SELECT industry_tech_region.industry_name,
SUM(travel_requests.travel_cost + travel_requests.stay_cost + travel_requests.other_cost) AS total_cost,
SUM(industry_allocation.allocation) AS total_allocation
FROM industry_tech_region
INNER JOIN industry_allocation
ON industry_tech_region.id = industry_allocation.industry_id
INNER JOIN travel_requests
ON industry_tech_region.id = travel_requests.industry_id
GROUP BY industry_tech_region.industry_name
But the result I get is as below which is incorrect
industry_name total_cost total_allocation
Aero 1700 300000
Auto 7450 1400000 (wrong should be 3725 and 700000)
Education 4300 300000
MTV 3400 500000
This is probably happening because there are 2 entries for industry_id 1 in the travel_requests table. But they should be counted only once.
Please let me know how do we correct the view statement.
Also I want to add another column in view which is remaining_allocation which is difference of total_allocation and total_cost for each industry.
you shoud join the sum (and not sum the join)
select
a.industry_name
, t1.total_cost
, t2.total_allocation
from dbo.industry_tech_region a
left join (
select dbo.travel_requests.industry_id
, SUM(dbo.travel_requests.travel_cost + dbo.travel_requests.stay_cost + dbo.travel_requests.other_cost) AS total_cost
FROM bo.travel_requests
group by dbo.travel_requests.industry_id
) t1 on a.id = t1.industry_id
left join (
select dbo.industry_allocation.industry_id
, SUM(dbo.industry_allocation.allocation) AS total_allocation
from dbo.industry_allocation
group by dbo.industry_allocation.industry_id
) t2 on a.id = t2.industry_id
this happen because you have two entry for the industry_id 1 and then the row are joined two time if you use the subquery for aggreated the row this can't happen ...
I have used left join because seems that not all the industry_id match for the 3 tables ..
You can use this approach too (without the ORDER BY because views do not allow it).
;WITH q AS (
SELECT industry_id
, sum(allocation) AS total_allocation
FROM #industry_allocation
GROUP BY industry_id
)
SELECT #industry_tech_region.industry_name
, isnull(SUM(#travel_request.travel_cost
+ #travel_request.stay_cost
+ #travel_request.other_cost),0.0) AS total_cost
,q.total_allocation AS total_allocation
FROM #industry_tech_region
LEFT JOIN q ON #industry_tech_region.id = q.industry_id
LEFT JOIN #travel_request ON #industry_tech_region.id = #travel_request.industry_id
GROUP BY #industry_tech_region.industry_name,q.total_allocation
ORDER BY industry_name

Select TOP 2 maximum values in SQL

I neet to select TOP 2 products with Max Price.
ProductID ProductName SupplierID CategoryID Unit Price
1 Chais 1 1 10 bags 18
2 Chang 1 1 24 bottles 19
3 Aniseed Syrup 1 2 12 bottles 10
I have used:
Select TOP 2 *
from Products
where Price = (Select Max(Price) from Products);
But the result is just 1 row.
This will give you the top 2 max prices but if the same price is in there twice you would get the same value twice, but from your code above that is what it would do so assuming that is what you are looking for.
Select TOP 2 * from Products order by Price DESC
You need order by clause :
select top (2) *
from Products p
order by price desc;