Mysql select query with ordering issue - mariadb

I want to display one paid status(1) column and then 3 unpaid(0) status column respectively.
Paid status is taken based on Expirydate, the date greater than now is considered paid status and date which is less than expiry date is considered unpaid status. Output can be based on Entry date.
My Table:
Id
Expirydate
Entrydate
1
2022-12-10
2022-10-11
2
2022-12-09
2022-09-01
3
2022-10-10
2022-10-18
4
0000-00-00
2022-10-17
5
2022-09-08
2022-10-01
6
0000-00-00
2022-10-15
7
0000-00-00
2022-09-09
8
2022-11-30
2022-09-10
Output would be:
Id
Expirydate
Entrydate
Status
1
2022-12-10
2022-10-11
1
3
2022-10-10
2022-10-18
0
4
0000-00-00
2022-10-17
0
5
2022-09-08
2022-10-01
0
2
2022-12-09
2022-09-01
1
6
0000-00-00
2022-10-15
0
7
0000-00-00
2022-09-09
0
8
2022-11-30
2022-09-10
1
NOTE: Considering now() as 2022-10-18. Also last there was only 2 unpaid status so the rest paid status would be shown.
1st attempt to calculate status:
SELECT table.* , CASE WHEN `expirydate` >= NOW() THEN "1" ELSE "0" END AS paidstatus
FROM table ORDER BY paidstatus DESC
Thanks.

You can do this to solve your problem.
SELECT *, ( table.Expirydate > NOW() ) as `Paid_Status` FROM table ORDER BY `Paid_Status` DESC;

Related

Select data based on a day in a month, "If date is lesser than a day in a month"

I have a dataset with a column date like this :
id
date
1
2018-02-13
2
2019-02-28
3
2014-01-23
4
2021-10-28
5
2022-01-23
6
2019-09-28
I want to select data that have a date lesser than 15th February
I tried an ifelse statement to define 15th february for each year in my database but i want to know if there is an easier way to do so !
Here's the result i'm expecting
id
date
1
2018-02-13
3
2014-01-23
5
2022-01-23
Thanks in advance
You could use lubridate
library(lubridate)
library(dplyr)
d %>% filter(month(date)<2 | (month(date)==2 & day(date)<=15))
Output:
id date
1 1 2018-02-13
2 3 2014-01-23
3 5 2022-01-23
This is a base R option. Using format check if the month/year is between January 1st and February 15th.
df[between(format(df$date, "%m%d"), "0101", "0215"),]
Output
id date
1 1 2018-02-13
3 3 2014-01-23
5 5 2022-01-23

How can I mutate the most recent date compared to a reference date by ID using Tidyverse?

I am looking for a solution in the Tidyverse to accomplish the following goal. I want to select the most recent date for each given date ("appointment_date") by ID ("client_number").
Data input:
Client_number
Appointment_date
1
2021-06-03
1
2021-07-01
1
2021-08-26
2
2019-08-01
2
2019-08-15
2
2019-09-02
Desired output:
Client_number
Appointment_date
Last_appointment
1
2021-06-03
NA
1
2021-07-01
2021-06-03
1
2021-08-26
2021-07-01
2
2019-08-01
NA
2
2019-08-15
2019-08-01
2
2019-09-02
2019-08-15
We group by Client_number and then take the lag
library(dplyr)
df1 %>%
group_by(Client_number) %>%
mutate(Last_appointment = lag(Appointment_date)) %>%
ungroup

In R how to calculate if Date is earlier then date X?

I got a DF with a date column in it. I want to check if the date in the column is after or before 1st of January 2020. Create a new column and if the previous columns date is before then insert 1st of January 2020 if not then insert previous columns date.
Date is in format YYYY-MM-DD
Beginning End
2020-12-31 2021-01-12
2018-01-02 2020-03-10
2019-04-12 2020-12-04
2020-10-15 2021-03-27
I want:
Beginning End Beginning_2
2020-12-31 2021-01-12 2020-12-31
2018-01-02 2020-03-10 2020-01-01
2019-04-12 2020-12-04 2020-01-01
2020-10-15 2021-03-27 2020-10-15
The code i wrote is:
DF$Beginning_2 <- ifelse("2020-01-01" > DF$Beginning,"2020-01-01", DF$Beginning)
I'm getting this
Beginning End Beginning_2
2020-12-31 2021-01-12 18554
2018-01-02 2020-03-10 2020-01-01
2019-04-12 2020-12-04 2020-01-01
2020-10-15 2021-03-27 18453
My code works half way. It turns the format in to char. I need it to stay as date. I tried butting as date all over the code but nothing much changed. The biggest change was that greater then 2020-01-01 dates were NA instead of "18554".
How to fix my code?
Thank you
You can use pmax:
DF$Beginning_2 <- pmax(DF$Beginning, as.Date("2020-01-01"))
#DF$Beginning_2 <- pmax(DF$Beginning, "2020-01-01") #Works also
DF
# Beginning End Beginning_2
#1 2020-12-31 2021-01-12 2020-12-31
#2 2018-01-02 2020-03-10 2020-01-01
#3 2019-04-12 2020-12-04 2020-01-01
#4 2020-10-15 2021-03-27 2020-10-15
str(DF)
#'data.frame': 4 obs. of 3 variables:
# $ Beginning : Date, format: "2020-12-31" "2018-01-02" ...
# $ End : Date, format: "2021-01-12" "2020-03-10" ...
# $ Beginning_2: Date, format: "2020-12-31" "2020-01-01" ...
Base R ifelse would return dates as numbers you will need to convert them back to dates.
DF$Beginning_2 <- as.Date(ifelse(DF$Beginning > as.Date("2020-01-01"),
DF$Beginning, as.Date("2020-01-01")), origin = '1970-01-01')
You may use dplyr::if_else which will maintain the class of the date columns.
DF$Beginning_2 <- dplyr::if_else(DF$Beginning > as.Date("2020-01-01"),
DF$Beginning, as.Date("2020-01-01"))
DF
# Beginning End Beginning_2
#1 2020-12-31 2021-01-12 2020-12-31
#2 2018-01-02 2020-03-10 2020-01-01
#3 2019-04-12 2020-12-04 2020-01-01
#4 2020-10-15 2021-03-27 2020-10-15

SQL - How to append a period

I have an existing SQLite_table like this:
startdate - enddate
2018-01-01 - 2018-06-30
2018-07-01 - 2018-12-31
2019-01-01 - 2019-06-30
2019-07-01 - 2019-12-31
2020-01-01 - 2020-06-30
2020-07-01 - 2020-12-31
2021-01-01 - 2021-06-30
What is the SQL-Statement for the result;
The result should be:
2019-11-01 2020-12-31 // 60 Days difference
2020-01-01 2020-06-30 // 180 Days difference
2020-07-01 2020-12-31 // 180 Days difference
2021-01-01 2021-06-30 // 180 Days difference
'2019-11-01' is entered via the search field in my Android app as input
The point is the output of the small period at the beginning / the
insertion of the first period in the statement
I tried 'Union' and it gives me an error.
How can I do this with a query ?
I am thankful for any help
and what is the SQL statement, when it should
return data from '2019-11-01' until 'now'
2019-11-01 2020-12-31 // 60 Days difference
2020-01-01 2020-06-30 // 180 Days difference
2020-07-01 2020-12-31 // 180 Days difference
2021-01-01 2021-02-16 // 46 Days difference ! !
Thank's
You need all the rows where enddate is less or equal than '2019-11-01':
SELECT MAX(startdate, '2019-11-01') startdate, enddate
FROM tablename
WHERE enddate >= '2019-11-01'
See the demo.
Results:
startdate
enddate
2019-11-01
2019-12-31
2020-01-01
2020-06-30
2020-07-01
2020-12-31
2021-01-01
2021-06-30
Edit, for your 2nd question:
SELECT MAX(startdate, '2019-11-01') startdate,
MIN(enddate, CURRENT_DATE) enddate
FROM tablename
WHERE enddate >= '2019-11-01' AND startdate <= CURRENT_DATE
See the demo.
Results:
startdate
enddate
2019-11-01
2019-12-31
2020-01-01
2020-06-30
2020-07-01
2020-12-31
2021-01-01
2021-02-16

Correlation of Events(AlerKey) basis its Time(First_Occurred) on the Nodes (Area). Which event is more likely to happen as a result of past event?

enter image description here
Correlation of Events(AlerKey) basis its Time(First_Occurred) on the Nodes (Area). Which event is more likely to happen as a result of past event with reference to the Time on the Node in R tool ?
Also if we can show the correlation percentage and at what confidence we can say this event will happen as a result of previous event ?
I have already grouped Nodes and sorted Occurrence time in ascending order.
Data has 5 columns, Node, AlertKey, count, FirstOccurence & LastOccurrence.
"Node AlertKey Count FirstOccurrence LastOccurrence
A HouseKeeping 40 2020-05-08 15:48:21 2020-05-18 12:12:40
A Loss_Of_Sec 6 2020-05-14 12:49:43 2020-05-18 00:13:48
A Loss_Of_Signal 3 2020-05-09 17:02:56 2020-05-17 23:05:34
A Not_Reach 9 2020-05-11 10:24:03 2020-05-14 12:55:53
A Node_Isolation 5 2020-05-11 10:24:03 2020-05-14 12:55:52
C Degraded_Signal 10 2020-05-15 13:40:48 2020-05-17 15:26:46
C Degraded_Signal 10 2020-05-15 13:40:48 2020-05-17 15:26:46
C Remote_Defect 1 2020-05-11 18:20:09 2020-05-18 18:20:08
C Loss_Frame 35 2020-05-09 12:21:10 2020-05-09 12:21:13"

Resources