MySQL - Joining table on itself based on criteria from other table - mysql

Here’s the problem I am trying to solve:
Table1 has Product ID’s, dates, and prices for those dates, Table2 has Product attributes.
I want to be able to compare prices for a client for different products on the same date based on a set of attributes. I’m easily able to get a list of products/dates/prices for a ‘simple’ product, as well as an ‘advanced’ product (see below).
I want to be able to join these two tables such that the output looks like:
[CLIENT] [PRODUCT] [DATE] [SIM_PROD] [SIM_PRICE] [ADV_PROD] [ADV_PRICE]
Here is as far as I've made it
SELECT b.NAME AS ‘CLIENT’, a.NAME AS ‘SIMPLE_PRODUCT’, t1.DATE AS ‘DATE’, t1.PIVOT_PRICE AS ‘SIMPLE_PRICE’
FROM TABLE1 t1
LEFT JOIN PRODUCT a
ON t1.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = TRUE;
SELECT b.NAME AS ‘CLIENT’, a.NAME AS ‘ADV_PRODUCT’, t2.DATE AS ‘DATE’, t2.PIVOT_PRICE AS ‘ADV_PRICE’
FROM TABLE1 t2
LEFT JOIN PRODUCT a
ON t2.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = FALSE;
I've been able to build similar tables where I pull in price from TABLE1 labeling it as t1 then pull in price again from TABLE1 and labeling it as t2, but only when using criteria in TABLE1, not criteria in a table that needs to be joined.
Is it possible to 'set' a table (EG simple) then 'set' a second one (EG advanced) and then join them on PARTNER_ID and DATE?

You can join the two subqueries:
SELECT t1.client, t1.date, t1.simple_product, t1.simple_price, t2.adv_product, t2.adv_price
FROM (
SELECT b.NAME AS CLIENT, a.NAME AS SIMPLE_PRODUCT, t1.DATE, t1.PIVOT_PRICE AS SIMPLE_PRICE
FROM TABLE1 t1
LEFT JOIN PRODUCT a
ON t1.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = TRUE
) AS t1
JOIN (
SELECT b.NAME AS CLIENT, a.NAME AS SIMPLE_PRODUCT, t1.DATE, t1.PIVOT_PRICE AS SIMPLE_PRICE
FROM TABLE1 t1
LEFT JOIN PRODUCT a
ON t1.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = FALSE
) AS t2
ON t1.client = t2.client AND t1.date = t2.date
You'll probably need to select additional criteria and add them to the ON condition. Otherwise this will produce a full cross product between all the products that have the same client and date.
Your desired output has an additional PRODUCT column, but I couldn't see where that comes from.

Related

Performance issue - refactor select subqueries which are doing the same joins

In MySQL, how would you re-write a SELECT query which JOIN SELECT subqueries that are doing almost the same thing.
In my case, I am getting performance issues because in the subqueries, I need to join on the same tables for each subqueries which is an heavy operation.
Consider the below (ugly) example:
set #businessdate = '2020-01-24';
Select Daily.*, Monthly.Price, Yearly.Price FROM
# DAILY
(Select #businessdate as BusinessDate, t1.Owner , SUM(t.Price) AS DailyPrice
FROM Table t
INNER JOIN Table2 t2 on ...
INNER JOIN Table1 t1 on ...
where t2.date = #businessdate
GROUP BY t1.Owner) Daily
INNER JOIN
# MONTHLY
(Select SUM(t.Price) AS Price, t1.Owner
FROM Table t
INNER JOIN Table2 t2 on ...
INNER JOIN Table1 t1 on ...
where MONTH(t2.date) = MONTH(#businessdate) and YEAR(t2.date) = YEAR(#businessdate)
GROUP BY t1.Owner) Monthly
INNER JOIN
#YEARLY
(Select SUM(t.Price) AS Price, t1.Owner
FROM Table t
INNER JOIN Table2 t2 on ...
INNER JOIN Table1 t1 on ...
where YEAR(t2.date) = YEAR(#businessdate)
GROUP BY t1.Owner) Yearly
The reason is I need the date in my where clause (coming from t2), and one field in my group by coming from t1. I also need to display some fields from t1.
I only need the SUM(Price) (the Price from Monthly and Yearly...) so to me it sounds like the joins in subqueries is overkill even if it is working fine (very slow...)
You could use conditional aggregation with a single query:
Select
#businessdate as BusinessDate, ... ,
SUM(CASE WHEN t2.date = #businessdate THEN t.Price ELSE 0 END) AS DailyPrice,
SUM(CASE WHEN MONTH(t2.date) = MONTH(#businessdate) AND
YEAR(t2.date) = YEAR(#businessdate)
THEN t.Price ELSE 0 END) AS MonthlyPrice,
SUM(CASE WHEN YEAR(t2.date) = YEAR(#businessdate)
THEN t.Price ELSE 0 END) AS AnnualPrice
FROM Table t
INNER JOIN Table2 t2 on ...
INNER JOIN Table1 t1 on ...
GROUP BY t1.Owner
This at least would allow you to avoid unneeded passes over the joined tables. Note that your GROUP BY clause is invalid as written; the business date is the only valid single column which can be selected, other than the conditional sums.

Create View by Left join on multiple columns with OR

I am trying to create a view from two tables by left joining on two columns: t1.recipient_email = t2.username or t1.created_by = t2.id. As shown in pseudocode below, I want the first t2.name to be the recipient_name and second t2.name to be sender_name. I can't think of the correct way to achieve this.
CREATE VIEW emailsent_log_view
(id_email_que_log, date_sent, recipent_email, recipient_name, send_status, sender_name)
AS
SELECT
t1.id,
t1.date_send,
t1.recipient_email,
t2.name, --recipient_name: corresponds with t1.recipient_email = t2.username
t1.send_status,
t2.name --sender_name: correspond with t1.created_by = t2.id
FROM email_que_log AS t1
LEFT JOIN user_account as t2
ON t1.recipient_email = t2.username
OR t1.created_by = t2.id
As you guessed, you can't pick and choose which row joins to which row with an or condition like that. The way to solve such issues is to join the table twice, once for each need:
CREATE VIEW emailsent_log_view
(id_email_que_log, date_sent, recipent_email, recipient_name, send_status, sender_name)
AS
SELECT
eql.id,
eql.date_send,
eql.recipient_email,
res.name AS reciever, -- From the first join
eql.send_status,
snd.name AS sender -- From the second join
FROM
email_que_log AS eql
LEFT JOIN
user_account AS res ON eql.recipient_email = res.username
LEFT JOIN
user_account AS snd ON eql.created_by = snd.id

select from 2 tables with for statement in sql

i have 2 table and want to select data from them
table 1 :
id
name
table 2
id
name
table1.id
and i want a query to make this resualt:
table1.id
table1.name
count(table2.id)
this is simple and solved by this way :
SELECT
c.id as corridor_id,
c.name as corridor_name,
(SELECT COUNT( r.id ) FROM rooms AS r WHERE r.corridorid = c.id ) as room_count
FROM corridors AS c
now if i add another table like this :
table3
id
name
table2.id
and want a query like this :
table1.id
table1.name
count(table2.id)
count(table3.id)
idk how can i do such as this query, but if there is a way i'll be happy to find it, many tnx
You'll want to join them all together, and then Group them along these lines:
SELECT
t1.Id,
t1.Name,
Count(t2.Id) AS T2Count,
Count(t3.Id) AS T3Count
FROM table1 t1
JOIN table2 t2
ON t1.Id = t2.table1_id
JOIN table3 t3
ON t2.id = t3.table2_id
GROUP BY t1.Id, t1.Name
You don't need nested SELECT statement here. You can do it by grouping and to avoid double-counting you would want DISTINCT keyword:
SELECT
c.id as corridor_id,
c.name as corridor_name,
COUNT(DISTINCT r1.id),
COUNT(DISTINCT r2.id)
FROM
corridors c
JOIN rooms r ON r.corridorid = c.id
JOIN rooms2 r2 ON r2.corridorid = c.id
GROUP BY c.id
If you want to properly treat missing values (0 counts) you can also do this:
SELECT
c.id as corridor_id,
c.name as corridor_name,
IFNULL(COUNT(DISTINCT r1.id), 0),
IFNULL(COUNT(DISTINCT r2.id), 0)
FROM
corridors c
LEFT JOIN rooms r ON r.corridorid = c.id
LEFT JOIN rooms2 r2 ON r2.corridorid = c.id
GROUP BY c.id

Return zero or null when Joining 3 tables in mysql

I am trying to JOIN 3 Tables(table1, table2 & table3) in Mysql query where I want to pull the matching data from 2 tables(table1 & table2) comparing a Common Column existing in 3 tables(ie. 'PID').
When Joining these 3 tables, there is no data in table1 with the given Date('2012-12-27') then it's returning complete blank row.. Here, I want to get the matching data from the table2 matching the given Date and 'ZERO' or 'NULL' where there is no matching data in the other table ie. table1.. instead of a whole blank row.
Here is the code I was trying that returns a complete BLANK ROW..
SELECT * FROM table3 b
LEFT JOIN table1 r ON r.PID = b.PID
LEFT JOIN table2 a ON ab.PID = b.PID
WHERE b.Name ='stallion' AND r.Date = '2012-12-27' AND a.Date = '2012-12-27'
;
Use two different JOIN statement then UNION them.
The rows where there is no data in table1 (r) have r.Data = NULL and are therefore filtered away by your WHERE condition. You need to add OR r.Date IS NULL to your WHERE condition or move the condition to the ON clause:
SELECT * FROM table3 b
LEFT JOIN table1 r ON r.PID = b.PID AND r.Date = '2012-12-27'
LEFT JOIN table2 a ON a.PID = b.PID AND a.Date = '2012-12-27'
WHERE b.Name ='stallion';

Get distinct value from join query (Join Table)

How get a distinct value form more than one table (inner join query).
Eg,
select a.id,b.name,c.address
from table1 a
inner join table2 b on (a.id = b.row_id)
inner join table3 c on (a.id = c.ticket_id)
where c.status = 'open';
Here the scenario is for example, two rows contain the same a.id value so how to get the distinct value from a.id.
Somebody help me that how to get?
just add Distinct ...
select DISTINCT a.id,b.name,c.address
from table1 a
inner join table2 b on (a.id = b.row_id)
inner join table3 c on (a.id = c.ticket_id)
where c.status = 'open';
i think this is works fine..
if you need only one record distinct then it should be like this...
SELECT DISTINCT(cat_id) FROM PRODUCTS WHERE brand_id = 'sony'