SQL count each occurrence of case sqlite - mysql

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

Related

MySQL - Comparing dayname of two queries

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 ?

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

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)

Converting week index to day in Mysql

I have weekdays store like 1,2,3 instead of Sunday, Monday, Tuesday etc..(1= Sunday, 2 = Monday, …, 7 = Saturday).
now I want to retrieve like Sunday, monday instead 1 and 2
I tried using DATE_FORMAT(date,format).. but no success.
Just use a case statement:
select (case day when 1 then 'Sunday'
when 2 then 'Monday'
when 3 then 'Tuesday'
when 4 then 'Wednesday'
when 5 then 'Thursday'
when 6 then 'Friday'
when 7 then 'Saturday'
end) as DayOfWeek
Or, in MySQL, you can also use ELT():
select elt(day, 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')

Problematic accents with MYSQL DATE_FORMAT french / français month and day names via UDF

I created an UDF to extend DATE_FORMAT function to get French month and day names.
I replace %a, %b, %W and %M separately with right word.
I use a CASE statement to convert English to French word, and then replace the format string tag with found word.
It works fine.
delimiter |
/*DROP FUNCTION DATE_FORMAT_FR*/
|
CREATE FUNCTION DATE_FORMAT_FR (dt DATETIME, f VARCHAR(32))
RETURNS VARCHAR(255) DETERMINISTIC NO SQL
BEGIN
DECLARE name VARCHAR(16);
/*short day name*/
IF INSTR(f, '%a') >= 0 THEN
SET name = CASE DATE_FORMAT(dt, '%a')
WHEN 'Mon' THEN 'Lun'
WHEN 'Tue' THEN 'Mar'
WHEN 'Wed' THEN 'Mer'
WHEN 'Thu' THEN 'Jeu'
WHEN 'Fri' THEN 'Ven'
WHEN 'Sat' THEN 'Sam'
WHEN 'Sun' THEN 'Dim'
ELSE DATE_FORMAT(dt, '%a')
END;
/*replace in source format string*/
SET f = REPLACE(f, '%a', name);
END IF;
/*long day name*/
IF INSTR(f, '%W') >= 0 THEN
SET name = CASE DATE_FORMAT(dt, '%W')
WHEN 'Monday' THEN 'Lundi'
WHEN 'Tuesday' THEN 'Mardi'
WHEN 'Wednesday' THEN 'Mercredi'
WHEN 'Thursday' THEN 'Jeudi'
WHEN 'Friday' THEN 'Vendredi'
WHEN 'Saturday' THEN 'Samedi'
WHEN 'Sunday' THEN 'Dimanche'
ELSE DATE_FORMAT(dt, '%W')
END;
/*replace in source format string*/
SET f = REPLACE(f, '%W', name);
END IF;
/*short month name*/
IF INSTR(f, '%b') >= 0 THEN
SET name = CASE DATE_FORMAT(dt, '%b')
WHEN 'Jan' THEN 'Jan'
WHEN 'Feb' THEN 'Fév'
WHEN 'Mar' THEN 'Mar'
WHEN 'Apr' THEN 'Avr'
WHEN 'May' THEN 'Mai'
WHEN 'Jun' THEN 'Juin'
WHEN 'Jul' THEN 'Juil'
WHEN 'Aug' THEN 'Août'
WHEN 'Sep' THEN 'Sept'
WHEN 'Oct' THEN 'Oct'
WHEN 'Nov' THEN 'Nov'
WHEN 'Dec' THEN 'Déc'
ELSE DATE_FORMAT(dt, '%b')
END;
/*replace in source format string*/
SET f = REPLACE(f, '%b', name);
END IF;
/*long month name*/
IF INSTR(f, '%M') >= 0 THEN
SET name = CASE DATE_FORMAT(dt, '%b')
WHEN 'Jan' THEN 'Janvier'
WHEN 'Feb' THEN 'Février'
WHEN 'Mar' THEN 'Mars'
WHEN 'Apr' THEN 'Avril'
WHEN 'May' THEN 'Mai'
WHEN 'Jun' THEN 'Juin'
WHEN 'Jul' THEN 'Juillet'
WHEN 'Aug' THEN 'Août'
WHEN 'Sep' THEN 'Septembre'
WHEN 'Oct' THEN 'Octobre'
WHEN 'Nov' THEN 'Novembre'
WHEN 'Dec' THEN 'D\351cembre'
ELSE DATE_FORMAT(dt, '%M')
END;
/*replace in source format string*/
SET f = REPLACE(f, '%M', name);
END IF;
/*returns standard conversion*/
RETURN DATE_FORMAT(dt, f);
END
But I have a problem with accents:
SELECT DATE_FORMAT_FR('2000-02-01', '%a %W %b %M')
> Mar Mardi F?v F?vrier
How can I solve this problem?
I have found that modifying the stored proc with phpMyAdmin UI, just revalidating it, no more accent problem appears...
So strange.