This is my table
table_datetime1
2018-03-01 00:05:00
2018-03-01 00:10:00
2018-03-01 00:15:00
2018-03-01 00:20:00
2018-03-01 00:25:00
table_datetime2
2018-03-01 00:05:00
2018-03-01 00:10:00
2018-03-01 00:15:00
2018-03-01 00:20:00
2018-03-01 00:30:00
in this case 2 records are not and I want to show me in the query those 2 records
example
2018-03-01 00:25:00
2018-03-01 00:30:00
This should give you both records:
select * from table1 a
left join table2 b on b.table_datetime2 = a.table_datetime1
where b.table_datetime2 is null
union
select * from table2 a
left join table1 b on b.table_datetime1 = a.table_datetime2
where b.table_datetime1 is null
Related
Here I have a query which returning aggregate of values based on 5 minutes intervals of time stamp.
Here is the query
SELECT
DATE_FORMAT(`recived_on`,'%Y-%m-%d %H:00') +
INTERVAL (MINUTE(`recived_on`) - MINUTE(`recived_on`) MOD 5) MINUTE AS receivedOn,SUM(quantity) AS Quantity
FROM tablename WHERE DATE(`recived_on`) = '20210129'
GROUP BY DATE_FORMAT(`recived_on`,'%Y-%m-%d %H:00') +
INTERVAL (MINUTE(`recived_on`) - MINUTE(`recived_on`) MOD 5) MINUTE;
This query is returning values like below.
2021-01-29 00:05:00 1
2021-01-29 00:15:00 1
2021-01-29 00:45:00 1
2021-01-29 01:05:00 1
2021-01-29 03:00:00 1
2021-01-29 04:45:00 1
2021-01-29 06:15:00 2
2021-01-29 06:40:00 1
For example between 00:05:00 and 00:15:00 there were no records . So it was not showing anything. But I need timestamp data like data for every 5 mins even it is zero . For example if there were no records then it should return 0 like below
2021-01-29 00:05:00 1
2021-01-29 00:10:00 0
2021-01-29 00:15:00 1
2021-01-29 00:20:00 0
2021-01-29 00:25:00 0
2021-01-29 00:30:00 0
2021-01-29 00:35:00 0
2021-01-29 00:40:00 0
2021-01-29 00:45:00 0
Any help would be greatly appreciated.
I need to join two tables on fake_id but table 2 contains more than one matching records for fake_id so I need to match with record where table2.end_time >= table1.event_time and table2.start_time <= table1.event_time
If there are more than one record in table 2 matching this condition, I need to only consider latest by updated_time
Here is what I tried.
spark.sql("select t1.fake_id, t1.attribute_1,t1.event_time,t22.end_time from table1 t1 left outer join (
select fake_id, end_time from table2 t2 where t2.fake_id=t1.fake_id and t2.end_time >= t1.event_time and t2.start_time <= t1.event_time order by t2.updated_time desc limit 1)
as t22 on t1.fake_id=t22.fake_id")
For above statement spark throwing me error for unknown column t1.fake_id
Table.1 -
---------------------------------------------------------------------------
fake_id attribute_1 event_time
---------------------------------------------------------------------------
1 attr_val_11 2020-08-01 05:00:00
2 attr_val_12 2020-08-01 15:00:00
3 attr_val_31 2020-08-03 07:00:00
4 attr_val_41 2020-08-01 05:00:00
Table.2 -
---------------------------------------------------------------------------
fake_id start_time end_time updated_time
---------------------------------------------------------------------------
1 2020-08-01 02:00:00 2020-08-01 08:00:00 2020-08-01 00:00:00
2 2020-08-01 04:00:00 2020-08-01 23:00:00 2020-08-01 00:00:00
3 2020-08-03 02:00:00 2020-08-03 08:00:00 2020-08-03 08:00:00
3 2020-08-03 05:00:00 2020-08-03 10:00:00 2020-08-03 12:00:00
3 2020-08-04 05:00:00 2020-08-04 10:00:00 2020-08-04 12:00:00
4 2020-08-01 08:00:00 2020-08-01 18:00:00 2020-08-01 18:00:00
4 2020-08-01 02:00:00 2020-08-01 05:00:00 2020-08-01 22:00:00
Result :
----------------------------------------------------------------------------------------------
fake_id attribute_1 event_time start_time end_time
----------------------------------------------------------------------------------------------
1 attr_val_11 2020-08-01 05:00:00 2020-08-01 02:00:00 2020-08-01 08:00:00
2 attr_val_12 2020-08-01 15:00:00 2020-08-01 04:00:00 2020-08-01 23:00:00
3 attr_val_31 2020-08-03 07:00:00 2020-08-03 05:00:00 2020-08-03 10:00:00
4 attr_val_41 2020-08-01 05:00:00 2020-08-01 02:00:00 2020-08-01 05:00:00
Use the between and get the row_number, sort and take the maximum update time.
spark.sql('''
select
fake_id,
attribute_1,
event_time,
start_time,
end_time
from (
select
t1.fake_id,
t1.attribute_1,
t1.event_time,
t2.start_time,
t2.end_time,
row_number() OVER (PARTITION BY t1.fake_id, t1.attribute_1 ORDER BY t2.updated_time DESC) as rank
from
table1 t1
left join
table2 t2
on
t1.fake_id = t2.fake_id and
t1.event_time between t2.start_time and t2.end_time) t
where
rank = 1
order by
fake_id
''').show()
+-------+-----------+-------------------+-------------------+-------------------+
|fake_id|attribute_1| event_time| start_time| end_time|
+-------+-----------+-------------------+-------------------+-------------------+
| 1|attr_val_11|2020-08-01 05:00:00|2020-08-01 02:00:00|2020-08-01 08:00:00|
| 2|attr_val_12|2020-08-01 15:00:00|2020-08-01 04:00:00|2020-08-01 23:00:00|
| 3|attr_val_31|2020-08-03 07:00:00|2020-08-03 05:00:00|2020-08-03 10:00:00|
| 4|attr_val_41|2020-08-01 05:00:00|2020-08-01 02:00:00|2020-08-01 05:00:00|
+-------+-----------+-------------------+-------------------+-------------------+
select a.no, a.Dtime,count(b.Dtime)+1 as Rank
from table1 a left
join table1 b on a.Dtime>b.Dtime and a.no=b.no
group by a.no,a.Dtime
order by a.no, a.Dtime
table1 Input:
NO Dtime
1 08:10:00
1 09:10:00
1 09:40:00
1 10:10:00
2 09:30:00
2 10:15:00
3 09:00:00
Output:
NO Dtime Rank
1 08:10:00 1
1 09:10:00 2
1 09:40:00 3
1 10:10:00 4
2 09:30:00 1
2 10:15:00 2
3 09:00:00 1
But I am looking for Output in mysql where table2 Rank links to table1 and table2 Dtime i.e. table1.Dtime>table2.time
table2 Input
NO Dtime
1 08:30:00
1 09:15:00
1 09:50:00
2 08:30:00
2 09:45:00
3 09:50:00
Output:
NO table1.Dtime Rank table2.Dtime
1 08:10:00 0 00:00:00
1 09:10:00 1 08:30:00
1 09:40:00 2 09:15:00
1 10:10:00 3 09:50:00
2 09:30:00 1 08:30:00
2 10:15:00 2 09:45:00
3 09:00:00 0 00:00:00
You can use the same approach with your initial query. Just left join to table2. To get the Dtime from table2 you can use a correlated subquery:
select a.no, a.Dtime,
count(b.Dtime) as Rank,
coalesce((select c.Dtime
from table2 as c
where c.no = a.no and a.Dtime > c.Dtime
order by c.Dtime desc limit 1), '00:00:00') as t2Dtime
from table1 a
left join table2 b on a.Dtime > b.Dtime and a.no = b.no
group by a.no,a.Dtime
order by a.no, a.Dtime
Demo here
I have the following table in mysql:
Key DI CI FD FA NM Valid_from Valid_to
0 1224468 123 2012-06-30 3 6 2013-01-23 9999-12-31
1 1234567 123 2013-12-31 3 10 2014-02-27 2014-03-10
2 1234567 123 2013-12-31 2 12 2014-03-10 9999-12-31
3 1234579 123 2013-12-31 3 12 2014-05-15 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
6 1224470 123 2015-11-11 2 12 2015-11-11 9999-12-31
7 1224471 123 2015-11-11 3 15 2015-11-11 9999-12-31
8 1224472 123 2015-11-10 2 13 2015-11-10 9999-12-31
9 1224473 123 2015-11-10 3 12 2015-11-10 9999-12-31
If there are records which has the same "FD", I need to get the ones which 's "FA" is "1", if exists.
Basically, I want this output.
Key DI CI FD FA NM Valid_from Valid_to
0 1224468 123 2012-06-30 3 6 2013-01-23 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
8 1224472 123 2015-11-10 2 13 2015-11-10 9999-12-31
9 1224473 123 2015-11-10 3 12 2015-11-10 9999-12-31
It looks a complicated query, and I couldn't manage to do it.
How can I do it?
Thanks
Looks like a job for
...
GROUP BY FD
...
HAVING COUNT(FD) > 1
I don't see why you'd want 2012-06-30 in your results? I thought you only wanted ones where there is an FA of 1?
Try out following query:
select * from tbl group by fd having count(*)=1
union all
select t1.* from tbl t1 inner join (
select max(`key`) as `key` from tbl where fa=1 group by fd
) t2 on t1.`key`=t2.`key` group by FD;
SELECT T1.*
FROM table_name T1 LEFT JOIN
(SELECT * FROM table_name GROUP BY `FD` HAVING COUNT(*)>1 ) T2 ON T1.`FD`=T2.`FD` AND T1.`FA` <>1
WHERE T2.`FD` IS NULL
Can you check this.
Hope this helps.
I have the following table in mysql:
Key DI CI FD FA NM Valid_from Valid_to
0 1224468 123 2012-06-30 3 6 2013-01-23 9999-12-31
1 1234567 123 2013-12-31 3 10 2014-02-27 2014-03-10
2 1234567 123 2013-12-31 2 12 2014-03-10 9999-12-31
3 1234579 123 2013-12-31 3 12 2014-05-15 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
6 1224470 123 2015-11-11 2 12 2015-11-11 9999-12-31
7 1224471 123 2015-11-11 3 15 2015-11-11 9999-12-31
8 1224472 123 2015-11-10 2 13 2015-11-10 9999-12-31
9 1224473 123 2015-11-10 3 12 2015-11-10 9999-12-31
If there are records which has the same "FD", I need to get the ones which 's "FA" is "1", if exists.
Basically, I want this output.
Key DI CI FD FA NM Valid_from Valid_to
0 1224468 123 2012-06-30 3 6 2013-01-23 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
8 1224472 123 2015-11-10 2 13 2015-11-10 9999-12-31
9 1224473 123 2015-11-10 3 12 2015-11-10 9999-12-31
I have tried the following code, but it gives a weird output:
Code:
SELECT T1.*
FROM findoc T1 LEFT JOIN
findoc T2
ON DATE(T1.`Financial_date`) = DATE(T2.`Financial_date`) AND T2.`Fig_audit` <> 1
WHERE T2.`Fig_audit` IS NULL OR T1.`Fig_audit` = 1
Output:
Key DI CI FD FA NM Valid_from Valid_to
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
It looks a complicated query, and I couldn't manage to do it.
How can I do it?
Thanks.
you can do case based aggregation to find out if there exists row with same date and with atleast one row with column value for FA as 1
select F.* from finddoc F
inner join
(
select fd , sum( case when fa = 1 then 1 else 0 end) as faOneCount
from finddoc
group by fd
) T
on (F.FD = T.FD and T.faOneCount = 1 AND F.FA =1)
or ( F.FD = T.FD and T.faOneCount =0 )
If I understand your question correctly (and I'm not sure I do), you could try something like this:
SELECT T1.*
FROM findoc T1
where T1.NM in (select distinct NM from findoc where FA = 1);
I'm assuming that NM is the common field for which you want to return all results if the FA for any of those NM entries is 1. But that could be a wrong assumption.