If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > brain too small

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-23-11, 05:11
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
brain too small

I'm afraid my brain is too small to figure out how to do the following in sql and i was hoping someone with a large brain might lend me some of theirs....

i have a table that contains sales data as follows:

order_no, date, order_status
1, 2011-08-22, 1
2, 2011-08-22, 2
3, 2011-08-23, 2
4, 2011-08-23, 1
5, 2011-08-23, 1

(order_status > 1 is a completed order)

and i want to be able to produce a summary as follows:

date, completed_orders, failed_orders
2011-08-22, 1, 1
2011-08-23, 1, 2

i tried to achieve this with count & grouping but failed and then read about pivoting but found my brain rejected it... any help much appreciated.

thanks.
Reply With Quote
  #2 (permalink)  
Old 08-23-11, 05:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT `date`
     , COUNT(CASE WHEN order_status > 1
                  THEN 'humpty' END) AS completed_orders
     , COUNT(CASE WHEN order_status = 1
                  THEN 'dumpty' END) AS failed_orders
  FROM daTable
GROUP
    BY `date`
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-23-11, 05:44
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
thanks rudy, your brain is of an enviable size...

a couple of quick queries, if you have the time..

whats the point of the 'then' part of the query and why is it needed?

thanks again,

jim
Reply With Quote
  #4 (permalink)  
Old 08-23-11, 05:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
a CASE expression requires a THEN clause in order to have something to evaluate to

i use 'humpty' and 'dumpty' but they really could be any values

the point is, if the CASE condition is false, the expression evaluates to the ELSE value, and if the ELSE value is not explicitly given, it defaults to NULL

and as you know, aggregate functions like COUNT ignore NULLs

so the COUNT functions only count the non-NULL values, 'humpty' and 'dumpty'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-23-11, 06:12
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
many thanks rudy... i appreciate you taking the time...

ta

j
Reply With Quote
  #6 (permalink)  
Old 08-23-11, 06:33
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
i'd like to also dispaly percentages of the completed / failed orders (never satisfied!!)

i was hoping something similar to the following would work:
SELECT substr(created, 1,10) as date
, COUNT(CASE WHEN order_status > 1
THEN 'humpty' END) AS completed_orders
, COUNT(CASE WHEN order_status = 1
THEN 'dumpty' END) AS failed_orders
, (completed_orders / (completed_orders + failed_orders) *100) as prct_sucess

FROM orders
GROUP
BY `date`

but i receive the following:

Unknown column 'completed_orders' in 'field list'


i imagine the case parts are evaluated after so i cannot access them - could anyone point me in the right direction?
Reply With Quote
  #7 (permalink)  
Old 08-23-11, 07:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
why are you taking a substring of a date? what's that column's actual datatype?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 08-23-11, 08:06
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
oh, its a time stamp so its got the date & time - 2011-08-23 11:34:12 or whatever but i dont want to group them by second! just by day so i pulled out the first 10 characters to just get the date
Reply With Quote
  #9 (permalink)  
Old 08-23-11, 08:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
what you want is the DATE() function, not SUBSTR()
Code:
SELECT date_created
     , completed_orders
     , failed_orders
     , 100.0 * completed_orders / 
              (completed_orders + failed_orders) AS prct_sucess
  FROM ( SELECT DATE(created) AS date_created
              , COUNT(CASE WHEN order_status > 1
                           THEN 'humpty' END) AS completed_orders
              , COUNT(CASE WHEN order_status = 1
                           THEN 'dumpty' END) AS failed_orders
           FROM orders
         GROUP
             BY date_created ) AS t
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 08-23-11, 08:39
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
beautiful.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On