Running total error

  • pwalter83 (6/23/2014)


    mister.magoo (6/23/2014)


    mister.magoo (6/20/2014)


    Can I ask what is not working in the SSRS RunningTotal, it may just be a scope problem?

    Evil Kraig F (6/20/2014)


    pwalter83 (6/20/2014)


    I am trying to calculate running total in sql as its not giving me the desired result on SSRS (on the fly calculation).

    What's happening in the SSRS that you're getting the incorrect value? Going to SQL for Running Totals is tossing the screwdriver aside to grab the sledgehammer to drive the screw.

    Can you please explain what is going wrong with your SSRS report?

    I had initially used the calculation within ssrs but it does not get the desired result. This is a parameter based report with the start and end week values. What happens is the running totals calculation begins from the start week (selected by the user) and do not account for the calculation before this week which is incorrect. For eg. if there was a running total balance till end of week 201410. This is does not show up if I select the start week as 201411. It just begins a new running total balance.

    Thanks.

    Well, you could just provide the opening balance for that week in your dataset, either as an extra row, in which case the runningtotal function will work OR as an extra column on every row and add that column to the RunningTotal in your report expression e.g. = RunningTotal(nothing)+Fields!OpeningBalance.Value

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Sachin Nandanwar (6/20/2014)


    pwalter83 (6/20/2014)


    Sachin Nandanwar (6/20/2014)


    I dont understand the problem here.Your query seems to be achieving it.

    CREATE TABLE [dbo].[GL](

    [Sequence] [int] NOT NULL,

    [WK NO] [int] NULL,

    [AC] [nvarchar](10) NULL,

    [COMP] [nvarchar](10) NULL,

    [USD_AMOUNT] [numeric](16, 2) NULL,

    [SOA_TYPE] [nvarchar](20) NULL

    ) ON [PRIMARY]

    insert into GL

    values('1','9','16201','12003','100','Agent')

    insert into GL

    values('2','10','16204','12003','200','Freight')

    insert into GL

    values('3','11','16207','12003','150','Agent')

    insert into GL

    values('5','12','16208','12003','250','Costs')

    insert into GL

    values('6','13','16224','12003','400','Cost')

    insert into GL

    values('7','14','16295','12003','500','Freight')

    insert into GL

    values('8','15','16295','12003','540','Cost')

    GO

    SELECT

    (SELECT SUM(YourSum) FROM

    (SELECT SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') or AC = '16208'THEN USD_AMOUNT ELSE 0 END) YourSum

    FROM GL b WHERE b.[WK NO] < = a.[WK NO])T) as [Running Total]

    FROM GL a

    GROUP BY a.[WK NO], a.COMP

    ORDER BY a.[WK NO], a.COMP

    No its not working, I just checked it. This is the resultset that I get:

    WKNO AUSD FUSD CUST agent_.. WK BAL Running Total

    9100.000.000.000.00100.001552722.28

    100.00200.000.000.00200.006866522.21

    11150.000.000.000.00150.008658921.50

    120.000.000.00250.00250.00-2440896.12

    130.000.00400.000.00400.00-3911241.30

    140.00500.000.000.00500.004083099.88

    150.000.00540.000.00540.009459770.45

    The Running total is all incorrect. I dont why its showing values in millions and decimals. Do you have any idea ?

    Thanks.

    Did you even run the query ?

    This is the result set coming out from the sample data provided.

    100.00

    300.00

    450.00

    700.00

    1100.00

    1600.00

    2140.00

    Thanks Sachin, your query works for the example above. But when I use the same query for the real time data, then I get the following result:

    -----------------------------------------

    Balance Running total

    -4746150.73-3193528.45

    5848249.312120271.48

    3978540.103912670.77

    -11281250.17-7187146.85

    1510035.33-8657492.03

    7970271.90-663150.85

    3984689.014713519.72

    -5852800.30-1231699.63

    -3445298.44-4377286.58

    6397520.684743091.52

    -6775787.54-2362136.98

    8698325.615365730.54

    2774214.0111705235.28

    -8173551.593490920.33

    8223711.257537798.42

    --------------------------------

    Do you have any suggestion as to why the running total is not correctly calculated for the Balance column ? Even the opening values differ (-4746150.73 and -3193528.45)

    Thanks.

    )

  • Paul, you have two threads open which cover the same problem. Can I suggest you choose one of them and close the other? You can do that by posting something like "No further posts to this thread, please see ..." with a hyperlink to the thread you have chosen.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (6/23/2014)


    Paul, you have two threads open which cover the same problem. Can I suggest you choose one of them and close the other? You can do that by posting something like "No further posts to this thread, please see ..." with a hyperlink to the thread you have chosen.

    Thanks, I have closed the other thread.

    Could you please suggest how to do a Running total on this query below:

    SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) as [WK BALANCE]

    Thanks.

  • pwalter83 (6/23/2014)


    ChrisM@Work (6/23/2014)


    Paul, you have two threads open which cover the same problem. Can I suggest you choose one of them and close the other? You can do that by posting something like "No further posts to this thread, please see ..." with a hyperlink to the thread you have chosen.

    Thanks, I have closed the other thread.

    Could you please suggest how to do a Running total on this query below:

    SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) as [WK BALANCE]

    Thanks.

    That's...not really how it works! Can I suggest that you confirm this can't be achieved in SSRS before you resort to TSQL? Two or three folks on this thread appear to be fairly confident that it can. Since it was your original plan and TSQL was plan B, it makes sense.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (6/23/2014)


    pwalter83 (6/23/2014)


    ChrisM@Work (6/23/2014)


    Paul, you have two threads open which cover the same problem. Can I suggest you choose one of them and close the other? You can do that by posting something like "No further posts to this thread, please see ..." with a hyperlink to the thread you have chosen.

    Thanks, I have closed the other thread.

    Could you please suggest how to do a Running total on this query below:

    SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) as [WK BALANCE]

    Thanks.

    That's...not really how it works! Can I suggest that you confirm this can't be achieved in SSRS before you resort to TSQL? Two or three folks on this thread appear to be fairly confident that it can. Since it was your original plan and TSQL was plan B, it makes sense.

    I expected some solution at the end of this lengthy discussion but this has led to nowhere. I am at the same point where I started.

  • pwalter83 (6/23/2014)


    ChrisM@Work (6/23/2014)


    pwalter83 (6/23/2014)


    ChrisM@Work (6/23/2014)


    Paul, you have two threads open which cover the same problem. Can I suggest you choose one of them and close the other? You can do that by posting something like "No further posts to this thread, please see ..." with a hyperlink to the thread you have chosen.

    Thanks, I have closed the other thread.

    Could you please suggest how to do a Running total on this query below:

    SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) as [WK BALANCE]

    Thanks.

    That's...not really how it works! Can I suggest that you confirm this can't be achieved in SSRS before you resort to TSQL? Two or three folks on this thread appear to be fairly confident that it can. Since it was your original plan and TSQL was plan B, it makes sense.

    I expected some solution at the end of this lengthy discussion but this has led to nowhere. I am at the same point where I started.

    Reading back a bit through this thread, we have unreal data and code that works but it doesn't work on the data. So, let's get back to the basics.

    Please see the first link under "Helpful Links" in my signature line below. Provide the CREATE TABLE statements and some readily consumable test data in the manner identified in the article. This running total thing isn't difficult but it's tough to demo without the right kind of data. It would be extremely helpful if you included the CREATE statements for the indexes on the tables, as well, because indexes will determine the route we need to take on this problem.

    Also, as I said on the other thread, although the <= criteria will work in the creation of running totals, it could be (and usually is) really bad for performance and resource usage because, in the presence of aggregates, it forms a "Triangular Join" that's a little bit more than half as bad as a full up accidental CROSS JOIN (Cartesian Product). That's a part of the reason why I need to see the data.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (6/23/2014)


    pwalter83 (6/23/2014)


    ChrisM@Work (6/23/2014)


    pwalter83 (6/23/2014)


    ChrisM@Work (6/23/2014)


    Paul, you have two threads open which cover the same problem. Can I suggest you choose one of them and close the other? You can do that by posting something like "No further posts to this thread, please see ..." with a hyperlink to the thread you have chosen.

    Thanks, I have closed the other thread.

    Could you please suggest how to do a Running total on this query below:

    SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) as [WK BALANCE]

    Thanks.

    That's...not really how it works! Can I suggest that you confirm this can't be achieved in SSRS before you resort to TSQL? Two or three folks on this thread appear to be fairly confident that it can. Since it was your original plan and TSQL was plan B, it makes sense.

    I expected some solution at the end of this lengthy discussion but this has led to nowhere. I am at the same point where I started.

    Reading back a bit through this thread, we have unreal data and code that works but it doesn't work on the data. So, let's get back to the basics.

    Please see the first link under "Helpful Links" in my signature line below. Provide the CREATE TABLE statements and some readily consumable test data in the manner identified in the article. This running total thing isn't difficult but it's tough to demo without the right kind of data. It would be extremely helpful if you included the CREATE statements for the indexes on the tables, as well, because indexes will determine the route we need to take on this problem.

    Also, as I said on the other thread, although the <= criteria will work in the creation of running totals, it could be (and usually is) really bad for performance and resource usage because, in the presence of aggregates, it forms a "Triangular Join" that's a little bit more than half as bad as a full up accidental CROSS JOIN (Cartesian Product). That's a part of the reason why I need to see the data.

    I agree with Jeff. I have run your code against the sample data you provided and with the exception of being off 540.00 from your expected results (there is no 540.00 for the first entry in your sample data, but the expected results you posted shows 540.00 more for that entry) it works as expected.

  • Lynn Pettis (6/23/2014)


    Jeff Moden (6/23/2014)


    pwalter83 (6/23/2014)


    ChrisM@Work (6/23/2014)


    pwalter83 (6/23/2014)


    ChrisM@Work (6/23/2014)


    Paul, you have two threads open which cover the same problem. Can I suggest you choose one of them and close the other? You can do that by posting something like "No further posts to this thread, please see ..." with a hyperlink to the thread you have chosen.

    Thanks, I have closed the other thread.

    Could you please suggest how to do a Running total on this query below:

    SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) as [WK BALANCE]

    Thanks.

    That's...not really how it works! Can I suggest that you confirm this can't be achieved in SSRS before you resort to TSQL? Two or three folks on this thread appear to be fairly confident that it can. Since it was your original plan and TSQL was plan B, it makes sense.

    I expected some solution at the end of this lengthy discussion but this has led to nowhere. I am at the same point where I started.

    Reading back a bit through this thread, we have unreal data and code that works but it doesn't work on the data. So, let's get back to the basics.

    Please see the first link under "Helpful Links" in my signature line below. Provide the CREATE TABLE statements and some readily consumable test data in the manner identified in the article. This running total thing isn't difficult but it's tough to demo without the right kind of data. It would be extremely helpful if you included the CREATE statements for the indexes on the tables, as well, because indexes will determine the route we need to take on this problem.

    Also, as I said on the other thread, although the <= criteria will work in the creation of running totals, it could be (and usually is) really bad for performance and resource usage because, in the presence of aggregates, it forms a "Triangular Join" that's a little bit more than half as bad as a full up accidental CROSS JOIN (Cartesian Product). That's a part of the reason why I need to see the data.

    I agree with Jeff. I have run your code against the sample data you provided and with the exception of being off 540.00 from your expected results (there is no 540.00 for the first entry in your sample data, but the expected results you posted shows 540.00 more for that entry) it works as expected.

    Thanks. I have tried but it the query doesnt work with decimal values. Could you please try with the following sample data again:

    ----------------------------

    insert into GLT

    values('1','9','16201','12003','-63807.41','Agent')

    insert into GLT

    values('2','10','16204','12003','460714.25','Freight')

    insert into GLT

    values('3','11','16207','12003','-2667204.23','Agent')

    insert into GLT

    values('5','12','16208','12003','687871.30','Costs')

    insert into GLT

    values('6','13','16224','12003','38231.14','Cost')

    insert into GLT

    values('7','14','16295','12003','14098.86','Freight')

    insert into GLT

    values('8','15','16295','12003','4013.10','Cost')

    insert into GLT

    values('9','9','16295','12004','-1544194.95','Agent')

    insert into GLT

    values('10','9','16295','12004','687871.30','Agent')

    ----------------------------

    Please find the ddl and query below for your reference:

    ---------------------

    CREATE TABLE [dbo].[GLT](

    [Sequence] [int] NULL,

    [WK NO] [int] NULL,

    [AC] [nvarchar](10) NULL,

    [COMP] [nvarchar](10) NULL,

    [USD_AMOUNT] [numeric](16, 2) NULL,

    [SOA_TYPE] [nvarchar](20) NULL

    ) ON [PRIMARY]

    SELECT a.[WK NO], MAX(AC) as AC, MAX(comp) as comp,

    SUM(CASE WHEN SOA_TYPE = 'Agent' THEN USD_AMOUNT ELSE 0 END) AS AUSD,

    SUM(CASE WHEN SOA_TYPE = 'Freight' THEN USD_AMOUNT ELSE 0 END) AS FUSD,

    SUM(CASE WHEN SOA_TYPE = 'Cost' THEN USD_AMOUNT ELSE 0 END) AS CUST,

    SUM(CASE WHEN (SOA_TYPE = 'Remittance to Agent' OR SOA_TYPE = 'Fund to Agent') THEN USD_AMOUNT ELSE 0 END) AS [AGENT not in deal],

    SUM(CASE WHEN AC = '16208' THEN USD_AMOUNT ELSE 0 END) AS AGENT_notindeal_WKBALANCE,

    SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') or AC = '16208' THEN USD_AMOUNT ELSE 0 END) as [WK BALANCE],

    (SELECT SUM(YourSum) FROM

    (SELECT SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') or AC = '16208'THEN USD_AMOUNT ELSE 0 END) YourSum

    FROM GLT b WHERE b.[WK NO] < = a.[WK NO])T) as [Running Total]

    FROM GLT a

    /*

    RIGHT OUTER JOIN

    Company ON a.COMP = Company.[AG Code]

    */

    --and [COMP] = '12003'

    GROUP BY a.[WK NO], [COMP]

    ORDER BY a.[WK NO], [COMP]

    -----------------------

    Thanks.

  • Here's a quick rejig of that sample data, and a reformat of the code. Is it only the running total column which is incorrect?

    IF OBJECT_ID ('tempdb..#GLT') IS NOT NULL DROP TABLE #GLT

    CREATE TABLE #GLT (

    [Sequence] [int] NULL,

    [WK NO] [int] NULL,

    [AC] [nvarchar](10) NULL,

    [COMP] [nvarchar](10) NULL,

    [USD_AMOUNT] [numeric](16, 2) NULL,

    [SOA_TYPE] [nvarchar](20) NULL

    ) ON [PRIMARY]

    insert into #GLT values

    ('1','9','16201','12003','-63807.41','Agent'),

    ('2','10','16204','12003','460714.25','Freight'),

    ('3','11','16207','12003','-2667204.23','Agent'),

    ('5','12','16208','12003','687871.30','Costs'),

    ('6','13','16224','12003','38231.14','Cost'),

    ('7','14','16295','12003','14098.86','Freight'),

    ('8','15','16295','12003','4013.10','Cost'),

    ('9','9','16295','12004','-1544194.95','Agent'),

    ('10','9','16295','12004','687871.30','Agent')

    ----------------------------------------------------------------------------------------

    SELECT a.[WK NO],

    AC= MAX(AC),

    comp= MAX(comp),

    AUSD= SUM(CASE WHEN SOA_TYPE = 'Agent' THEN USD_AMOUNT ELSE 0 END),

    FUSD= SUM(CASE WHEN SOA_TYPE = 'Freight' THEN USD_AMOUNT ELSE 0 END),

    CUST= SUM(CASE WHEN SOA_TYPE = 'Cost' THEN USD_AMOUNT ELSE 0 END),

    [AGENT not in deal] = SUM(CASE WHEN (SOA_TYPE = 'Remittance to Agent' OR SOA_TYPE = 'Fund to Agent') THEN USD_AMOUNT ELSE 0 END),

    AGENT_notindeal_WKBALANCE = SUM(CASE WHEN AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [WK BALANCE]= SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') or AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [Running Total] = (

    SELECT SUM(YourSum)

    FROM ( -- t

    SELECT SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) YourSum

    FROM #GLT b WHERE b.[WK NO] < = a.[WK NO]

    ) t

    ) -- [Running Total]

    FROM #GLT a

    GROUP BY a.[WK NO], [COMP]

    ORDER BY a.[WK NO], [COMP]

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (6/24/2014)


    Here's a quick rejig of that sample data, and a reformat of the code. Is it only the running total column which is incorrect?

    IF OBJECT_ID ('tempdb..#GLT') IS NOT NULL DROP TABLE #GLT

    CREATE TABLE #GLT (

    [Sequence] [int] NULL,

    [WK NO] [int] NULL,

    [AC] [nvarchar](10) NULL,

    [COMP] [nvarchar](10) NULL,

    [USD_AMOUNT] [numeric](16, 2) NULL,

    [SOA_TYPE] [nvarchar](20) NULL

    ) ON [PRIMARY]

    insert into #GLT values

    ('1','9','16201','12003','-63807.41','Agent'),

    ('2','10','16204','12003','460714.25','Freight'),

    ('3','11','16207','12003','-2667204.23','Agent'),

    ('5','12','16208','12003','687871.30','Costs'),

    ('6','13','16224','12003','38231.14','Cost'),

    ('7','14','16295','12003','14098.86','Freight'),

    ('8','15','16295','12003','4013.10','Cost'),

    ('9','9','16295','12004','-1544194.95','Agent'),

    ('10','9','16295','12004','687871.30','Agent')

    ----------------------------------------------------------------------------------------

    SELECT a.[WK NO],

    AC= MAX(AC),

    comp= MAX(comp),

    AUSD= SUM(CASE WHEN SOA_TYPE = 'Agent' THEN USD_AMOUNT ELSE 0 END),

    FUSD= SUM(CASE WHEN SOA_TYPE = 'Freight' THEN USD_AMOUNT ELSE 0 END),

    CUST= SUM(CASE WHEN SOA_TYPE = 'Cost' THEN USD_AMOUNT ELSE 0 END),

    [AGENT not in deal] = SUM(CASE WHEN (SOA_TYPE = 'Remittance to Agent' OR SOA_TYPE = 'Fund to Agent') THEN USD_AMOUNT ELSE 0 END),

    AGENT_notindeal_WKBALANCE = SUM(CASE WHEN AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [WK BALANCE]= SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') or AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [Running Total] = (

    SELECT SUM(YourSum)

    FROM ( -- t

    SELECT SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) YourSum

    FROM #GLT b WHERE b.[WK NO] < = a.[WK NO]

    ) t

    ) -- [Running Total]

    FROM #GLT a

    GROUP BY a.[WK NO], [COMP]

    ORDER BY a.[WK NO], [COMP]

    Thanks.

    Yes, its only the running total calculation which is incorrect.

  • pwalter83 (6/24/2014)


    ChrisM@Work (6/24/2014)


    Here's a quick rejig of that sample data, and a reformat of the code. Is it only the running total column which is incorrect?

    IF OBJECT_ID ('tempdb..#GLT') IS NOT NULL DROP TABLE #GLT

    CREATE TABLE #GLT (

    [Sequence] [int] NULL,

    [WK NO] [int] NULL,

    [AC] [nvarchar](10) NULL,

    [COMP] [nvarchar](10) NULL,

    [USD_AMOUNT] [numeric](16, 2) NULL,

    [SOA_TYPE] [nvarchar](20) NULL

    ) ON [PRIMARY]

    insert into #GLT values

    ('1','9','16201','12003','-63807.41','Agent'),

    ('2','10','16204','12003','460714.25','Freight'),

    ('3','11','16207','12003','-2667204.23','Agent'),

    ('5','12','16208','12003','687871.30','Costs'),

    ('6','13','16224','12003','38231.14','Cost'),

    ('7','14','16295','12003','14098.86','Freight'),

    ('8','15','16295','12003','4013.10','Cost'),

    ('9','9','16295','12004','-1544194.95','Agent'),

    ('10','9','16295','12004','687871.30','Agent')

    ----------------------------------------------------------------------------------------

    SELECT a.[WK NO],

    AC= MAX(AC),

    comp= MAX(comp),

    AUSD= SUM(CASE WHEN SOA_TYPE = 'Agent' THEN USD_AMOUNT ELSE 0 END),

    FUSD= SUM(CASE WHEN SOA_TYPE = 'Freight' THEN USD_AMOUNT ELSE 0 END),

    CUST= SUM(CASE WHEN SOA_TYPE = 'Cost' THEN USD_AMOUNT ELSE 0 END),

    [AGENT not in deal] = SUM(CASE WHEN (SOA_TYPE = 'Remittance to Agent' OR SOA_TYPE = 'Fund to Agent') THEN USD_AMOUNT ELSE 0 END),

    AGENT_notindeal_WKBALANCE = SUM(CASE WHEN AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [WK BALANCE]= SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') or AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [Running Total] = (

    SELECT SUM(YourSum)

    FROM ( -- t

    SELECT SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) YourSum

    FROM #GLT b WHERE b.[WK NO] < = a.[WK NO]

    ) t

    ) -- [Running Total]

    FROM #GLT a

    GROUP BY a.[WK NO], [COMP]

    ORDER BY a.[WK NO], [COMP]

    Thanks.

    Yes, its only the running total calculation which is incorrect.

    In that data, you have entries for "Cost" and one entry for "Costs". It that one entry supposed to be just "Cost?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (6/24/2014)


    pwalter83 (6/24/2014)


    ChrisM@Work (6/24/2014)


    Here's a quick rejig of that sample data, and a reformat of the code. Is it only the running total column which is incorrect?

    IF OBJECT_ID ('tempdb..#GLT') IS NOT NULL DROP TABLE #GLT

    CREATE TABLE #GLT (

    [Sequence] [int] NULL,

    [WK NO] [int] NULL,

    [AC] [nvarchar](10) NULL,

    [COMP] [nvarchar](10) NULL,

    [USD_AMOUNT] [numeric](16, 2) NULL,

    [SOA_TYPE] [nvarchar](20) NULL

    ) ON [PRIMARY]

    insert into #GLT values

    ('1','9','16201','12003','-63807.41','Agent'),

    ('2','10','16204','12003','460714.25','Freight'),

    ('3','11','16207','12003','-2667204.23','Agent'),

    ('5','12','16208','12003','687871.30','Costs'),

    ('6','13','16224','12003','38231.14','Cost'),

    ('7','14','16295','12003','14098.86','Freight'),

    ('8','15','16295','12003','4013.10','Cost'),

    ('9','9','16295','12004','-1544194.95','Agent'),

    ('10','9','16295','12004','687871.30','Agent')

    ----------------------------------------------------------------------------------------

    SELECT a.[WK NO],

    AC= MAX(AC),

    comp= MAX(comp),

    AUSD= SUM(CASE WHEN SOA_TYPE = 'Agent' THEN USD_AMOUNT ELSE 0 END),

    FUSD= SUM(CASE WHEN SOA_TYPE = 'Freight' THEN USD_AMOUNT ELSE 0 END),

    CUST= SUM(CASE WHEN SOA_TYPE = 'Cost' THEN USD_AMOUNT ELSE 0 END),

    [AGENT not in deal] = SUM(CASE WHEN (SOA_TYPE = 'Remittance to Agent' OR SOA_TYPE = 'Fund to Agent') THEN USD_AMOUNT ELSE 0 END),

    AGENT_notindeal_WKBALANCE = SUM(CASE WHEN AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [WK BALANCE]= SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') or AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [Running Total] = (

    SELECT SUM(YourSum)

    FROM ( -- t

    SELECT SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) YourSum

    FROM #GLT b WHERE b.[WK NO] < = a.[WK NO]

    ) t

    ) -- [Running Total]

    FROM #GLT a

    GROUP BY a.[WK NO], [COMP]

    ORDER BY a.[WK NO], [COMP]

    Thanks.

    Yes, its only the running total calculation which is incorrect.

    In that data, you have entries for "Cost" and one entry for "Costs". It that one entry supposed to be just "Cost?

    Sorry about the typo. it is supposed to be Cost.

    Thanks.

  • pwalter83 (6/24/2014)


    Jeff Moden (6/24/2014)


    pwalter83 (6/24/2014)


    ChrisM@Work (6/24/2014)


    Here's a quick rejig of that sample data, and a reformat of the code. Is it only the running total column which is incorrect?

    IF OBJECT_ID ('tempdb..#GLT') IS NOT NULL DROP TABLE #GLT

    CREATE TABLE #GLT (

    [Sequence] [int] NULL,

    [WK NO] [int] NULL,

    [AC] [nvarchar](10) NULL,

    [COMP] [nvarchar](10) NULL,

    [USD_AMOUNT] [numeric](16, 2) NULL,

    [SOA_TYPE] [nvarchar](20) NULL

    ) ON [PRIMARY]

    insert into #GLT values

    ('1','9','16201','12003','-63807.41','Agent'),

    ('2','10','16204','12003','460714.25','Freight'),

    ('3','11','16207','12003','-2667204.23','Agent'),

    ('5','12','16208','12003','687871.30','Costs'),

    ('6','13','16224','12003','38231.14','Cost'),

    ('7','14','16295','12003','14098.86','Freight'),

    ('8','15','16295','12003','4013.10','Cost'),

    ('9','9','16295','12004','-1544194.95','Agent'),

    ('10','9','16295','12004','687871.30','Agent')

    ----------------------------------------------------------------------------------------

    SELECT a.[WK NO],

    AC= MAX(AC),

    comp= MAX(comp),

    AUSD= SUM(CASE WHEN SOA_TYPE = 'Agent' THEN USD_AMOUNT ELSE 0 END),

    FUSD= SUM(CASE WHEN SOA_TYPE = 'Freight' THEN USD_AMOUNT ELSE 0 END),

    CUST= SUM(CASE WHEN SOA_TYPE = 'Cost' THEN USD_AMOUNT ELSE 0 END),

    [AGENT not in deal] = SUM(CASE WHEN (SOA_TYPE = 'Remittance to Agent' OR SOA_TYPE = 'Fund to Agent') THEN USD_AMOUNT ELSE 0 END),

    AGENT_notindeal_WKBALANCE = SUM(CASE WHEN AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [WK BALANCE]= SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') or AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [Running Total] = (

    SELECT SUM(YourSum)

    FROM ( -- t

    SELECT SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) YourSum

    FROM #GLT b WHERE b.[WK NO] < = a.[WK NO]

    ) t

    ) -- [Running Total]

    FROM #GLT a

    GROUP BY a.[WK NO], [COMP]

    ORDER BY a.[WK NO], [COMP]

    Thanks.

    Yes, its only the running total calculation which is incorrect.

    In that data, you have entries for "Cost" and one entry for "Costs". It that one entry supposed to be just "Cost?

    Sorry about the typo. it is supposed to be Cost.

    Thanks.

    Paul, can you show us what you expect to see in the running totals column please? It would be nice if you could do this by adding an extra column to your sample data in the script above. Thanks.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (6/24/2014)


    pwalter83 (6/24/2014)


    Jeff Moden (6/24/2014)


    pwalter83 (6/24/2014)


    ChrisM@Work (6/24/2014)


    Here's a quick rejig of that sample data, and a reformat of the code. Is it only the running total column which is incorrect?

    IF OBJECT_ID ('tempdb..#GLT') IS NOT NULL DROP TABLE #GLT

    CREATE TABLE #GLT (

    [Sequence] [int] NULL,

    [WK NO] [int] NULL,

    [AC] [nvarchar](10) NULL,

    [COMP] [nvarchar](10) NULL,

    [USD_AMOUNT] [numeric](16, 2) NULL,

    [SOA_TYPE] [nvarchar](20) NULL

    ) ON [PRIMARY]

    insert into #GLT values

    ('1','9','16201','12003','-63807.41','Agent'),

    ('2','10','16204','12003','460714.25','Freight'),

    ('3','11','16207','12003','-2667204.23','Agent'),

    ('5','12','16208','12003','687871.30','Costs'),

    ('6','13','16224','12003','38231.14','Cost'),

    ('7','14','16295','12003','14098.86','Freight'),

    ('8','15','16295','12003','4013.10','Cost'),

    ('9','9','16295','12004','-1544194.95','Agent'),

    ('10','9','16295','12004','687871.30','Agent')

    ----------------------------------------------------------------------------------------

    SELECT a.[WK NO],

    AC= MAX(AC),

    comp= MAX(comp),

    AUSD= SUM(CASE WHEN SOA_TYPE = 'Agent' THEN USD_AMOUNT ELSE 0 END),

    FUSD= SUM(CASE WHEN SOA_TYPE = 'Freight' THEN USD_AMOUNT ELSE 0 END),

    CUST= SUM(CASE WHEN SOA_TYPE = 'Cost' THEN USD_AMOUNT ELSE 0 END),

    [AGENT not in deal] = SUM(CASE WHEN (SOA_TYPE = 'Remittance to Agent' OR SOA_TYPE = 'Fund to Agent') THEN USD_AMOUNT ELSE 0 END),

    AGENT_notindeal_WKBALANCE = SUM(CASE WHEN AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [WK BALANCE]= SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') or AC = '16208' THEN USD_AMOUNT ELSE 0 END),

    [Running Total] = (

    SELECT SUM(YourSum)

    FROM ( -- t

    SELECT SUM(CASE WHEN SOA_TYPE IN ('Agent','Freight','Cost') OR AC = '16208' THEN USD_AMOUNT ELSE 0 END) YourSum

    FROM #GLT b WHERE b.[WK NO] < = a.[WK NO]

    ) t

    ) -- [Running Total]

    FROM #GLT a

    GROUP BY a.[WK NO], [COMP]

    ORDER BY a.[WK NO], [COMP]

    Thanks.

    Yes, its only the running total calculation which is incorrect.

    In that data, you have entries for "Cost" and one entry for "Costs". It that one entry supposed to be just "Cost?

    Sorry about the typo. it is supposed to be Cost.

    Thanks.

    Paul, can you show us what you expect to see in the running totals column please? It would be nice if you could do this by adding an extra column to your sample data in the script above. Thanks.

    Thanks Chris.

    In the Running total column, it needs to calculate the running total for the WK BALANCE column as shown below (at present the calculation is incorrect):

    91620112003-63807.410.000.000.000.00-63807.41-920131.06

    91629512004-856323.650.000.000.000.00-856323.65-920131.06

    1016204120030.00460714.250.000.000.00460714.25-459416.81

    111620712003-2667204.230.000.000.000.00-2667204.23-3126621.04

    1216208120030.000.000.000.00687871.30687871.30-2438749.74

    1316224120030.000.0038231.140.000.0038231.14-2400518.60

    1416295120030.0014098.860.000.000.0014098.86-2386419.74

    1516295120030.000.004013.100.000.004013.10-2382406.64

  • Viewing 15 posts - 31 through 45 (of 75 total)

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