SQL QUERY get a month's average of a relative day of the week

0

I need a query that returns me the average of a relative month, and a day of the week. For example: I need to know the components for the month of February, the day of the week Monday. It is possible to define some expression for DATA , DATA is of date type in sql

SELECT AVG(P.Quant) AS Expr1
FROM     P
WHERE  (Pro.Tipo = 'B') AND (V.ID= 1)
AND (DATA =:....")
    
asked by anonymous 02.04.2014 / 14:05

2 answers

1

If you want every Monday of the month of February, regardless of the year, use:

SELECT AVG(P.Quant) AS Expr1
FROM     P
WHERE  (Pro.Tipo = 'B') AND (V.ID= 1)
AND EXTRACT(DOW FROM P.data) = 1
AND EXTRACT(MONTH FROM P.data) = 2;

The EXTRACT function may have another name in the DBMS you are using.

    
02.04.2014 / 15:36
1

I do not have 50 points to comment on, but could you post your table structure?!

SELECT avg(month(valor_do_mes)) as media_mes, avg(day(valor_do_dia)) as media_dia FROM tbl WHERE /*aí você pode converter para data e fazer o where*/ MONTH(campo) and DAY(dia)

If it does not work, send the structure of your table!

Thanks!

    
02.04.2014 / 14:42