Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Seleziona gli ultimi 30 giorni con una query sql

Puoi usare un CTE ricorsivo:

;WITH CTE AS
(
    SELECT convert(date,GETDATE()) sDate, DATENAME(DW, GETDATE()) sDayofWeek
    UNION ALL
    SELECT DATEADD(DAY,-1,sDate), DATENAME(DW, DATEADD(DAY,-1,sDate))
    FROM CTE
    WHERE sDate > GETDATE()-29
)
SELECT * FROM CTE