• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

Weekday order and additional calculation in MySQL

My query returns weekdays in alphabetical order likes following:

Friday
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday

Question #1:
How do I re-write this query and make the weekday shown in the order of:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Question #2:
How do I add a additional column in this query to show a calculated result?
e.g.
Weekday|Win|Lost|Rate(Win/Lost)
Monday|4|5|80%

Thanks.

SELECT date_format( GameDate, '%W' ) , SUM( Win ) , SUM( Lost )
FROM MyTable
WHERE GameDate >= date_sub( now( ) , INTERVAL 500
MONTH )
GROUP BY date_format( GameDate, '%W' )
ORDER BY date_format( GameDate, '%W' )

Open in new window

0
dennis231
Asked:
dennis231
  • 2
1 Solution
 
Roger BaklundCommented:
Try using %w (lower case) instead of %W in the ORDER BY clause:

...ORDER BY date_format( GameDate, '%w' )

This will order with sunday first, then monday, tuesday...

To put sunday last, try this:

...ORDER BY date_format( GameDate, '%w' )=0,date_format( GameDate, '%w' )
0
 
Roger BaklundCommented:
...oh, and the calculated column:

CONCAT(FLOOR((SUM( Win )/SUM( Lost ))*100),'%') AS 'Rate(Win/Lost)'
0
 
SharathData EngineerCommented:

For first question, you can try like this:
 
SELECT WeekDayCol,Sum_Win,Sum_Lost FROM (
SELECT date_format( GameDate, '%W' ) AS WeekDayCol, 
       CASE WHEN date_format( GameDate, '%W' ) = 'Monday' THEN 1
            WHEN date_format( GameDate, '%W' ) = 'TuesDay' THEN 2
            WHEN date_format( GameDate, '%W' ) = 'Wednesday' THEN 3
            WHEN date_format( GameDate, '%W' ) = 'Thursday' THEN 4
            WHEN date_format( GameDate, '%W' ) = 'Friday' THEN 5
            WHEN date_format( GameDate, '%W' ) = 'Saturday' THEN 6
            WHEN date_format( GameDate, '%W' ) = 'Sunday' THEN 7 END AS NumDateFormat,
       SUM( Win ) AS Sum_Win, SUM( Lost ) AS Sum_Lost
FROM MyTable
WHERE GameDate >= date_sub( now( ) , INTERVAL 500
MONTH )
GROUP BY date_format( GameDate, '%W' ) ) t1
ORDER BY NumDateFormat
 
For second question:
 
SELECT WeekDayCol,Sum_Win,Sum_Lost, (Sum_Win * 1.0 * 100) / Sum_Lost AS Rate
  FROM (
SELECT date_format( GameDate, '%W' ) AS WeekDayCol, 
       CASE WHEN date_format( GameDate, '%W' ) = 'Monday' THEN 1
            WHEN date_format( GameDate, '%W' ) = 'TuesDay' THEN 2
            WHEN date_format( GameDate, '%W' ) = 'Wednesday' THEN 3
            WHEN date_format( GameDate, '%W' ) = 'Thursday' THEN 4
            WHEN date_format( GameDate, '%W' ) = 'Friday' THEN 5
            WHEN date_format( GameDate, '%W' ) = 'Saturday' THEN 6
            WHEN date_format( GameDate, '%W' ) = 'Sunday' THEN 7 END AS NumDateFormat,
       SUM( Win ) AS Sum_Win, SUM( Lost ) AS Sum_Lost
FROM MyTable
WHERE GameDate >= date_sub( now( ) , INTERVAL 500
MONTH )
GROUP BY date_format( GameDate, '%W' ) ) t1
ORDER BY NumDateFormat

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now