Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Select with count group by every week
Message
From
15/04/2024 11:51:15
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01687852
Message ID:
01687853
Views:
30
>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!!!!
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform