Select row of second table only if not exists in first one - mysql

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))

Related

SQL query for below data

For input, When quantity value greater then 1, convert in a new row with value 1 for quantity column.
INPUT
ID ProductFK Quantity Price
------------------------------------------------
10 1 2 100
11 2 3 150
12 1 1 120
OUTPUT
ID ProductFK Quantity Price
------------------------------------------------
10 1 1 100
10 1 1 100
11 2 1 150
11 2 1 150
11 2 1 150
12 1 1 120
We can do this using a sequence table trick. Inner join your current table to a sequence on the condition that the quantity be greater than or equal to the sequence value. For example:
SELECT t1.ID, t1.ProductFK, 1 AS Quantity, t1.Price
FROM yourTable t1
INNER JOIN (SELECT 1 AS Quantity UNION ALL SELECT 2 UNION ALL SELECT 3) t2
ON t1.Quantity >= t2.Quantity
ORDER BY t1.ID;
Demo

MySQL: Join on most recent date

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

Swap column values across 2 different tables

I have two tables and need to swap the values of a column in each table - I can do this when they are in the same table but when I try to do this with different tables then the second value is already overwritten so gets lost.
For example:
table1
id user_id currency col2 col3......
1 1 10 Bob 2018-04-16
2 2 150 Tom 2018-05-17
3 3 60 Phil 2018-06-04
4 4 125 Jon 2017-12-01
5 5 35 Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 USD 16 Active
2 2 USD 17 Active
3 3 GBP 21 Left
4 4 CAD 16 Active
5 5 AUD 19 Active
I need these to look like:
table1
id user_id currency col2 col3......
1 1 USD Bob 2018-04-16
2 2 USD Tom 2018-05-17
3 3 GBP Phil 2018-06-04
4 4 CAD Jon 2017-12-01
5 5 AUD Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 10 16 Active
2 2 150 17 Active
3 3 60 21 Left
4 4 125 16 Active
5 5 35 19 Active
I tried:
UPDATE table1 t1, table2 t2
SET t1.currency=t2.salary, t2.salary=t1.currency
WHERE t1.user_id=t2.user_id;
but this does not work (currency gets set correctly but not the salary), is it possible to do?
Swap two columns values between two tables looked like a possible solution but the solution is changing table names as all the columns need swopped whereas I only need single columns swapped.
I believe you'll need to use a mix of both DDL and DML to do this.
First off you'll need to rename one of the columns to be swapped and add a column to hold the new value:
alter table table1 change currency salary int;
alter table table1 add currency varchar(3) after salary;
then update each table independently:
update table1 t1, table2 t2
set t1.currency = t2.salary
where t1.user_id = t2.user_id;
update table1 t1, table2 t2
set t2.salary = t1.salary
where t1.user_id = t2.user_id;
and finally remove the extra column:
alter table table1 drop salary;

MySQL/ Hive : Join conditioned rows using windowing or analytical functions

I have two tables which I want to join with a specific logic.
Table_1 ( S_No, ID, Date1, Date2 )
S_No ID Date1 Date2
1 id1 2014-05-01 2014-07-03
2 id1 2015-03-23 2016-06-18
3 id1 2016-06-21 2016-07-29
Table_2 ( S_No_New, ID_New, Date_New )
S_No_New ID_New Date_New
2_1 id1 2014-04-25
2_2 id1 2014-06-14
2_3 id1 2015-01-10
2_4 id1 2015-02-15
2_5 id1 2015-05-17
2_6 id1 2016-04-24
2_7 id1 2016-06-19
2_8 id1 2016-06-25
2_9 id1 2016-07-11
2_10 id1 2016-08-11
2_11 id1 2016-08-16
I want to join above two table in such a way that I get a count of how many rows are there in table_2 before Date1 and between Date1 and Date2 and then when we move to the next row we have to use the data which is not counted yet so far for the same id.
And if we have a date entry in table_2 after the last Date2 entry in table 1 then we need to append a new row with '+1" added to S_No and similar remaining column details.
Overall this problem can be split into two parts :
1) Getting the counts column
2) Adding up the extra rows ( S_No "4" in this example )
Please drop an answer if you know solution to either of the two.
Final output :
S_No ID Date1 Date2 Count_pre Count_Between
1 id1 2014-05-01 2014-07-03 1 1
2 id1 2015-03-23 2016-06-18 2 2
3 id1 2016-06-21 2016-07-29 1 2
4 id1 NULL NULL 2 0
Logic :
S_No 1 :
Count_Pre = Dates before 2014-05-01
Count_between = Dates between 2014-05-01 and 2014-07-03
S_No 2 :
Count_Pre = Dates between 2014-07-03 and 2015-03-23
Count_between = Dates between 2015-03-23 and 2016-06-18
and so on
Intermediate table has to look something like this:
S_No ID Date Date2 S_No_New Date_New
1 id1 2014-05-01 2014-07-03 2_1 2014-04-25
1 id1 2014-05-01 2014-07-03 2_2 2014-06-14
2 id1 2015-03-23 2016-06-18 2_3 2015-01-10
2 id1 2015-03-23 2016-06-18 2_4 2015-02-15
2 id1 2015-03-23 2016-06-18 2_5 2015-05-17
2 id1 2015-03-23 2016-06-18 2_6 2016-04-24
3 id1 2016-06-21 2016-07-29 2_7 2016-06-19
3 id1 2016-06-21 2016-07-29 2_8 2016-06-25
3 id1 2016-06-21 2016-07-29 2_9 2016-07-11
4 id1 NULL NULL 2_10 2016-08-11
4 id1 NULL NULL 2_11 2016-08-16
I was trying out different windowing and analytics function but couldn't get through this problem.
Is it possible to do this kind of join in hive ( basic sql ) ?
NOTE : EDIT 2 : I need to implement this in hive and it supports all the builtin functions but not the variables from mysql. It supports aggregate, windowing and analytics funtions.
EDIT : Changed the date format from dd/mm/yyyy to yyyy-mm-dd
SELECT t.t1s_no,t.date1,t.date2,
sum(case when t.srce = 'P' then 1 else 0 end) as 'prev',
sum(case when t.srce = 'B' then 1 else 0 end) as 'between',
sum(case when t.srce = 'X' then 1 else 0 end) as 'missing'
FROM
(
SELECT S.*,
ROW_NUMBER() OVER (PARTITION BY S.DATE_NEW ORDER BY s.srce ,S.DATE1) RN
FROM
(SELECT 'P' AS SRCE,T1.S_NO T1S_NO,T1.ID T1ID,T1.DATE1 DATE1,T1.DATE2 DATE2,T2.DATE_NEW
FROM TABLE_1 T1
JOIN TABLE_2 T2 ON T2.DATE_NEW < T1.DATE1
UNION
SELECT 'B' AS SRCE,T1.S_NO T1S_NO,T1.ID T1ID,T1.DATE1 DATE1,T1.DATE2 DATE2,T2.DATE_NEW
FROM TABLE_1 T1
JOIN TABLE_2 T2 ON T2.DATE_NEW BETWEEN T1.DATE1 AND T1.DATE2
UNION
SELECT 'X' AS SRCE,4 T1S_NO,T1.ID T1ID,T1.DATE1 DATE1,T1.DATE2 DATE2,T2.DATE_NEW
FROM TABLE_2 T2
left JOIN TABLE_1 T1 ON (T2.DATE_NEW BETWEEN T1.DATE1 AND T1.DATE2) or (t2.date_new < t1.date1)
where t1.date1 is null
) S
) T
WHERE T.RN = 1
group by t.t1s_no,t.date1,t.date2
ORDER BY T.T1S_NO, T.DATE1
;
Result
t1s_no date1 date2 prev between missing
----------- ---------------- ---------------- ----------- ----------- -----------
1 2014-05-01 2014-07-03 1 1 0
2 2015-03-23 2016-06-18 2 2 0
3 2016-06-21 2016-07-29 1 2 0
4 NULL NULL 0 0 2
(4 rows affected)

Mysql find value that follows another

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