Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Why do we need error handling?When changing column data types use ALTER TABLE TableName ALTER Column syntax, don't drop and recreate column »
    comments

    This recent MSDN thread presented an interesting, I believe, question:

    For a predefined set of the Application IDs show the weekly transactions and display Monday's day for each week. The interesting twist in this common PIVOT query is to display a Monday's date for a week.

    Rather than showing a solution for that particular thread, I show an AdventureWorks sample query. This query will return top 5 total amounts for each week by Customer (using SalesOrderHeader table).

    Here is the query

    1. DECLARE  @First_Bow      DATETIME,
    2.          @Week_Start_Day TINYINT
    3.  
    4. SET @Week_Start_Day = 2
    5.  
    6. SELECT @First_Bow = CONVERT(DATETIME,-53690 + ((@Week_Start_Day + 5)%7))
    7.  
    8. WITH cte
    9.      AS (SELECT SH.CustomerID,
    10.                 TotalDue            AS Total,
    11.                 DATEADD(DAY,(DATEDIFF(DAY,@FIRST_BOW,OrderDate) / 7) * 7,
    12.                         @FIRST_BOW) AS [MondayDate]
    13.          FROM   Sales.SalesOrderHeader SH),
    14.      cte2
    15.      AS (SELECT Total,
    16.                 MondayDate,
    17.                 ROW_NUMBER()
    18.                   OVER(PARTITION BY MondayDate ORDER BY Total DESC, CustomerID) AS Row
    19.          FROM   cte)
    20. SELECT *
    21. FROM   cte2
    22.        PIVOT
    23.        (SUM(Total)
    24.         FOR Row IN ( [1],[2],[3],[4],[5],[6] ) ) pvt


    The interesting part here is in determining the Monday's day for each week. I decided to use a solution presented in SQL Server Forums topic.

    *** Remember, if you have a SQL related question, try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    Naomi Nosonovsky, Senior Software Developer, has more than 15 years of enterprise experience in analysis, design, development and implementation of high-performance client/server data management solutions. She is a Personality of the Year for 2008, 2009, 2010 and 2011 at www.universalthread.com in .NET, SQL Server & VFP categories. She is also an All Star contributor/MCC Community Contributor at forums.asp.net and MSDN T-SQL forum.
    Social SitingsFacebookLinkedIndeliciousLTD RSS Feed
    770 views
    Instapaper

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)