MySQL - Comparing dayname of two queries - mysql

I want to compare two columns Assigned_Date and Expected_Delivery_Day in my table and check if day has passed.
This is my table:
I tried to get the dayofweek for each column and compare the weight with IF, but im not sure how.
This is what i tried:
SELECT dayofweek(`Assigned_Date`) FROM `drvapp_routes`;
SELECT CASE
WHEN `Expected_Delivery_Day` = 'Sunday' THEN 0
WHEN `Expected_Delivery_Day` = 'Monday' THEN 1
WHEN `Expected_Delivery_Day` = 'Tuesday' THEN 2
WHEN `Expected_Delivery_Day` = 'Wednesday' THEN 3
WHEN `Expected_Delivery_Day` = 'Thursday' THEN 4
WHEN `Expected_Delivery_Day` = 'Friday' THEN 5
WHEN `Expected_Delivery_Day` = 'Saturday' THEN 6
ELSE NULL END AS EDD
from `drvapp_routes`;
Is this the right way? How do i compare two day names and see if Assigned_Date has passed the Expected_Delivery_Day ?

Related

how to calculate mysql date diff between two dates excluding Fridays and Saturdays?

how to calculate mysql date diff between two dates excluding Fridays and Saturdays as Fridays and Saturdays are off days
i've tried
SELECT ((DATEDIFF('2022-01-02', '2021-12-30'))+1 - ((WEEK('2022-01-02') - WEEK('2021-12-30')) * 2) - (case when weekday('2022-01-02') = 5 then 1 else 0 end) - (case when weekday('2021-12-30') = 4 then 1 else 0 end)) as DifD
but it gives wrong count (105 days) and suppose to give 2 days
you need to use case expression to check if the day is Thursday then add -2
SELECT ((DATEDIFF('2014-10-25', '2014-10-15')) -
((WEEK('2014-10-25') - WEEK('2014-10-15')) * 2) -
(case when weekday('2014-10-25') = 5 then 1 else 0 end) -
(case when weekday('2014-10-15') = 4 then 1 else 0 end)) as DifD
// Total difference
SELECT ((DATEDIFF('2014-10-25', '2014-10-15'))
// calendar week(s) ---> not including the year
WEEK('2022-01-02') // = 1
WEEK('2021-12-30') // = 52
// get weekday, where 0 is monday and 6 is sunday
case when weekday('2022-01-02') = 4 // (or 5) subtract 1
If you want this to work on different years, like your example above u have to add 52 weeks for every year difference.
a working example would be:
SELECT ((DATEDIFF('2022-01-02', '2021-12-30'))+1 - ((WEEK('2022-01-02') - WEEK('2021-12-30') + 52) * 2) - (case when weekday('2022-01-02') = 5 then 1 else 0 end) - (case when weekday('2021-12-30') = 4 then 1 else 0 end)) as DifD
Thank you everyone for helping, i found the answer from here mysql-function to count days between 2 dates excluding weekends
CREATE FUNCTION TOTAL_WEEKDAYS(date1 DATE, date2 DATE) RETURNS INT RETURN ABS(DATEDIFF(date2, date1)) + 1 - ABS(DATEDIFF(ADDDATE(date2, INTERVAL 1 - DAYOFWEEK(date2) DAY), ADDDATE(date1, INTERVAL 1 - DAYOFWEEK(date1) DAY))) / 7 * 2 - (DAYOFWEEK(IF(date1 < date2, date1, date2)) = 6) - (DAYOFWEEK(IF(date1 > date2, date1, date2)) = 7);
sql query for any dates e.g. :
SELECT TOTAL_WEEKDAYS('2022-01-09', '2022-01-12') weekdays1;

Prioritizing case in mysql

I want to prioritize the status part. Everything which is not repported should appear first. Regardless date. But now, It orders eveything after date. How can I prioritize the first case 'not repported part'? The rest of the list should appear after date.
SELECT * FROM list
ORDER BY date DESC,
CASE
WHEN `bookings`.`status` = 'Not repported' THEN 1
WHEN day= 'Monday' THEN 2
WHEN day= 'Tuesday' THEN 3
WHEN day= 'Wednesday' THEN 4
WHEN day= 'Thursdau' THEN 5
WHEN day= 'Friday' THEN 6
WHEN day= 'Saturday' THEN 7 WHEN
day= 'Sunday' THEN 8 END ASC
limit 1,20 ";
I want something like this.
------------------------------
DATE----DAY-------STATUS------
2011---Monday-----Not reported
2015---Sunday-----Not reported
2010---Wednedday--Not reported
2016---Monday---------Reported
2015---Monday---------Reported
2014---Tuesday--------Reported
2013---Sunday---------Reported
------------------------------
You have two order statements. One for the date, and one for your booking status. Mysql will first order according to your first order statement, and then according to your second order statement. So switching the order statements solves your problem. In addition there is the "ELSE" keyword for "CASE"s in mysql.
In total I would write it like this:
SELECT *
FROM list
ORDER BY
CASE
WHEN `bookings`.`status` = 'Not repported' THEN 1
ELSE 2
END ASC,
date DESC
LIMIT 1,20
(Im not so sure where your "bookings" table now comes from, but I left it there. You might miss a JOIN clause)
Change the order by
Something like this
SELECT * FROM list
ORDER BY
CASE
WHEN `bookings`.`status` = 'Not repported' THEN 1 ELSE 2
END,
CASE
WHEN day= 'Monday' THEN 2
WHEN day= 'Tuesday' THEN 3
WHEN day= 'Wednesday' THEN 4
WHEN day= 'Thursdau' THEN 5
WHEN day= 'Friday' THEN 6
WHEN day= 'Saturday' THEN 7 WHEN
day= 'Sunday' THEN 8 END ASC,
date DESC
limit 1,20 ";
This will give you this result
'Not repported' , 1/1/2017
'Not repported' , 3/7/2017
'Not repported' , 15/8/2017
Monday , 6/2/2017
Monday , 11/11/2017

CASE statement not working as it should in SQL

SELECT userid,
(SELECT CASE WHEN (COUNT(CASE WHEN casinowagers != 0
THEN 1
ELSE null
END)
+ COUNT(CASE WHEN depositmade_amt != 0
THEN 1
ELSE null
END)) >= 3
AND (Round(sum(totalhold - playercomps - freemoney - (depositmade_amt*.1)),2)) >= 10
THEN "VIP"
ELSE "NON-VIP"
END as VIPcheck
FROM player_activity
WHERE YEAR(txndate) = YEAR(CURRENT_DATE - INTERVAL 3 MONTH)
AND MONTH(txndate) = MONTH(CURRENT_DATE - INTERVAL 3 MONTH)
) as vipMonthStatus,
(COUNT(CASE WHEN casinowagers != 0
THEN 1
ELSE null
END)
+ COUNT(CASE WHEN depositmade_amt != 0
THEN 1
ELSE null
END)) as activityCount,
(Round(sum(totalhold - playercomps - freemoney - (depositmade_amt*.1)),2)) as Value,
FROM player_activity
WHERE userid = 2023410
GROUP BY year(txndate),month(txndate)
LIMIT 1000
So basically the vipMonth status is always returning as "VIP". However, for the month of May (because of the below "where" statement), it should be "non-vip" because only 1 deposit and 1 wager were made.
What gives?
( WHERE YEAR(txndate) = YEAR(CURRENT_DATE - INTERVAL 3 MONTH)
AND MONTH(txndate) = MONTH(CURRENT_DATE - INTERVAL 3 MONTH) )
You need to tie the player_activity table in the vipMonthStatus subquery to the player_activity table in the main query.
First alias the "inner" player_activity and the "outer" player_activity tables so that you can tell them apart. Something like in_player_activity and out_player_activity.
Then you need to add an additional WHERE statement to the vipMonthStatus subquery to restrict the records in the inner table to the associated records in the outer table based on userid . It would end up looking like this:
FROM player_activity in_player_activity
WHERE YEAR(in_player_activity.txndate) = YEAR(CURRENT_DATE - INTERVAL 3 MONTH)
AND MONTH(in_player_activity.txndate) = MONTH(CURRENT_DATE - INTERVAL 3 MONTH)
AND in_player_activity.userid = out_player_activity.userid
Keep in mind now that you have introduced table aliases, you will need to add the alias prefix to all of the other column names. Personally I would use something more concise like opa and ipa.

how to group by week start from friday

I have table sql like this:
This my query for count tgl:
SELECT count( tgl ) AS total, absen.id
FROM absen
WHERE absen.status = 'm'
GROUP BY absen.id
So I want group by absen.id and absen.tgl
How to group by week from Friday to Thursday?
2016-01-08 is friday and 2016-01-15 is thursday.
Bellow query can bring the result you want, but i think you defined the wrong end date, because in your example from 2015-01-08 up to 2015-01-15 its 8 day and one week has 7 days.
select
count( tgl ) AS total,
absen.id,
CASE WHEN (weekday(tgl)<=3) THEN date(tgl + INTERVAL (3-weekday(tgl)) DAY)
ELSE date(tgl + INTERVAL (3+7-weekday(tgl)) DAY)
END as week_days
FROM absen
WHERE status = 'm'
GROUP BY id,week_days
here is the fiddle fiddle
Query Description:
mysql weekday array numbers:
$weekArr = array(
'Monday' => 0,
'Tuesday' => 1,
'Wednesday' => 2,
'Thursday' => 3,
'Friday' => 4,
'Saturday' => 5,
'Sunday' => 6);
So now suppose today is Tuesday and date is 2016-01-12, now let's count from today towards the start date in our table which is 2016-01-07 and it match with Thursday of past week, so according to the weekday array number its weekday(2016-01-07) == 3 so it goes to the WHEN part of our query, and query will select something like this CASE WHEN (weekday('2016-01-07') <= 3) THEN date('2016-01-07' + INTERVAL(3-3)) that is equal to SELECT '2016-01-07' and so on for others.
I just found how to get this by trouble shooting on excel by using this WEEK('date' + INTERVAL 3 DAY, 3)

SQL count each occurrence of case sqlite

I'm trying to return 2 columns: Day of the week and number of occurrences in that day. How would I return the number of occurrences for each day?
SELECT case CAST(strftime('%w', HireDate) AS INTEGER)
WHEN 0 then 'Sunday'
WHEN 1 then 'Monday'
WHEN 2 then 'Tuesday'
WHEN 3 then 'Wednesday'
WHEN 4 then 'Thursday'
WHEN 5 then 'Friday'
ELSE 'Saturday' END AS 'Day of week', AS 'Hired'
FROM Employee;
Just rewrite case on group by clause:
SELECT
( case CAST(strftime('%w', HireDate) AS INTEGER)
WHEN 0 then 'Sunday'
WHEN 1 then 'Monday'
WHEN 2 then 'Tuesday'
WHEN 3 then 'Wednesday'
WHEN 4 then 'Thursday'
WHEN 5 then 'Friday'
ELSE 'Saturday' END ) AS 'Day of week',
count(*) AS 'Hired' --<--here
FROM
Employee
GROUP BY --<--here
( case CAST(strftime('%w', HireDate) AS INTEGER)
WHEN 0 then 'Sunday'
WHEN 1 then 'Monday'
WHEN 2 then 'Tuesday'
WHEN 3 then 'Wednesday'
WHEN 4 then 'Thursday'
WHEN 5 then 'Friday'
ELSE 'Saturday' END );