We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Weekday order and additional calculation in MySQL

dennis231
dennis231 asked
on
Medium Priority
335 Views
Last Modified: 2012-05-06
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

Comment
Watch Question

Top Expert 2008

Commented:
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' )
Top Expert 2008

Commented:
...oh, and the calculated column:

CONCAT(FLOOR((SUM( Win )/SUM( Lost ))*100),'%') AS 'Rate(Win/Lost)'
Data Engineer
CERTIFIED EXPERT
Commented:

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.