1

I have set of data, which shows minutes used by a "child contract" on a daily basis. I need to have a running total of the minutes consumed by the child contract so that we can keep track.

This is the code I used.

Select 
    "combined"."id", 
    "combined"."ContractCustomer", 
    "combined"."ContractCustomerChild", 
    "combined"."Minutes",
     SUM( "combined"."Minutes") OVER (PARTITION BY      "combined"."ContractCustomerChild" ORDER BY "combined"."id") As "CustomerMinutes",
FROM "dbo"."combined"

I expect the sum to be restart for every contract child, however, it does seem to behave that way.

This is the result i got:

   ID CustomerContractChild Minutes CustomerMinutes 
       1 20150101+C1          1000       1000     
       2 20150101+C1          2000       3000 
       3 20150101+c2          2500       5500

This is what I expect

   ID CustomerContractChild Minutes CustomerMinutes 
       1 20150101+C1          1000       1000     
       2 20150101+C1          2000       3000 
       3 20150101+c2          2500       2500

What did I do wrongly?

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
AperurePro
  • 11
  • 2

2 Answers2

0

I have run this and get exact output u required. This is same as yours without ContractCustomer column (u don't show it in result) and column ContractCustomerChild in code is showing CustomerContractChild in results and removing , before FROM.

Select 
    "combined".ID, 
    "combined"."CustomerContractChild", 
    "combined"."Minutes",
     SUM( "combined"."Minutes") OVER (PARTITION BY      "combined"."CustomerContractChild" ORDER BY "combined"."id") As "CustomerMinutes"
FROM "dbo"."combined"
Esty
  • 1,882
  • 3
  • 17
  • 36
  • Thank you for the taking the time. Yes my mistake I do have a column with ContractCustomer. In essence the contractcustomer is the same customercontractchild except for the suffix +C1, +C2,+C3. – AperurePro Nov 03 '15 at 06:23
  • Doesn't matter. And then your query was correct for required output. Did ur problem solved?? – Esty Nov 03 '15 at 07:25
  • For complex running total problem this post may be useful. [link](http://stackoverflow.com/questions/11310877/calculate-running-total-running-balance) – Esty Nov 03 '15 at 07:29
  • I rerain the code with contractcustomer and without, the results in the same. It seems the result became wrong after I joint this view to another table. I have to trace it. – AperurePro Nov 03 '15 at 07:39
  • Tanjim Rahman, thanks for the link. I'm trying to trace at which point the results get messed up. I have removed my joints and will track the result through every step. – AperurePro Nov 03 '15 at 09:30
0

Thank you for the responses. My eyes must have been playing trick on me due to sorting etc. I reviewed the results and they look correct. I will close this for now. I'll sample a few more results and have a go if they turn out wrong.

AperurePro
  • 11
  • 2