Select as a result of one day of the week

Asked

Viewed 31 times

0

Well I have a select that returns the total order amount made in the current week. I do it as follows:

SELECT
    SUM(valor)
FROM
    pedidos
WHERE
    YEARWEEK(data) = YEARWEEK(NOW())

Well I need a select for each day of the week, that returns me the total of each day of the current week.

How can I do that?

2 answers

2


You can try to do that:

SELECT
    DAYOFWEEK(data) as dia, SUM(valor) as total
FROM
    pedidos
WHERE
    YEARWEEK(data) = YEARWEEK(NOW())
GROUP BY DAYOFWEEK(data)
ORDER BY DAYOFWEEK(data)

  • It worked, he returned me the days in numbers. It would be 0 for Sunday and 6 for Saturday?

  • Yeah, that’s right, that’s right

  • Thank you very much.

2

You must truncate the date using the mysql DATE function and use the GROUP BY clause:

    SELECT
      DATE(data), SUM(valor)
    FROM
      pedidos
    WHERE
      YEARWEEK(data) = YEARWEEK(NOW())
    GROUP BY DATE(data)
    ORDER BY DATE(data)
  • It worked, but Bins' answer suits the mine needs better, because it returns me the days of the week in numbers.

  • Okay, I thought you’d like to display the date of the day.

  • Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.