Hard for me to put in a coherent statement but I can give a sample set
ID STATUS DATE
1 A 2016-01-01
2 A 2016-01-01
2 B 2016-01-02
3 C 2016-01-13
4 D 2016-01-14
5 A 2016-01-15
5 B 2016-01-16
6 A 2016-01-17
7 C 2016-01-18
8 B 2016-01-19
9 B 2016-01-20
I want an sql statement that can determine two things:
1) How many items go from STATUS = A to a STATUS = B, with the same ID
2) I only want to show the rows with the aforementioned statuses - as follows:
ID STATUS DATE
2 A 2016-01-01
2 B 2016-01-02
5 A 2016-01-15
5 B 2016-01-16
COUNT(distinct ID) of that result should return 2 in this case
Any help would be appreciated
Join the table with itself, matching rows with the row after them with the same id.
SELECT t1.id, t1.status AS start_status, t1.date AS start_date,
t2.status AS end_status, t2.date AS end_date
FROM yourTable AS t1
JOIN yourTable AS t2 ON t1.id = t2.id AND t1.date = date_sub(t2.date, interval 1 day)
WHERE t1.status = 'A' AND t2.status = 'B'
This will show both rows together, e.g.
id start_status start_date end_status end_date
2 A 2016-01-01 B 2016-01-02
5 A 2016-01-15 B 2016-01-16
Related
This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 2 years ago.
I have two table that I want to combine without FULL OUTER JOIN as it not work in h2 db. I have to create select sql query:I have tried much with UNION too but I think it will not be possible with UNION.
Table 1
id c_id s_id p_date
---------------------------------------------
1 1 1 2020-10-10
2 1 1 2020-10-11
3 2 1 2020-10-11
4 2 2 2020-10-12
Table 2
id c_id s_id s_date
---------------------------------------------
1 1 1 2020-10-15
2 1 2 2020-10-16
3 2 2 2020-10-17
4 2 2 2020-10-17
I am expecting below result:
c_id s_id p_date s_date
-------------------------------------------------
1 1 2020-10-10 2020-10-15
1 1 2020-10-11 -
1 2 - 2020-10-16
2 1 2020-10-11 -
2 2 2020-10-12 2020-10-17
2 2 - 2020-10-17
Please help to get this result.
You can use union all like this:
select t1.c_id, t1.s_id, t1.p_date, t2.s_date
from table1 t1 left join
table2 t2
on t1.c_id = t2.c_id and t1.s_id = t2.s_id
union all
select t2.c_id, t2.s_id, t1.p_date, t2.s_date
from table2 t2 left join
table1 t1
on t1.c_id = t2.c_id and t1.s_id = t2.s_id
where t1.c_id is null;
The first subquery gets all the rows where there are matches between the two tables plus rows where table2 has no match for table1.
The second subquery gets the additional rows from table2 that have no match in table1.
Here is a db<>fiddle.
I have a table containing dates and a value:
table_1
-------
dt x
2018-01-01 1
2018-01-06 4
2018-01-07 2
2018-01-12 3
And I have another table containing dates:
table_2
-------
dt
2018-01-01
2018-01-03
2018-01-04
2018-01-06
2018-01-08
2018-01-09
2018-01-11
I want to create a new table my_joined_table that
contains all dates from table_2 and
for each date, contains the most recent value of table_1.x that is not newer than the date in the given row
So the result should be:
my_joined_table
---------------
dt x
2018-01-01 1
2018-01-03 1
2018-01-04 1
2018-01-06 4
2018-01-08 2
2018-01-09 2
2018-01-11 2
This is probably a standard problem, but I cannot seem to figure it out. Any help is appreciated.
This query will give you the result you want. It JOINs table_2 to table_1 on table_1 having the maximum dt less than or equal to the table_2 dt value:
SELECT t2.dt, t1.x
FROM table_2 t2
JOIN table_1 t1 ON t1.dt = (SELECT MAX(dt) FROM table_1 WHERE table_1.dt <= t2.dt)
Output:
dt x
2018-01-01 1
2018-01-03 1
2018-01-04 1
2018-01-06 4
2018-01-08 2
2018-01-09 2
2018-01-11 2
To create your my_joined_table table, just use a CREATE TABLE ... SELECT query:
CREATE TABLE my_joined_table AS
SELECT t2.dt, t1.x
FROM table_2 t2
JOIN table_1 t1 ON t1.dt = (SELECT MAX(dt) FROM table_1 WHERE table_1.dt <= t2.dt)
Demo on dbfiddle
I have two tables t1 and t2 with the same structure
id INT
userid INT
date DATETIME
The first table contains my data, while the second table is kind of helper table which contains rows for 10 fix dates and userid = -1
What i need is a SELECT which gives me all rows from t1 with userid=X joined(merged) with all rows from t2 which date is not already in the result of t1.
Pseudo code
SELECT id, date
FROM t1, t2
WHERE (t1.userid=:id OR t2.userid=-1) AND t2.date NOT IN t1.date
Sample:
t1:
id userid date
1 1 2015-12-01
2 1 2015-12-02
3 1 2015-12-03
4 2 2015-12-01
5 2 2015-12-02
t2:
id userid date
1 -1 2015-12-01
2 -1 2015-12-02
3 -1 2015-12-03
4 -1 2015-12-04
5 -1 2015-12-05
Expected output for userid=1:
1 1 2015-12-01
2 1 2015-12-02
3 1 2015-12-03
4 -1 2015-12-04
5 -1 2015-12-05
Thanks for your help
I'll use a union select for doing this.
SELECT
id, date
FROM
t1
WHERE
t1.id=:id
UNION ALL
(SELECT
id, date
FROM
t2
WHERE
t2.id=-1
AND t2.date NOT IN (SELECT date FROM t1 WHERE t1.userid=:id))
I have two tables like this:
table_1
id user_id item_id item_number
15 1 1 7
16 1 2 12
17 1 3 1
18 1 4 0
19 1 5 11
20 5 1 2
21 5 2 2
22 5 3 5
23 5 4 7
24 5 5 1
table_2
id user_id item_id attribute
41 5 1 1
42 1 1 1
43 7 5 1
44 1 4 1
45 1 4 0
I would like to select user_id, item_id and item_number from table_1 and number of rows from table_2 where table_1.user_id = table_2.user_id and table_1.item_id = table_2.item_id.
I have this:
SELECT item_id, item_number, COUNT(attribute) AS number
FROM table_1
LEFT OUTER JOIN table_2 ON table_1.user_id = table_2.user_id
WHERE table_1.user_id='1'
Can you help me?
Expected result:
user_id item_id item_number number
1 1 7 1
1 2 12 0
1 3 1 0
1 4 0 2
1 5 11 0
Try this:
SELECT t1.user_id, t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id AND t1.item_id = t2.item_id
WHERE t1.user_id = '1'
GROUP BY t1.user_id, t1.item_id;
you have to use alias as same column names confuses the server to identify which column have to fetch..so you can use this..
SELECT t1.user_id,t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id
WHERE t1.user_id='1' GROUP BY t1.user_id, t1.item_id;
Since you don't really need any column values from table_2, I'd go with a sub-query to get the count:
select user_id, item_id, item_number,
(select count(*) from table_2
where table_1.user_id = table_2.user_id
and table_1.item_id = table_2.item_id)
from table_1
WHERE table_1.user_id='1'
I have two following tables
table 1)
ID | HOTEL ID | NAME
1 100 xyz
2 101 pqr
3 102 abc
table 2)
ID | BOOKING ID | DEPARTURE DATE | AMOUNT
1 1 2013-04-12 100
2 1 2013-04-14 120
3 1 2013-04-9 90
4 2 2013-04-14 100
5 2 2013-04-18 150
6 3 2013-04-12 100
I want to get reault in mysql such that it take the row from table two with MAX DEPARTURE DATE.
ID | BOOKING ID | DEPARTURE DATE | AMOUNT
2 1 2013-04-14 120
5 2 2013-04-18 150
6 3 2013-04-12 100
SELECT b.ID,
b.BookingID,
a.Name,
b.departureDate,
b.Amount
FROM Table1 a
INNER JOIN Table2 b
ON a.ID = b.BookingID
INNER JOIN
(
SELECT BookingID, MAX(DepartureDate) Max_Date
FROM Table2
GROUP BY BookingID
) c ON b.BookingID = c.BookingID AND
b.DepartureDate = c.Max_date
SQLFiddle Demo
Well,
SELECT * FROM `table2` ORDER BY `DEPARTURE_DATE` DESC LIMIT 0,1
should help