SQL QUERY get Average one month of a relative day of the week

Asked

Viewed 1,051 times

0

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

SELECT AVG(P.Quant) AS Expr1
FROM     P
WHERE  (Pro.Tipo = 'B') AND (V.ID= 1)
AND (DATA =:....")
  • You mean "every Monday of February"?

2 answers

1

I don’t have 50 points to comment on, but I could 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)

In case it doesn’t work, send the structure of your table!

Valeuu!

  • 10 dots so you can make useful comments

1


If you want every Monday 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.

  • With Linq it is possible to use EXTRACT?

  • @user6018: I don’t know the Link but in MS-SQL Server there is the DATEPART function with the same functionality. Technet.microsoft.com/en-us/library/aa258863(v=sql.80). aspx

Browser other questions tagged

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