Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:28:59+00:00 2026-06-15T13:28:59+00:00

Possible Duplicate: How to group by the each week upto last six week sundays

  • 0

Possible Duplicate:
How to group by the each week upto last six week sundays dates in sql?

I need to take the count of weekno. It works fine but i don’t know the weekno that is [34],[35],[36],[37],[38],[39]. The weekno knows only on the runtime.For example in a year it contains totally 52 weeks. I got this query from the help of @Bluefeet, he posted the query on the same stackoverflow. If i change the from date and to date in the between field it doesn’t work. Because i hard coded the weekno in the below query. Please give me the solution whatever the date in the between field within the year.

SET DATEFIRST 1 
SELECT case when InstanceType is not null then InstanceType else 'Sum' End InstanceType ,  
  sum([34]) AS FirstWeek, 
  sum([35]) AS SecondWeek, 
  sum([36]) AS ThirdWeek, 
  sum([37]) AS FourthWeek, 
  sum([38]) AS FifthWeek, 
  sum([39]) AS SixthWeek,  
  max(InstanceDescription) AS InstanceDescription 
FROM 
( 
  SELECT [SPGI01_INSTANCE_TYPE_C] AS InstanceType, 
    [34], [35], [36], [37], [38], [39], InstanceDescription  
  FROM 
  ( 
    SELECT I01.[SPGI01_INSTANCE_TYPE_C], 
      DatePart(wk, I01.[SPGI01_CREATE_S]) WeekNo, 
      DATEADD(DAY, 7 -DATEPART(WEEKDAY,I01.[SPGI01_CREATE_S]),  I01.[SPGI01_CREATE_S]) WeekEnd, 
      J03.SPGJ03_MSG_TRANSLN_X InstanceDescription  
    FROM [SUPER-G].[dbo].[CSPGI01_ASN_ACCURACY] I01  
    INNER JOIN [SUPER-G].[dbo].[CSPGI50_VALID_INSTANCE_TYPE] I50 
      ON I50.[SPGI50_INSTANCE_TYPE_C] = I01.[SPGI01_INSTANCE_TYPE_C]  
    LEFT JOIN CSPGJ02_MSG_OBJ J02 
      ON I50.SPGJ02_MSG_K = J02.SPGJ02_MSG_K  
    LEFT JOIN CSPGJ03_MSG_TRANSLN J03 
      ON J02.SPGJ02_MSG_K = J03.SPGJ02_MSG_K  
    where I50.[SPGA04_RATING_ELEMENT_D] = 1  
      and I01.[SPGI01_EXCEPTIONED_F] = 'N' 
      and I01.[SPGI01_DISPUTED_F] != 'Y'  
      AND J03.[SPGJ03_LOCALE_C] =  'en_US'  
      and I01.[SPGA02_BUSINESS_TYPE_C] = 'PROD'  
      and I01.[SPGA03_REGION_C] = 'EU'  
      and I01.[SPGI01_SUB_BUSINESS_TYPE_C] = 'PRD'  
      and I01.[SPGI01_CREATE_S] between '10-08-2012 00:00:00.000' AND '11-18-2012 23:59:59.000'  
  ) x 
  pivot 
  ( 
    count(WeekEnd) 
    FOR weekno IN ([34], [35], [36], [37], [38], [39])  
  ) p 
) x1 
GROUP BY  InstanceType WITH ROLLUP 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:29:00+00:00Added an answer on June 15, 2026 at 1:29 pm

    If you are looking to be able to pass in any date values to get the data that meets your criteria, then for this type of PIVOT you will need to use a dynamic SQL solution similar to this:

    DECLARE @cols AS NVARCHAR(MAX),
        @colsRollup AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX),
        @StartDate DateTime,
        @EndDate DateTime
    
    Set @StartDate = '10-08-2012 00:00:00.000'
    Set @EndDate = '11-18-2012 23:59:59.000'
    
    select @cols = STUFF((SELECT ',' + QUOTENAME(WeekEnd) 
                        from
                        (
                          select DatePart(wk, I01.[SPGI01_CREATE_S]) WeekEnd
                          from [SUPER-G].[dbo].[CSPGI01_ASN_ACCURACY]
                          where I01.[SPGI01_CREATE_S] between @StartDate AND @EndDate
                        ) src
                        group by WeekEnd
                        order by WeekEnd desc
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    select @colsRollup = STUFF((SELECT ', Sum(' + QUOTENAME(WeekEnd) +') as WeekNo'+Cast(Weekend as varchar(2))
                        from
                        (
                          select DatePart(wk, I01.[SPGI01_CREATE_S]) WeekEnd
                          from [SUPER-G].[dbo].[CSPGI01_ASN_ACCURACY]
                          where I01.[SPGI01_CREATE_S] between @StartDate AND @EndDate
                        ) src
                        group by WeekEnd
                        order by WeekEnd desc
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = '
                 SELECT case when InstanceType is not null then InstanceType else ''Sum'' End InstanceType ,  
                    '+@colsRollup+',  max(InstanceDescription) AS InstanceDescription
                 FROM
                 (
                   SELECT SPGI01_INSTANCE_TYPE_C as InstanceType,
                        InstanceDescription, ' + @cols + ' 
                   from 
                   (
                      SELECT I01.[SPGI01_INSTANCE_TYPE_C], 
                        DatePart(wk, I01.[SPGI01_CREATE_S]) WeekNo, 
                        DATEADD(DAY, 7 -DATEPART(WEEKDAY,I01.[SPGI01_CREATE_S]),  I01.[SPGI01_CREATE_S]) WeekEnd, 
                        J03.SPGJ03_MSG_TRANSLN_X InstanceDescription  
                      FROM [SUPER-G].[dbo].[CSPGI01_ASN_ACCURACY] I01  
                      INNER JOIN [SUPER-G].[dbo].[CSPGI50_VALID_INSTANCE_TYPE] I50 
                        ON I50.[SPGI50_INSTANCE_TYPE_C] = I01.[SPGI01_INSTANCE_TYPE_C]  
                      LEFT JOIN CSPGJ02_MSG_OBJ J02 
                        ON I50.SPGJ02_MSG_K = J02.SPGJ02_MSG_K  
                      LEFT JOIN CSPGJ03_MSG_TRANSLN J03 
                        ON J02.SPGJ02_MSG_K = J03.SPGJ02_MSG_K  
                      where I50.[SPGA04_RATING_ELEMENT_D] = 1  
                        and I01.[SPGI01_EXCEPTIONED_F] = ''N'' 
                        and I01.[SPGI01_DISPUTED_F] != ''Y''  
                        AND J03.[SPGJ03_LOCALE_C] =  ''en_US''  
                        and I01.[SPGA02_BUSINESS_TYPE_C] = ''PROD'' 
                        and I01.[SPGA03_REGION_C] = ''EU''  
                        and I01.[SPGI01_SUB_BUSINESS_TYPE_C] = ''PRD''
                        and I01.[SPGI01_CREATE_S] between '+ convert(varchar(10), @StartDate, 120)+' AND '+ convert(varchar(10), @EndDate, 120)+'
                  ) x
                  pivot 
                  (
                      count(WeekEnd)
                      for weekno in (' + @cols + ')
                  ) p 
                 ) x1
                 GROUP BY InstanceType WITH ROLLUP '
    
    execute(@query)
    

    Note: this is untested since I do not have any sample data, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Retrieving the last record in each group I have two tables set
Possible Duplicate: SQL Server: Only last entry in GROUP BY I have a table
Possible Duplicate: SQL Server: Get Top 1 of Each Group I have 2 tables
Possible Duplicate: SQL: Select first row in each GROUP BY group? Two SQL tables.
Possible Duplicate: SQL Server Group Concat with Different characters I need an example on
Possible Duplicate: linq-to-sql group by with count and custom object model I have a
Possible Duplicate: Retrieving the last record in each group Hi all i am having
Possible Duplicate: Linq - Top value form each group I have data in the
Possible Duplicate: SQL ORDER BY total within GROUP BY UPDATE: I've found my solution,
Possible Duplicate: T-SQL Group Rows Into Columns I have a table _data that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.