Good day. There is a table that consists of 5 fields: name,phone,address, date,status .

It is necessary to make a request that will return a list of dates and the number of elements of different statuses.

For example : on October 31, there were 4 entries in confirmed status, 7 with canceled and 2 with deferred. Now I try to do this:

 SELECT order_date, status FROM orders GROUP BY order_date,status ORDER BY status; 

But then just the date and status is returned, and I need the number of all statuses to be listed on each date.

  • The count(*) gets the number of records. Add it to the list of selections - Mike
  • 2
    Where did you get postgresql in tags? - Pavel Mayorov
  • Base postgresql. - denjke
  • 2
    Then where does the mysql in the question title from? - Yaant

2 answers 2

 SELECT date as [Дата], status as [Статус], count(*) as [Кол-во] FROM orders GROUP BY date, status; 
  • Thanks for the answer. But I do not group by date, but I need to display statistics for one day on all statuses at once. "2016-10-31"; "confirmed"; 5 "2016-10-21"; "confirmed"; 1 "2016-10-30"; "confirmed"; 1 "2016-10-21"; "canceled" ; 2 - denjke
 SELECT order_date, sum(if(status='подтвержден',1,1)) `кол-во подтвержденных`, ... FROM orders GROUP BY order_date; 
  • and you can make to give out only one date record, but she had fields with the number of each status for a given day? - denjke
  • You can, updated the answer, but will it work in postgris xs - retvizan