Left Join with condition from join - mysql

Suppose I have 2 tables:
Table_1 has |Product_ID|Max_Date
Table_2 has |Invoice_ID|Product_ID|Sale_Date|Amount_Sold
Table_2 stores all the Sales for the products.
I want to join the tables in such a way that I get a SUM(Amount_Sold) from Table_2 grouped by Product_ID but only considering the sales that occured before the Max_Date for each product from Table_1.
I tried
SELECT * FROM
(SELECT * FROM Table_1)a
LEFT JOIN
(SELECT Product_ID,Sale_Date,SUM(Amount_Sold) FROM Table_2 GROUP BY Product_ID)b
ON a.Product_ID=b.Product_ID AND b.Sale_Date<a.Max_Date
but it didn't return the correct sums.
I think the answer might be along these lines, but I have not been able to figure it out...
mysql: How to INNER JOIN a table but limit join to 1 result with the highest vote or count?
Any ideas?
Thanks in advance.

You can include the date criteria in your join conditional, which also makes the joins simpler to read, IMHO.
SELECT t2.product_id, sum(amount_sold)
FROM Table_1 t1 INNER JOIN Table_2 t2
ON t1.product_id = t2.product_id AND t2.sale_date <= t1.max_date
GROUP BY t2.product_id

Related

Select only up to 2 lines for orders which have products with more than 1 item ordered

I would like to display all orders which have more than 1 item but to display only 2 rows if they have more than 2 items
If you have any ideas i will appreciate it, thank you.
You already have a row number column for each id, which greatly simplifies the query. In this case, we can arrive at your expected output by just joining the ORDERS table to a subquery which identifies id having 2 or more records associated with them.
SELECT t1.*
FROM ORDERS t1
INNER JOIN
(
SELECT id
FROM ORDERS
GROUP BY id
HAVING COUNT(*) >= 2
) t2
ON t1.id = t2.id
WHERE t1.rown_num <= 2
try this simple solution
SELECT * FROM orders od WHERE od.id IN( SELECT id FROM orders o GROUP BY o.id HAVING o.id HAVING COUNT(o.id)>1)
try this:
the first join will give you the rows you want and the second join will add the rown_num field
select a.id,
a.item_num,
b.rown_num
from
(select id,
item_num
from orders
where rown_num in (1,2)
group by id,item_num
having count(id)>=2)a
inner join (select *
from orders )b on a.id=b.id
and a.item_num=b.item_num

How to join two tables with grouping in MySQL?

I have two tables,Table 1 and Table 2,Accid is the key to join two tables,
i want to sum revenueact and revenuutilz based on year and account, so out will look like this
in reality more data is there,when i join two tables and group by year only first account is coming,can anyone please help me on this?
You could try this:
SELECT
Accname,
YEAR,
SUM(revenueact) AS Revac,
SUM(revenuutilz) AS Revut
FROM table1 a
INNER JOIN Table2 b
ON a.Accid = b.Accid
GROUP BY Accname,Year
You cound use a join adn a group by
select t2.accname, sum(t1.revenueact), sum(t1.revenuutiliz), t1.year
from table1 t1
inner join table2 t2 on t1.accid = t2.accid
group by t2.accname, t1.year

Generic query for MySQL and PGSQL

I want to create a generic query which work with MySQL and PostgreSQL.
For this query, I need to select all column in 3 tables but the result need to have a distinct clause on the ID to eliminate duplicates rows.
Actually in the database, there is 6 records but just 3 are differents:
One of them appears 3 times, an other appears 2 and the last just on.
Records which appears many time are exactly the same and I just want keep one of each.
This is a picture of the 6 records:
And me I want that:
Here is the MySQL Query:
SELECT
*
FROM
table_1
INNER JOIN table_2
ON table_1.id = table_2.table_1_id
INNER JOIN table_3
ON table_2.table_3_id = table_3.id
WHERE
table_3.type = 'foo'
GROUP BY
table_1.id
And this is the PostgreSQL query:
SELECT DISTINCT ON (table_1.id)
*
FROM
table_1
INNER JOIN table_2
ON table_1.id = table_2.table_1.id
INNER JOIN table_3
ON table_2.table_3_id = table_3.id
WHERE
table_3.type = 'foo'
I don't find how to create just one query which work with MySQL and PostgreSQL
Try to first get the distinct ids of the first table, then join the rest on to them.
Though, you will still get somewhat random values for table_1 I guess, if there are multiples ids with different values to them and you not specifying which you want.
SELECT
table_1.*, table_2.*, table_3.*
FROM
(SELECT table_1.id FROM table_1 GROUP BY table_1.id) AS distinctIds
INNER JOIN table_1 ON table_1.id = distinctIds.id
INNER JOIN table_2 ON distinctIds.id = table_2.table_1_id
INNER JOIN table_3 ON table_2.table_3_id = table_3.id
WHERE
table_3.type = "foo"

How to know total entries of a particular state for all the psu

As I am running this query in MySql:
select count distinct(prim_key) IN table_1 inner join
table_2 on ID group by column name
I want to know total entries of a particular state for all the psu.
the table table_1 tells the total entries by the field agency.
Please help me.
select count(distinct prim_key)
from table_1 t1
inner join table_2 t2 on t1.ID = t2.ID
group by state
Try this::
select
count (distinct(prim_key))
from table_1
inner join table_2 on table1.ID=table_2.ID
group by column name

Query in multiple tables with MYSQL

I have 2 tables.
Table_1 has product 3 rows : ID, Quantity and Price.
Table_2 has 2 rows : ID, Special_note.
Not all products have a special note. When a product doesn't have a special note, there is no row for that product in table 2.
I'm trying to use a select query that will get all the information from table_1 but also grab the special note from table_2 when there's one.
The problem I'm having right now is that if there is no special note, it won't grab the information in table_1 at all.
I understand why it's doing it but I don't know how to fix the query so that it returns all the products whether there is a special note or not.
SELECT TABLE_1.ID, QUANTITY, PRICE, SPECIAL_NOTE
FROM TABLE_1, TABLE_2
WHERE TABLE_1.ID = TABLE_2.ID
I simplified the query a little bit for the purpose of this example.
Thanks for your help!
Use a LEFT OUTER JOIN:
SELECT t1.ID, t1.QUANTITY, t1.PRICE, t2.SPECIAL_NOTE
FROM TABLE_1 t1
LEFT OUTER JOIN TABLE_2 t2 ON t1.ID = t2.ID
Update:
To add a WHERE clause, e.g., where quantity >= 1, do this:
SELECT t1.ID, t1.QUANTITY, t1.PRICE, t2.SPECIAL_NOTE
FROM TABLE_1 t1
LEFT OUTER JOIN TABLE_2 t2 ON t1.ID = t2.ID
WHERE t1.QUANTITY >= 1