[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • 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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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