Complex Windowed Sum

  • Greetings,
    Your assistance is kindly requested for the following problem

    Description :
    A series of transactions issuing (I) or redeeming (R) bonds.
    A customer may be issued bonds one or more times. The bonds are entered in an "account" called UnitNo.
    A customer may have more than one UnitNo.
    A customer may have bonds redeemed. The withdrawals are made from a specific UnitNo.
    The ordering of the transactions is critical, otherwise the percentages will mean nothing.

    The requirements are :
    1) A running total of bond quantities issues and redeemed
    2) The total quantities of each customer combing all of the customer's UnitNo's
    3) The percentage of the customers' quantities w/r to the running total of bonds in circulation

    I think I have managed to do it.

    The specific question :
    1) I need to add two more columns showing for one specific customer (a) the current total of that customer and (b) the percentage of his bond quantities w/r to the total running count.
    2) And while I got your attention, I also need to add a third column showing in which fiscal year the transactions took place - Considering an April 30 fiscal year end. (I always get confused with this accounting complication.)

    Thanks in advance for your help

    Table creation
    -------------------

    CREATE TABLE [dbo].[TestData]

    (

    [TrxNo] [int] NOT NULL Primary Key,

    [TrxType] [varchar](1) NOT NULL,

    [UnitNo] [varchar](255) NOT NULL,

    [Customer] [varchar](10) NOT NULL,

    [TrxDate] [date] NOT NULL,

    [TrxQty] [int] NOT NULL

    );


    CREATE TABLE [dbo].[TestResults]

    (

    [TrxNo] [int] NOT NULL Primary Key,

    [TrxType] [varchar](1) NOT NULL,

    [UnitNo] [varchar](255) NOT NULL,

    [Customer] [varchar](10) NOT NULL,

    [TrxDate] [date] NOT NULL,

    [TrxQty] [int] NOT NULL,

    [CustomerTotal] [int] NOT NULL DEFAULT 0.0,

    [RunningTotal] [int] NOT NULL DEFAULT 0.0,

    [CustomerPercentage] [money] NOT NULL DEFAULT 0.0,

    [C0004TotalQty] [int] NOT NULL DEFAULT 0.0,

    [C0004Percentage] [money] NOT NULL DEFAULT 0.0,

    [FiscalYear] [int] NULL

    );

    Test Data
    ---------

    INSERT

    INTO [TestData]

    (

    [TrxNo],

    [TrxType],

    [UnitNo],

    [Customer],

    [TrxDate],

    [TrxQty]

    )

    SELECT 1, 'I', 'XBR-64 ', 'C0001' , '2004-07-06', 50000 UNION

    SELECT 2, 'I', 'XBR-63', 'C0002' , '2004-07-10', 125000 UNION

    SELECT 3, 'I', 'XBR-65', 'C0003' , '2004-07-19', 5000 UNION

    SELECT 4, 'I', 'XBR-66', 'C0004' , '2004-07-19', 70000 UNION

    SELECT 5, 'I', 'XBR-67', 'C0004' , '2004-07-19', 50000 UNION

    SELECT 6, 'I', 'XBR-68', 'C0005' , '2004-07-19', 50000 UNION

    SELECT 7, 'I', 'XBR-69', 'C0004' , '2004-09-25', 200000 UNION

    SELECT 8, 'I', 'XBR-70', 'C0004' , '2004-09-25', 100000 UNION

    SELECT 9, 'I', 'XBR-71 ', 'C0001', '2004-11-15', 80000 UNION

    SELECT 10, 'I', 'XBR-72', 'C0003' , '2004-12-10', 5000 UNION

    SELECT 11, 'I', 'XBR-75', 'C0007' , '2005-03-14', 15000 UNION

    SELECT 12, 'I', 'XBR-57', 'C0009' , '2005-04-01', 30000 UNION

    SELECT 13, 'I', 'XBR-37', 'C0007' , '2005-07-01', 25000 UNION

    SELECT 14, 'I', 'XBR-76', 'C0003' , '2005-07-26', 10000 UNION

    SELECT 15, 'I', 'XBR-77', 'C0010' , '2005-09-21', 25000 UNION

    SELECT 16, 'I', 'XBR-80', 'C0004' , '2005-10-21', 700000 UNION

    SELECT 17, 'I', 'XBR-78', 'C0003' , '2005-10-24', 10000 UNION

    SELECT 18, 'R', 'XBR-79', 'C0007' , '2005-10-24', -30000 UNION

    SELECT 19, 'I', 'XBR-83', 'C0004' , '2006-03-13', 600000 UNION

    SELECT 20, 'R', 'XBR-57', 'C0009' , '2006-04-01', -30000 UNION

    SELECT 21, 'I', 'XBR-92', 'C0004' , '2008-02-21', 500000 UNION

    SELECT

    22, 'I', 'XBR-93', 'C0004' , '2008-02-21'

    , 500000

    -- Solution attempt

    INSERT

    INTO [TestResults]

    (

    [TrxNo],

    [TrxType],

    [UnitNo],

    [Customer],

    [TrxDate],

    [TrxQty],

    [CustomerTotal],

    [RunningTotal],

    [CustomerPercentage]

    )

    SELECT

    [TrxNo],

    [TrxType],

    [UnitNo],

    [Customer],

    [TrxDate],

    [TrxQty],

    SUM([TrxQty]) OVER (PARTITION BY [Customer] ORDER BY [TrxNo]),

    SUM([TrxQty]) OVER (ORDER BY [TrxNo]),

    (SUM([TrxQty]) OVER (PARTITION BY [Customer] ORDER BY [TrxNo]) * 100) / SUM([TrxQty]) OVER (ORDER BY [TrxNo])

    FROM [TestData]

    ORDER BY [TrxNo]

    Expected Results

    TrxNoTrxTypeUnitNoCustomerTrxDateTrxQtyCustomerTotalRunningTotalCustomer
    Percentage
    C0004
    TotalQty
    C0004
    Percentage
    Fiscal
    Year
    1IXBR-64 C00012004-07-0650,00050,00050,000100.0000.00
    2IXBR-63C00022004-07-10125,000125,000175,00071.0000.00
    3IXBR-65C00032004-07-195,0005,000180,0002.0000.00
    4IXBR-66C00042004-07-1970,00070,000250,00028.0070,00028.00
    5IXBR-67C00042004-07-1950,000120,000300,00040.00120,00040.00
    6IXBR-68C00052004-07-1950,00050,000350,00014.00120,00034.29
    7IXBR-69C00042004-09-25200,000320,000550,00058.00320,00058.18
    8IXBR-70C00042004-09-25100,000420,000650,00064.00420,00064.62
    9IXBR-71 C00012004-11-1580,000130,000730,00017.00420,00057.53
    10IXBR-72C00032004-12-105,00010,000735,0001.00420,00057.14
    11IXBR-75C00072005-03-1415,00015,000750,0002.00420,00056.00
    12IXBR-57C00092005-04-0130,00030,000780,0003.00420,00053.85
    13IXBR-37C00072005-07-0125,00040,000805,0004.00420,00052.17
    14IXBR-76C00032005-07-2610,00020,000815,0002.00420,00051.53
    15IXBR-77C00102005-09-2125,00025,000840,0002.00420,00050.00
    16IXBR-80C00042005-10-21700,0001,120,0001,540,00072.001,120,00072.73
    17IXBR-78C00032005-10-2410,00030,0001,550,0001.001,120,00072.26
    18RXBR-79C00072005-10-24-30,00010,0001,520,0000.001,120,00073.68
    19IXBR-83C00042006-03-13600,0001,720,0002,120,00081.001,720,00081.13
    20RXBR-57C00092006-04-01-30,00002,090,0000.001,720,00082.30
    21IXBR-92C00042008-02-21500,0002,220,0002,590,00085.002,220,00085.71
    22IXBR-93C00042008-02-21500,0002,720,0003,090,00088.002,720,00088.03

  • It's fairly simple.
    To get just a Customer's data, you can use a CASE statement.
    To get the fiscal year ending on April 30th, you just substract 4 months.

    SUM(CASE WHEN Customer = 'C0004' THEN TrxQty ELSE 0 END) OVER (PARTITION BY [Customer] ORDER BY [TrxNo]),
      (SUM(CASE WHEN Customer = 'C0004' THEN TrxQty ELSE 0 END) OVER (PARTITION BY [Customer] ORDER BY [TrxNo]) * 100.) / SUM([TrxQty]) OVER (ORDER BY [TrxNo]),
      YEAR(DATEADD(MM,-4,trxDate))

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thank you for your prompt reply.

    I just got harpooned in a staff meeting, so I will try it later tonight.

    Regards

  • Finally out that meeting.

    Works like a cham.

    Thanks a million.

    It's been quite a while since I used SQL Server Central - there used to be a way to flag an answer as having solved the problem, but I cannot find it in the different user interface I am seeing today for the first time.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply