+ Reply to Thread
Page 2 of 2 FirstFirst 1 2
Results 11 to 12 of 12

Thread: Create History Table / Outcome results

  1. #11
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Hi Wolffy,

    I've populated the history table with all of 2008 records...my question is...

    since my table data looks like below, is there a way to use the datepart function to retreive the year from field (CurrentMoYr) to get the year...but how would I use this in my select case statment?


    Code:
    ToolID      RegionID        DistrictID      ShippedQty       CurrentMoYr     MoID
    1------------1-------------1-------------1--------------2/1/2008-----2
    1------------1-------------1-------------0--------------3/1/2008-----3
    
    i'd like to display like below if possible:

    Code:
    ToolName----Q1-08Shipped----Q2-08Shipped---- Q3-08Shipped---- Q4-08Shipped---- Q1-09Shipped...so on
    Tool1----------2----------4-------------0-----------3----------2
    Tool2----------1----------2-------------2-----------3----------1
    Tool3----------3----------2-------------3-----------3----------2
    Tool4----------3----------0-------------2-----------1----------1
    

  2. #12
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Hmmm...that might be a really ugly query. You might want to split this into two parts and first create a view that aggregates the tools by year and quarter -- that should make the report easier.
    Code:
    Select  ToolID
             , datepart(yyyy, CurrentMoYr) as Yr
             , datepart(q, CurrentMoYr) as Qtr
             , sum(ShippedQty) as ShippedQty
     From  myTable
     Group By ToolID, datepart(yyyy, CurrentMoYr), datepart(q, CurrnetMoYr)
    
    Given that, you should be able to pivot the table into the results you want.

    However, PIVOT is an SQL 2005 feature -- in SQL 2000 you're stuck with ugly case statements. Using the view above:
    Code:
    Select  ToolID
           ,  Case When Yr = 2008 and q = 1 Then ShippedQty End as 'Year2008-Q1'
           ,  Case When Yr = 2008 and q = 2 Then ShippedQty End as 'Year2008-Q2'
           ,  Case When Yr = 2008 and q = 3 Then ShippedQty End as 'year2008-Q3'
    From myView
    
    Yes, you'll need to add a Case statement for each Quarter.

    Or, do the Pivot transform in code from the view and build the output table at reporting time
    Last edited by Wolffy; June 10th, 2009 at 12:02 PM.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

+ Reply to Thread
Page 2 of 2 FirstFirst 1 2

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO