MySQL join query with multiple where condition - mysql

I have 3 tables. I need to join those 3 and get 2 fields from each table. And there will be few where conditions. Where condition is for date range. Even if one table has the the result I need to show it along with other table data showing as 0.
I have tried using inner join. But what it does is taking only 1st where condition and if no result in first it will not go for next conditions. My table structures and required output are shown below.
table1
+--------+---------+------------+
| amount | site_id | date |
+--------+---------+------------+
| 10 | 1 | 12/12/2014 |
| 50 | 2 | 10/12/2014 |
| 30 | 3 | 05/11/2014 |
+--------+---------+------------+
table2
+--------+---------+------------+
| amount | site_id | date |
+--------+---------+------------+
| 100 | 1 | 2/11/2014 |
| 40 | 2 | 10/10/2014 |
| 30 | 3 | 05/11/2014 |
+--------+---------+------------+
table3
+--------+---------+------------+
| amount | site_id | date |
+--------+---------+------------+
| 60 | 1 | 12/12/2014 |
| 50 | 3 | 11/12/2014 |
| 70 | 4 | 05/09/2014 |
+--------+---------+------------+
output : total amounts between 01/12/2014 and 31/12/2014
+---------+---------------+---------------+---------------+-------+
| site_id | table1_amount | table2_amount | table3_amount | total |
+---------+---------------+---------------+---------------+-------+
| 1 | 60 | 0 | 60 | 120|
| 3 | 0 | 0 | 50 | 50 |
+---------+---------------+---------------+---------------+-------+
Can anyone suggest a query to get this output?
This is what I have done so far
select sum(table1.amount),sum(table2.amount),sum(table3.amount),(sum(table1.amount)+sum(table2.amount)+sum(table3.amount)) from table1 inner join table2 on table1.site_id=table2.site_id inner join table3 on table3.site_id=table2.site_id where table1.date>='01/12/2014' and table1.date<='31/12/2014' or table2.date>='01/12/2014' and table2.date<='31/12/2014' or table3.date>='01/12/2014' and table3.date<='31/12/2014' group by table1.site_id

Try this:
SELECT S.site_id,
IFNULL(t1.table1_Amount, 0) AS table1_Amount,
IFNULL(t2.table2_Amount, 0) AS table2_Amount,
IFNULL(t3.table3_Amount, 0) AS table3_Amount,
(IFNULL(t1.table1_Amount, 0) + IFNULL(t2.table2_Amount, 0) + IFNULL(t3.table3_Amount, 0)) AS total
FROM Site S
LEFT OUTER JOIN ( SELECT t1.site_id, SUM(t1.amount) AS table1_Amount
FROM table1 t1
WHERE t1.date >= '01/12/2014' AND t1.date <= '31/12/2014'
GROUP BY t1.site_id
) AS t1 ON S.site_id = t1.site_id
LEFT OUTER JOIN ( SELECT t2.site_id, SUM(t2.amount) AS table2_Amount
FROM table2 t2
WHERE t2.date >= '01/12/2014' AND t2.date <= '31/12/2014'
GROUP BY t2.site_id
) AS t2 ON S.site_id = t2.site_id
LEFT OUTER JOIN ( SELECT t3.site_id, SUM(t3.amount) AS table3_Amount
FROM table1 t3
WHERE t3.date >= '01/12/2014' AND t3.date <= '31/12/2014'
GROUP BY t3.site_id
) AS t3 ON S.site_id = t3.site_id;

Related

How can I select rows with an extra column joined from the same table

I have a table like this:
id | path | name | date | data
---+-----------+------+------------+-----
1 | Docs | 1000 | 2022-01-01 | aaa0
2 | Docs/1000 | Text | 2022-01-11 | AAA0
3 | Docs | 1001 | 2022-02-02 | aaa1
4 | Docs/1001 | Text | 2022-02-12 | AAA1
How can I select all rows with path 'Docs' and add the date of the corresponding 'Text', i.e:
id | path | name | date | date_of_text | data
---+------+------+------------+--------------+-----
1 | Docs | 1000 | 2022-01-01 | 2022-01-11 | AAA0
3 | Docs | 1001 | 2022-02-02 | 2022-02-12 | AAA1
You can achieve the desired result with self join -
SELECT T1.id, T1.path, T1.name, T1.date, T2.date date_of_text, T2.data
FROM table_name T1
LEFT JOIN table_name T2 ON T1.name = SUBSTRING(path, POSITION("/" IN path) + 1, LENGTH(path))
WHERE T1.path = 'Docs'
Lots of ways to do this including
correlated sub query
select t.*,id % 2 ,(select date from t t1 where t1.ID = t.id + 1) datetext
from t
where id % 2 > 0;
self join
select t.*,t.id % 2 , t1.date
from t
join t t1 on t1.ID = t.id + 1
where t.id % 2 > 0;
Aggregation
select min(id) id,min(path) path,min(date) date,upper(data) data ,max(date) datetext
from t
group by t.data;

Is there better way to get the difference between two table with same fields?

I have two tables as below:
table1:
+----+----------+-------+
| id | order_id | price |
+----+----------+-------+
| 1 | 1024 | 20 |
| 2 | 1025 | 30 |
| 3 | 1026 | 35 |
| 4 | 1027 | 45 |
+----+----------+-------+
table2
+----+----------+-------+------+
| id | order_id | price | name |
+----+----------+-------+------+
| 1 | 1024 | 20 | a |
| 2 | 1025 | 30 | b |
| 3 | 1026 | 35 | c |
| 4 | 1027 | 40 | d |
+----+----------+-------+------+
What I want to do is just camparing fields order_id and price, and get the different content when order_id = 1027
Here is my humble opinion:
SELECT * FROM (
SELECT order_id, price FROM table1
UNION ALL
SELECT order_id, price FROM table2
) t
GROUP BY order_id, price
HAVING COUNT(*) = 1
# result
+----------+-------+
| order_id | price |
+----------+-------+
| 1027 | 40 |
| 1027 | 45 |
+----------+-------+
Is there any better way to get it.
Any commentary is very welcome. great thanks.
Another alternative would be to use a JOIN to find non-matching prices:
SELECT t1.order_id, t1.price AS table1_price, t2.price AS table2_price
FROM table1 t1
JOIN table2 t2 ON t2.order_id = t1.order_id AND t2.price != t1.price
Output:
order_id table1_price table2_price
1027 45 40
Demo on dbfiddle
If you also want to capture rows which exist in one table but not the other, then you will need a FULL OUTER JOIN, which MySQL doesn't support, and must be emulated using a UNION of a LEFT JOIN and a RIGHT JOIN:
SELECT *
FROM (SELECT t1.order_id AS order_id, t1.price AS table1_price, t2.price AS table2_price
FROM table1 t1
LEFT JOIN table2 t2 ON t2.order_id = t1.order_id
UNION
SELECT t2.order_id, t1.price AS table1_price, t2.price AS table2_price
FROM table1 t1
RIGHT JOIN table2 t2 ON t2.order_id = t1.order_id) t
WHERE table1_price != table2_price OR
table1_price IS NULL OR
table2_price IS NULL
Output:
order_id table1_price table2_price
1027 45 40
1028 50 null
1029 null 45
Demo on dbfiddle
You can use left join to get the values
SELECT table1.order_id, table1.price
FROM table1
LEFT JOIN table2 ON table2.order_id = table1.order_id AND table2.price != table1.price

MySQL: Enumerate and count

I have two tables, table1 and table2.
Example of the table1 table.
^ invoice ^ valid ^
| 10 | yes |
| 11 | yes |
| 12 | no |
Example of the table2 table
^ invoice ^ detail ^
| 10 | A |
| 10 | C |
| 10 | F |
| 11 | A |
| 11 | F |
| 10 | E |
| 12 | A |
Want to select from table 2 all rows that:
Have a valid invoice in table 1
And enumerate:
the detail for each invoice
the invoice
Here the desired result
^ invoice ^ detail ^ ordination ^ ordinationb ^
| 10 | A | 1 | 1 |
| 10 | C | 2 | 1 |
| 10 | F | 3 | 1 |
| 11 | A | 1 | 2 |
| 11 | F | 2 | 2 |
| 10 | E | 4 | 1 |
The sentence should valid for use in phpMyAdmin 4.8.4
Here is the MySQL 8+ way of doing this:
SELECT
t2.Invoice,
t2.`lines`,
ROW_NUMBER() OVER (PARTITION BY t2.Invoice ORDER BY t2.`lines`) line_order,
DENSE_RANK() OVER (ORDER BY t2.Invoice) ordination
FROM table2 t2
WHERE EXISTS (SELECT 1 FROM table1 t1 WHERE t1.Invoice = t2.Invoice AND t1.valid = 'yes');
Demo
If you are using a version of MySQL earlier than 8, then you might have to resort to using session variables. This can lead to an ugly query. If you have a long term need for queries like this one, then I recommending upgrading to MySQL 8+.
Edit:
It just dawned on me that we can use correlated subqueries to simulate both your ROW_NUMBER and DENSE_RANK requirements. Here is one way to do this query in MySQL 5.7 or earlier:
SELECT
t2.Invoice,
t2.detail,
(SELECT COUNT(*) FROM table2 t
WHERE t.Invoice = t2.Invoice AND t.detail <= t2.detail) ordination,
t.dr AS ordinationb
FROM table2 t2
INNER JOIN
(
SELECT DISTINCT
t2.Invoice,
(SELECT COUNT(*)
FROM (SELECT DISTINCT Invoice FROM table2) t
WHERE t.Invoice <= t2.Invoice) dr
FROM table2 t2
) t
ON t.Invoice = t2.Invoice
WHERE EXISTS (SELECT 1 FROM table1 t1 WHERE t1.Invoice = t2.Invoice AND t1.valid = 'yes')
ORDER BY
t2.Invoice,
t2.detail;
Demo

cumulative average in MySQL

I have a table with id and values shown below. is it possible to get another column which takes the value divided by the cumulative average as we go down the row?
original table : t1
+----+----------------------+
| id | Val |
+----+---------------------+-
| 1 | NULL |
| 2 | 136 |
| 3 | 42 |
table i want to get
+----+---------------------+-----------------------------+
| id | Val | VAL/(AVG(VAL) ) |
+----+---------------------+-----------------------------+
| 1 | NULL | NULL |
| 2 | 136 | 136/((136+0)/2)=2.000 |
| 3 | 42 | 42/((42+136+0)/3)=0.708 |
here is my query:
SELECT t1.id, t1.Val, Val/AVG(t1.Val)
FROM followers t1
JOIN followers t2
ON t2.id <= t1.id
group by t1.id;
however i get this instead:
+----+---------------------+----------------------+
| id | Val | VAL/(AVG(VAL) ) |
+----+---------------------+----------------------+
| 1 | NULL | NULL |
| 2 | 136 | 1.0000 |
| 3 | 42 | 1.0000 |
seems like AVG(Val) returns the same value from the col Val.
I was hoping to do something similar to this link here but instead of sum i want average.
MySQL SELECT function to sum current data
I re-implemented the edits and took rows with NULL into account:
+----+---------------------+---------------------+
| id | Val | VAL/(AVG(VAL) ) |
+----+---------------------+----------------------+
| 1 | NULL | NULL |
| 2 | 136 | 1.0000 |<---need this to = 2.000
| 3 | 42 | 0.4719 |<---need this to = 0.708
SELECT t1.id, t1.Val, t1.Val/(SUM(t2.Val)/(t1.id)) AS C
FROM followers t1
JOIN followers t2
ON t2.id <= t1.id
group by t1.id;
I think you want t2.val in the avg():
SELECT t1.id, t1.Val, t1.Val/AVG(t2.Val)
FROM followers t1 JOIN
followers t2
ON t2.id <= t1.id
group by t1.id;
EDIT:
Mike Brand is correct that the above is a lousy way to do what you want. In MySQL, you can do the same using variables:
select t.id, t.val,
(case when (#n := #n + 1) is null then null
when (#cumval := #cumval + val) is null then null
else t.val / (#cumval / #n)
end)
from followers t cross join
(select #n := 0, #cumval := 0) vars
order by t.id;
This might misbehave with NULL values of val, but it gives the idea for a faster way to do the calculation in MySQL.

Query MySQL Group by and Having

I am creating an application for my school and I am in trouble constructing the right query.
I have 2 tables,table1 and table2.
table1
---------------------------------------------------------
| StudentID | SubjectID | Present | Type |
---------------------------------------------------------
| 2 | 3 | yes | 1 |
| 2 | 2 | yes | 2 |
| 3 | 1 | no | 3 |
---------------------------------------------------------
table2
---------------------------------------------------------
| SubjectID | SubjectName | Number1 | Number2 |
---------------------------------------------------------
| 1 | Name1 | 6 | 4 |
| 2 | Name2 | 4 | 8 |
| 3 | Name3 | 5 | 2 |
---------------------------------------------------------
SubjectID in table1 is foreign key references table2.
I want to build a query sql that gives me the StudentID`s from table1
that didnt miss any Type 3 subject (i.e no row like this
---------------------------------------------------------
| StudentID | SubjectID | Present | Type |
---------------------------------------------------------
| 3 | 1 | no | 3 |
---------------------------------------------------------
And have completed 75 percent of type 1 (i.e
I find it like this
SELECT t1.StudentID,t1.SubjectID ,t1.Type,t2.Number1 as num
FROM table1 as t1,table2 as t2
WHERE t1.Present=yes and t2.SubjectID=t1.SubjectID
GROUP BY StudentID,SubjectID
HAVING COUNT(*)/num >= 75/100
But I cant combine the two things together.
You can combine queries by giving them aliases and joining as subqueries...
SELECT finisher.StudentID FROM
(
SELECT DISTINCT StudentID
FROM table1 t1
JOIN table2 t2 ON t2.SubjectID = t1.SubjectID
WHERE t1.Present = 'yes' AND t1.Type1 = 1
GROUP BY t1.StudentID, t2.SubjectID
HAVING COUNT(*) / t2.Number2 >= 0.75
) finisher
JOIN
(
SELECT DISTINCT t1.StudentID
FROM table1 t1
LEFT JOIN
(
SELECT DISTINCT StudentID
FROM table1
WHERE Type = 3 AND Present = 'no'
) missed ON missed.StudentID = t1.StudentID
WHERE t1.Type = 3
AND missed.StudentID IS NULL
) notmissed ON finisher.StudentID = notmissed.StudentID
"StudentID`s from table1 that didnt miss any Type 3"... I assume here you don't want to include students without any type 3 rows.
Seems like this is done and duste, but how about...
SELECT x.*
FROM
( SELECT t1.StudentID
, t1.SubjectID
, t1.Type
, t2.Number1 num
FROM table1 t1
JOIN table2 t2
ON t2.SubjectID=t1.SubjectID
WHERE t1.Present='yes'
GROUP
BY t1.StudentID
, t1.SubjectID
HAVING COUNT(*)/num >= 0.75
) x
LEFT
JOIN table1 y
ON y.student_id = x.student_id
AND y.subject_id = x.subject_id
AND y.type = 3
AND y.present = 'no'
WHERE y.student_id IS NULL;