id num date Attempt
1 555 2015-01-03 01:00:00 1
1 555 2015-01-03 02:00:00 2
1 555 2015-01-03 03:00:00 3
1 555 2015-01-01 04:03:03 4
1 555 2015-01-02 06:00:00 5
1 555 2015-01-06 17:55:24 6
1 555 2015-01-07 18:00:00 7
2 666 2015-01-07 01:00:00 1
2 666 2015-01-07 02:00:00 2
Hi, I have the above table layout but the "Attempts" field is blank.
I'm trying to update the attempts field based on the ID, Date.
I used this
SET #x = 0;
UPDATE attempts
SET attempt = (#x:=#x+1) ORDER BY id, date
The thing is I need to also add the group by clause so it picks up on the new id and restart the increment from 1 again as the numbering just continues onto the next id so my results currently look like:
id num date Attempt
1 555 2015-01-03 01:00:00 1
1 555 2015-01-03 02:00:00 2
1 555 2015-01-03 03:00:00 3
1 555 2015-01-01 04:03:03 4
1 555 2015-01-02 06:00:00 5
1 555 2015-01-06 17:55:24 6
1 555 2015-01-07 18:00:00 7
2 666 2015-01-07 01:00:00 8
2 666 2015-01-07 02:00:00 9
So 8 & 9 should be 1 and 2 respectively due to the new ID.
Can anyone help with a bit of code to get my desired result?
Here is one method:
SET #x = 0;
SET #num = -1;
UPDATE attempts
SET attempt = if(#num = num, #x := #x+1,
if(#num := num, #x := 1, #x := 1)
)
ORDER BY id, date;
Do be clear whether you want to order by id, date or num, date.
Related
I want to create a stored procedure in MySQL, but first, I want to get the query right. However, I keep getting the problem that I can't seem to get the correct id back from my query that correspond with the DateTime stamps that I get back.
this is the table I am trying to get the result from:
id EventId start end
1 1 2019-04-05 00:00:00 2019-04-07 00:00:00
2 2 2020-04-03 00:00:00 2020-04-03 00:00:00
3 3 2020-04-02 00:00:00 2020-04-02 00:00:00
7 1 2020-06-11 00:00:00 2020-06-11 00:00:00
9 2 2020-06-18 00:00:00 2020-06-18 00:00:00
10 3 2020-06-11 00:00:00 2020-06-11 00:00:00
11 3 2020-06-07 00:00:00 2020-06-07 00:00:00
query:
SELECT DISTINCT Eventid, MIN(start), id
from date_planning
WHERE `start` >= NOW()
GROUP BY Eventid
this gives me the following result
EventId Min(start) id
1 2020-06-11 00:00:00 3
2 2020-06-18 00:00:00 9
3 2020-06-07 00:00:00 10
but these are the correct ids that belong to those DateTimes:
EventId Min(start) id
1 2020-06-11 00:00:00 7
2 2020-06-18 00:00:00 9
3 2020-06-07 00:00:00 11
You want the row with the minimum "future" date for each eventId. To solve this greatest-n-per-group problem, you need to filter rather than aggregate. Here is one option using a correlated subquery:
select dt.*
from date_planning dt
where dt.start = (
select min(dt1.start)
from date_planning dt1
where dt1.eventId = dt.eventId and dt1.start >= now()
)
For performance, you need an index on (eventId, start).
How to make blank/missing in Date_time column if the no.of frozen/repeating Date_time is equal to the no of desired Date_time record between previous correct Date_time and next correct Date_time in the time series (ROW_ID : 5,6,12,13)- Replace the frozen Date_time with the missing Date_time and keep record.
WE have the data in 15 mins interval
INPUT:
Row_id Name Date_time Value
1 US 01-Nov-16 04:15:00 12
2 US 01-Nov-16 04:30:00 15
3 US 01-Nov-16 04:45:00 17
4 US 01-Nov-16 05:00:00 19
5 US 01-Nov-16 05:15:00 21
6 US 01-Nov-16 05:15:00 23
7 US 01-Nov-16 05:45:00 25
8 UK 06-Dec-16 09:15:00 27
9 UK 06-Dec-16 09:30:00 29
10 UK 06-Dec-16 09:45:00 31
11 UK 06-Dec-16 10:00:00 33
12 UK 06-Dec-16 10:15:00 35
13 UK 06-Dec-16 10:15:00 37
14 UK 06-Dec-16 10:45:00 39
OUTPUT:
Row_id Name Date_time Value
1 US 01-Nov-16 04:15:00 12
2 US 01-Nov-16 04:30:00 15
3 US 01-Nov-16 04:45:00 17
4 US 01-Nov-16 05:00:00 19
5 US 21
6 US 23
7 US 01-Nov-16 05:45:00 25
8 UK 06-Dec-16 09:15:00 27
9 UK 06-Dec-16 09:30:00 29
10 UK 06-Dec-16 09:45:00 31
11 UK 06-Dec-16 10:00:00 33
12 UK 35
13 UK 37
14 UK 06-Dec-16 10:45:00 39
I have tried to get the output with below code
create table DS2 as
SELECT date_time,value, name, COUNT() - 2
FROM (
SELECT date_time,value, name, rn - seq AS grp
FROM (
SELECT date_time,value, name,
#rn := #rn + 1 AS rn,
#seq := IF(#name = name And #val = date_time, #seq+1, 1) AS seq,
#name := name,
#val := date_time
FROM DS1
CROSS JOIN (SELECT #rn := 0, #seq := 0, #name = '', #val = 0) AS vars
ORDER BY date) AS t ) AS s
GROUP BY date_time, name, grp
HAVING COUNT() > 2
;
Thanks in advance.
I have a table:
Name Registered Date
Amit 2017-01-01
Akshay 2017-01-03
Ankith 2017-01-05
Amit 2017-01-12
Amit 2017-01-13
Amit 2017-02-01
Amit 2017-02-01
I want to write a query which will display the registration weekly report:
Say date between 2017-01-01 to 2017-03-01
Week Count
2017-01-01 3
2017-01-08 2
2017-01-15 0
2017-01-22 0
2017-01-29 2
2017-02-05 0
2017-02-12 0
2017-02-19 0
2017-02-26 0
Here Count is the number of people who registered that week. 3 people registered in between 2017-01-01 to 2017-01-07.
So which query i have to use for this result?
Thanks
If you can use the WEEK function and display the week number instead of a date, then:
select dummy.n, count(table.RegiseredDate)
from (SELECT 1 as n UNION SELECT 2 as n UNION SELECT 3 as n ... UNION SELECT 53 as n) dummy
left outer join table on dummy.n=WEEK(table.Registered Date)
where start_date>= x and end_date<= y
group by WEEK(Registered Date)
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.
I want to select match count from result where the match count is exact int on a date.
date id_event id_timewindows max_hits
2014-12-16 1 1,2,3 2
2014-12-16 2 2,3,4 2
2014-12-16 3 4 2
2014-12-16 4 5,6 2
2014-12-16 5 7,8 2
2014-12-16 6 9 2
The result what i want is:
date id_event id_timewindows max_hits
2014-12-16 1 2,3 2
2014-12-16 2 2,3 2
Have anybody idea, how to do it in MySQL?
UPDATE:
So i have to explain more. The id_timewindows is not a string attribute, the first one is a result of a view which grouped by id_events and one id_event has multiple id_timewindow.
View result before grouped:
date id_event id_timewindow begin end max_rooms
2014-12-16 1 1 06:00:00 07:00:00 2
2014-12-16 1 2 07:00:00 08:00:00 2
2014-12-16 1 3 08:00:00 09:00:00 2
2014-12-16 2 2 07:00:00 08:00:00 2
2014-12-16 2 3 08:00:00 09:00:00 2
2014-12-16 2 4 09:00:00 10:00:00 2
2014-12-16 3 4 09:00:00 10:00:00 2
2014-12-16 4 6 11:00:00 12:00:00 2
2014-12-16 4 5 10:00:00 11:00:00 2
2014-12-16 5 7 12:00:00 13:00:00 2
2014-12-16 5 8 13:00:00 14:00:00 2
2014-12-16 6 9 14:00:00 15:00:00 2
I use GROUP BY id_event and the id_timewindows is group_concat(id_timewindow SEPARATOR ',')
I found a solution:
SELECT
date,
id_timewindow,
max_rooms,
COUNT(concat(date, id_timewindow)) as counter
FROM `timewindows_reserved`
GROUP BY
date, id_timewindow
HAVING counter < max_rooms
That will result what inverse what i want and i can use it.
date id_timewindow max_hits
2014-12-16 1 2
2014-12-16 4 2
2014-12-16 5 2
2014-12-16 6 2
2014-12-16 7 2
2014-12-16 8 2
2014-12-16 9 2
If I group by date and make a LIST from the id_timewindow then i can recevie the same result as what I wanted, but in inverse logic. Not a reserved timewindows rather the free timewindows. If I reverse this then I can get the result:
SELECT
date,
id_event,
GROUP_CONCAT(id_timewindow SEPARATOR ','),
max_rooms,
COUNT(concat(date, id_timewindow)) as counter
FROM `table`
GROUP BY
date, id_timewindow
HAVING counter >= max_rooms