Prioritizing case in mysql - 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

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 ?

MySQL Between Statement and >= <= > < not working

i cant get my between statement to work, I can get AND EffectiveDate >= '2015-06-01' to work, as soon as i add another date condition it breaks. I have tried using CAST('2015-06-01' AS DATE) with no success, i have also tried using '2015-06-01 00:00:00' variations of dates as well with no success.
SELECT
ID,
Division,
EffectiveDate,
PM,
case Status
when 0 then 'Dead'
when 1 then 'Active'
when 2 then 'Job'
when 3 then 'Pending'
when 4 then 'Sales Lead'
when 5 then 'Budget'
when 6 then 'Change Order'
end as Status,
Name,
Address,
ProjectType,
sellPrice
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND Status = 2 OR Status = 6
AND EffectiveDate BETWEEN '2015-06-01' AND '2015-06-30'
ORDER BY EffectiveDate ASC
;
One problem with the query is that OR has lower precedence than AND: the condition on EffectiveDate does not apply to rows that have Division = 'TI'
and Status = 2.
You probably want to write
AND (Status = 2 OR Status = 6)
or, equivalently,
AND Status IN (2, 6)

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)

case when query in mysql

I want to execute below query,
when date diff is greater than 1 , how to add that in when clause as diffdate is not available there.
select
CASE (
SELECT DATEDIFF('2014-11-30','2015-11-29') AS DiffDate
)
WHEN 1 THEN "1 Day"
WHEN 7 THEN "Week"
WHEN ??? THEN "Yearly"
END;
Use an else
select CASE DATEDIFF('2014-11-30','2015-11-29')
WHEN 1 THEN '1 Day'
WHEN 7 THEN '7 Days'
ELSE 'Yearly'
END;

Get records between two time values in MS Acess

I have a datetime field. I want to get the records between 9AM and 5PM also I need the records between 5PM AND 9AM. If I am using between operator it gives me the same number of records.
SELECT count(*)
FROM DirectLineMainCallQuery AS CallTbl
Where Format(CallTbl.CallDate,"dddd") <> 'Saturday' AND Format(CallTbl.CallDate,"dddd") <> 'Sunday'
AND (Format(CallTbl.StartTime,'hh:mm') Between '09:00' AND '17:00')
UNION ALL
SELECT count(*)
FROM DirectLineMainCallQuery AS CallTbl
Where Format(CallTbl.CallDate,"dddd") <> 'Saturday' AND Format(CallTbl.CallDate,"dddd") <> 'Sunday'
AND (Format(CallTbl.StartTime,'hh:mm') Between '17:00' AND '09:00') ;
Any help would be appreciated.
Thanks
AK47 has the correct comment, of needing two time ranges, the second one being either 1700 to midnight, or 0000 to 0900, as below-- Notice the extra pair of parens that enclose the last two betweens...
SELECT Count(*)
FROM directlinemaincallquery AS CallTbl
WHERE Format(CallTbl.calldate, "dddd") <> 'Saturday'
AND Format(CallTbl.calldate, "dddd") <> 'Sunday'
AND ( Format(CallTbl.starttime, 'hh:mm') BETWEEN '09:00' AND '17:00' )
UNION ALL
SELECT Count(*)
FROM directlinemaincallquery AS CallTbl
WHERE Format(CallTbl.calldate, "dddd") <> 'Saturday'
AND Format(CallTbl.calldate, "dddd") <> 'Sunday'
AND (( Format(CallTbl.starttime, 'hh:mm') BETWEEN '17:00' AND '23:59' )
OR ( Format(CallTbl.starttime, 'hh:mm') BETWEEN '00:00' AND '09:00' ));
Edited 4/6 nite
You said -- If I am using between operator it gives me the same number of records
And MSACCESS agrees with you... Between 9 and 17 is the same as Between 17 and 9
I created this test, and got this result----
SELECT table1.*
FROM table1
Where Frame between '8' and '3'
Frame
3
4
5
6
7
8
MSACCESS does not care that the "larger" is before the "smaller", but rather gives you BETWEEN the smaller and the larger of the two values. While you may think that it should "Do What I Mean", it cannot. The way to ask it is to make two Betweens for 17--thru--2359 and 0000 thru 0900 as shown in my example, or to use Greater/Lessor signs (>= <= ).
Please try following code,
SELECT count(*)
FROM DirectLineMainCallQuery AS CallTbl
Where Format(CallTbl.CallDate,"dddd") <> 'Saturday' AND Format(CallTbl.CallDate,"dddd") <> 'Sunday'
AND (Format(CallTbl.StartTime,'hh:mm') >= '09:00' AND (CallTbl.StartTime,'hh:mm') < '17:00')
Union All
SELECT count(*)
FROM DirectLineMainCallQuery AS CallTbl
Where Format(CallTbl.CallDate,"dddd") <> 'Saturday' AND Format(CallTbl.CallDate,"dddd") <> 'Sunday'
AND (Format(CallTbl.StartTime,'hh:mm') >= '17:00' AND (CallTbl.StartTime,'hh:mm') < '9:00')