This topic is locked

Report Writing for last 7 days data

8/3/2009 1:33:12 AM
ASPRunnerPro General questions
A
Aussie Mick author

Hello, I am trying to write SQL code to produce only data for the last 7 days, this is for a report.
The table is called Call Log and I want the report to produce
Name

Branch

Date = where date is within the last 7 days.
Is this possible and if so how would I do it.
Thank you.
Aussie Mick

J
Jane 8/3/2009
hichem 8/6/2009

Hi Mick,

It really depends what RDBMS you are using, hence it is useful to indicate such information as SQL syntax differs and the worst is really SQL Server. Oracle and MySQL are quite similar.

Here's a way to do it with SQL Server.

It depends also on the date format you want as an output but make sure you are comparing the same date formats.

For simpler date comparisons, make sure your date field is datetime type.

-----

select

cl.name,

cl.branche

from call_log cl

where CONVERT(VARCHAR(10), cl.date, 120)>= CONVERT(VARCHAR(10), DATEADD (day, -7, GETDATE()), 120)

Group by cl.name, cl.branche

----
If you are using MySQL

replace your date condition with something like

cl.date > date_format((curdate() + interval -(7) day),'%d/%m/%Y %H:%i:%s')

-----
Hope this helps
Hich

C
clig 8/14/2009

Hello, I am trying to write SQL code to produce only data for the last 7 days, this is for a report.

The table is called Call Log and I want the report to produce
Name

Branch

Date = where date is within the last 7 days.
Is this possible and if so how would I do it.
Thank you.
Aussie Mick


Sample for a query filtered for previous week's worth of records regardless what day it was run:
(dbo.NTSS_Tickets_Strip.DateResolved BETWEEN DATEADD(dd, - (DATEPART(dw, GETDATE()) + 6), GETDATE()) AND DATEADD(dd, - DATEPART(dw, GETDATE()), GETDATE()))
Play with this sample or if you always want to work back from the moment the query is run 7 days then use:
(DateEntered BETWEEN DATEDIFF(DAY, 0, GETDATE()) - 7 AND DATEDIFF(DAY, 0, GETDATE()) + 1)

  • this example would return all records between and including Aug 7 - 14 if it was run today (Aug14) based on the column "DateEntered"
    SELECT * FROM dbo.NTSS_Tickets

    WHERE (DateEntered BETWEEN DATEDIFF(DAY, 0, GETDATE()) - 7 AND DATEDIFF(DAY, 0, GETDATE()) + 1)

    ORDER BY DateEntered