Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Select with count group by every week
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01687852
Message ID:
01687854
Views:
15
>>Hi,
>>
>>I am working on a report (in VFP 9) but the report is based on data in the MS SQL Server. This is why I put this question in the MS SQL Server Forum.
>>
>>The SQL Select needs to Count a number of records for various condition (see an example below). But what I find challenging is how to specify that the Group By should be on the Monday of every week.
>>
>>Here is an example:
>>
>>select [Monday_Of_Each_Week], Count(TableName.Deleted = 'D') as DEL_RECS, Count(TableName.Status = 'I' as STAT1_RECS from TableName 
>>group by [Monday_Of_Each_Week]
>>
>>
>>The Monday of each week would be as follows:
>>The first Monday of 2024 is 01/01/2024, 2nd Monday is 01/08/2024, and so on. Basically the program should select the first week (01/01/2024) where Monday falls on the year. For example, next year (2025), the first week will be the week of 01/06/2025 (because the first Monday in the year is 01/06/2025).
>>
>>UPDATE. I am updating the SQL Select above because I need to show the count of records for each week (52 weeks in the year). Therefore, the SQL Select should select 52 records.
>>
>>Any suggestions?
>>
>>TIA.
>Check
>https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-ver16
>
>DATEPART() function
>
>
>Something like
>
>select DATEPART(wk, DateField)            AS WeekNumber
>        , Count(TableName.Deleted = 'D') AS DEL_RECS
>        , Count(TableName.Status = 'I')     AS STAT1_RECS
>from TableName 
>WHERE YEAR(DateField) = 2024 -- 2023??
>group by DATEPART(wk, DateField) 
>
>
>NOT TESTED!!!!

Borislav,
Thank you very much. I will try this approach.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform