简体   繁体   中英

Sql select statement that will bring back my data in weeks, using derived tables with JasperReports

I'm currently running the following sql statement in JasperReports Server to bring back my data using derived tables.

Select count(createddate) as ModulesCreatedDuringPastWeek, 
count(updateddate) as  ModulesUpdatedDuringPastWeek, 
createddate, 
updateddate 
from merchendisingmodule 
group by merchendisingmodule.createddate, merchendisingmodule.updateddate

However when grouping my data, I am only able to do it in Year, quarter, month and day. However for my report I'm needing the data to be group weeks, and so I was wondering what I will need to add to my code to do this.

DATEADD(D,-DATEPART(weekday,createddate)+1,createddate)

I use this method to prevent issues around the year transitions (week 53 in first days of januari and also in the last days of december, will group days together that are 360 days apart). I use the first day of the week, instead of week numbers. I can use these dates to group by. Also this will ensure that every week is 7 days long, instead of the last week of the year being only 3 or 4 days long.

Btw, in this example the first day of the week is sunday.

If your dates include time, use:

CAST(FLOOR(CAST(createddate AS FLOAT)) AS DATETIME)

instead of createddate in the above SYNTAX

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM