Calculating Balance using sql The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableHow to Delete using INNER JOIN with SQL Server?

Missile strike detection (but it's not actually a missile)

A Man With a Stainless Steel Endoskeleton (like The Terminator) Fighting Cloaked Aliens Only He Can See

Would a completely good Muggle be able to use a wand?

Is it professional to write unrelated content in an almost-empty email?

Should I tutor a student who I know has cheated on their homework?

What steps are necessary to read a Modern SSD in Medieval Europe?

Easy to read palindrome checker

Unclear about dynamic binding

How do I align (1) and (2)?

Does increasing your ability score affect your main stat?

The exact meaning of 'Mom made me a sandwich'

Powershell. How to parse gci Name?

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

Why, when going from special to general relativity, do we just replace partial derivatives with covariant derivatives?

Make solar eclipses exceedingly rare, but still have new moons

Is French Guiana a (hard) EU border?

0 rank tensor vs 1D vector

Prepend last line of stdin to entire stdin

What does "Its cash flow is deeply negative" mean?

Does Germany produce more waste than the US?

Why isn't the Mueller report being released completely and unredacted?

How to place nodes around a circle from some initial angle?

Why isn't acceleration always zero whenever velocity is zero, such as the moment a ball bounces off a wall?

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?



Calculating Balance using sql



The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableHow to Delete using INNER JOIN with SQL Server?










2















I am using below query to get balance for a certain customer



SELECT t.[InvoiceID], t.S_Type as Type,
t.Date, t.Debit, t.Credit, b.Balance
FROM Statement as t CROSS apply
(SELECT Balance = SUM(Debit) - SUM(Credit)
FROM Statement as x
WHERE (x.date < t.date or
x.date = t.date
) AND
x.CustID = t.CustID
) b
WHERE t.CustID ='1' and date between '2015-01-01' and '2016-01-12'
order by InvoiceID, Type desc, Date


out put



InvoiceID Type Date Debit Credit Balance
3 Sales Invoice 2015-06-09 200.00 0.00 225.00
3 Receipt Voucher 2016-01-04 0.00 200.00 0.00
5 Sales Invoice 2015-06-09 25.00 0.00 225.00
5 Receipt Voucher 2016-01-04 0.00 25.00 0.00


as you can see balance values are in correct



is supposed to be



InvoiceID Type Date Debit Credit Balance
3 Sales Invoice 2015-06-09 200.00 0.00 200.00
3 Receipt Voucher 2016-01-04 0.00 200.00 0.00
5 Sales Invoice 2015-06-09 25.00 0.00 25.00
5 Receipt Voucher 2016-01-04 0.00 25.00 0.00


update



when i changed x.CustID = t.CustID to x.InvoiceID = t.InvoiceID i tried with another customer that has all sales invoices the balance give the same debit values it dose not sum the debit values



out put



InvoiceID Type Date Debit Credit Balance
1 Sales Invoice 2015-06-09 200.00 0.00 200.00
10 Sales Invoice 2015-06-09 850.00 0.00 850.00
12 Sales Invoice 2015-06-09 20.00 0.00 20.00
59 Sales Invoice 2015-09-03 0.00 0.00 0.00









share|improve this question
























  • please give us the sample input data ... they are required to give answer

    – Dhaval
    Jan 12 '16 at 13:20
















2















I am using below query to get balance for a certain customer



SELECT t.[InvoiceID], t.S_Type as Type,
t.Date, t.Debit, t.Credit, b.Balance
FROM Statement as t CROSS apply
(SELECT Balance = SUM(Debit) - SUM(Credit)
FROM Statement as x
WHERE (x.date < t.date or
x.date = t.date
) AND
x.CustID = t.CustID
) b
WHERE t.CustID ='1' and date between '2015-01-01' and '2016-01-12'
order by InvoiceID, Type desc, Date


out put



InvoiceID Type Date Debit Credit Balance
3 Sales Invoice 2015-06-09 200.00 0.00 225.00
3 Receipt Voucher 2016-01-04 0.00 200.00 0.00
5 Sales Invoice 2015-06-09 25.00 0.00 225.00
5 Receipt Voucher 2016-01-04 0.00 25.00 0.00


as you can see balance values are in correct



is supposed to be



InvoiceID Type Date Debit Credit Balance
3 Sales Invoice 2015-06-09 200.00 0.00 200.00
3 Receipt Voucher 2016-01-04 0.00 200.00 0.00
5 Sales Invoice 2015-06-09 25.00 0.00 25.00
5 Receipt Voucher 2016-01-04 0.00 25.00 0.00


update



when i changed x.CustID = t.CustID to x.InvoiceID = t.InvoiceID i tried with another customer that has all sales invoices the balance give the same debit values it dose not sum the debit values



out put



InvoiceID Type Date Debit Credit Balance
1 Sales Invoice 2015-06-09 200.00 0.00 200.00
10 Sales Invoice 2015-06-09 850.00 0.00 850.00
12 Sales Invoice 2015-06-09 20.00 0.00 20.00
59 Sales Invoice 2015-09-03 0.00 0.00 0.00









share|improve this question
























  • please give us the sample input data ... they are required to give answer

    – Dhaval
    Jan 12 '16 at 13:20














2












2








2








I am using below query to get balance for a certain customer



SELECT t.[InvoiceID], t.S_Type as Type,
t.Date, t.Debit, t.Credit, b.Balance
FROM Statement as t CROSS apply
(SELECT Balance = SUM(Debit) - SUM(Credit)
FROM Statement as x
WHERE (x.date < t.date or
x.date = t.date
) AND
x.CustID = t.CustID
) b
WHERE t.CustID ='1' and date between '2015-01-01' and '2016-01-12'
order by InvoiceID, Type desc, Date


out put



InvoiceID Type Date Debit Credit Balance
3 Sales Invoice 2015-06-09 200.00 0.00 225.00
3 Receipt Voucher 2016-01-04 0.00 200.00 0.00
5 Sales Invoice 2015-06-09 25.00 0.00 225.00
5 Receipt Voucher 2016-01-04 0.00 25.00 0.00


as you can see balance values are in correct



is supposed to be



InvoiceID Type Date Debit Credit Balance
3 Sales Invoice 2015-06-09 200.00 0.00 200.00
3 Receipt Voucher 2016-01-04 0.00 200.00 0.00
5 Sales Invoice 2015-06-09 25.00 0.00 25.00
5 Receipt Voucher 2016-01-04 0.00 25.00 0.00


update



when i changed x.CustID = t.CustID to x.InvoiceID = t.InvoiceID i tried with another customer that has all sales invoices the balance give the same debit values it dose not sum the debit values



out put



InvoiceID Type Date Debit Credit Balance
1 Sales Invoice 2015-06-09 200.00 0.00 200.00
10 Sales Invoice 2015-06-09 850.00 0.00 850.00
12 Sales Invoice 2015-06-09 20.00 0.00 20.00
59 Sales Invoice 2015-09-03 0.00 0.00 0.00









share|improve this question
















I am using below query to get balance for a certain customer



SELECT t.[InvoiceID], t.S_Type as Type,
t.Date, t.Debit, t.Credit, b.Balance
FROM Statement as t CROSS apply
(SELECT Balance = SUM(Debit) - SUM(Credit)
FROM Statement as x
WHERE (x.date < t.date or
x.date = t.date
) AND
x.CustID = t.CustID
) b
WHERE t.CustID ='1' and date between '2015-01-01' and '2016-01-12'
order by InvoiceID, Type desc, Date


out put



InvoiceID Type Date Debit Credit Balance
3 Sales Invoice 2015-06-09 200.00 0.00 225.00
3 Receipt Voucher 2016-01-04 0.00 200.00 0.00
5 Sales Invoice 2015-06-09 25.00 0.00 225.00
5 Receipt Voucher 2016-01-04 0.00 25.00 0.00


as you can see balance values are in correct



is supposed to be



InvoiceID Type Date Debit Credit Balance
3 Sales Invoice 2015-06-09 200.00 0.00 200.00
3 Receipt Voucher 2016-01-04 0.00 200.00 0.00
5 Sales Invoice 2015-06-09 25.00 0.00 25.00
5 Receipt Voucher 2016-01-04 0.00 25.00 0.00


update



when i changed x.CustID = t.CustID to x.InvoiceID = t.InvoiceID i tried with another customer that has all sales invoices the balance give the same debit values it dose not sum the debit values



out put



InvoiceID Type Date Debit Credit Balance
1 Sales Invoice 2015-06-09 200.00 0.00 200.00
10 Sales Invoice 2015-06-09 850.00 0.00 850.00
12 Sales Invoice 2015-06-09 20.00 0.00 20.00
59 Sales Invoice 2015-09-03 0.00 0.00 0.00






sql sql-server sql-server-2008






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 16:39









Cœur

19.1k9114155




19.1k9114155










asked Jan 12 '16 at 12:21









AymanAyman

464923




464923












  • please give us the sample input data ... they are required to give answer

    – Dhaval
    Jan 12 '16 at 13:20


















  • please give us the sample input data ... they are required to give answer

    – Dhaval
    Jan 12 '16 at 13:20

















please give us the sample input data ... they are required to give answer

– Dhaval
Jan 12 '16 at 13:20






please give us the sample input data ... they are required to give answer

– Dhaval
Jan 12 '16 at 13:20













2 Answers
2






active

oldest

votes


















1














you can use the following statement for your correct results:



SELECT t.[InvoiceID], t.S_Type as Type,
t.Date, t.Debit, t.Credit, b.Balance
FROM Statement as t CROSS apply
(SELECT Balance = SUM(Debit) - SUM(Credit)
FROM Statement as x
WHERE (x.date < t.date or
x.date = t.date
) AND
x.InvoiceID = t.InvoiceID
AND x.CustID = t.CustID
) b
WHERE t.CustID ='1' and date between '2015-01-01' and '2016-01-12'
order by InvoiceID, Type desc, Date





share|improve this answer

























  • thanks but please see the update in question when i change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID

    – Ayman
    Jan 12 '16 at 13:03











  • okay i'm seeing into this ...

    – Akshay ghavnta
    Jan 12 '16 at 13:05











  • add both of these conditions together in where

    – Akshay ghavnta
    Jan 12 '16 at 13:11












  • still have the issue

    – Ayman
    Jan 12 '16 at 13:14


















0














The results look correct to me. The first row of the results is returning invoiceId = 3 and the balance column is including the value from invoiceId=5 which happened on the same day for the same client and therefore should be included. If you only want the values by invoice then change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID






share|improve this answer























  • when i changed x.CustID = t.CustID to x.InvoiceID = t.InvoiceID i tried with another customer that has all sales invoices the balance give the same debit value it dose not sum the debit values

    – Ayman
    Jan 12 '16 at 12:35











  • I think perhaps you are misunderstanding your data and how you are querying it. What is your requirement for the balance column? Are you looking for a running balance for the client at the time of the invoice? If so then your initial results are correct: on the 09/06 the overall balance for the client is 225.00 because both invoices happened on the same day. Same with the 04/01.

    – Sean
    Jan 12 '16 at 13:09











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34743550%2fcalculating-balance-using-sql%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














you can use the following statement for your correct results:



SELECT t.[InvoiceID], t.S_Type as Type,
t.Date, t.Debit, t.Credit, b.Balance
FROM Statement as t CROSS apply
(SELECT Balance = SUM(Debit) - SUM(Credit)
FROM Statement as x
WHERE (x.date < t.date or
x.date = t.date
) AND
x.InvoiceID = t.InvoiceID
AND x.CustID = t.CustID
) b
WHERE t.CustID ='1' and date between '2015-01-01' and '2016-01-12'
order by InvoiceID, Type desc, Date





share|improve this answer

























  • thanks but please see the update in question when i change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID

    – Ayman
    Jan 12 '16 at 13:03











  • okay i'm seeing into this ...

    – Akshay ghavnta
    Jan 12 '16 at 13:05











  • add both of these conditions together in where

    – Akshay ghavnta
    Jan 12 '16 at 13:11












  • still have the issue

    – Ayman
    Jan 12 '16 at 13:14















1














you can use the following statement for your correct results:



SELECT t.[InvoiceID], t.S_Type as Type,
t.Date, t.Debit, t.Credit, b.Balance
FROM Statement as t CROSS apply
(SELECT Balance = SUM(Debit) - SUM(Credit)
FROM Statement as x
WHERE (x.date < t.date or
x.date = t.date
) AND
x.InvoiceID = t.InvoiceID
AND x.CustID = t.CustID
) b
WHERE t.CustID ='1' and date between '2015-01-01' and '2016-01-12'
order by InvoiceID, Type desc, Date





share|improve this answer

























  • thanks but please see the update in question when i change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID

    – Ayman
    Jan 12 '16 at 13:03











  • okay i'm seeing into this ...

    – Akshay ghavnta
    Jan 12 '16 at 13:05











  • add both of these conditions together in where

    – Akshay ghavnta
    Jan 12 '16 at 13:11












  • still have the issue

    – Ayman
    Jan 12 '16 at 13:14













1












1








1







you can use the following statement for your correct results:



SELECT t.[InvoiceID], t.S_Type as Type,
t.Date, t.Debit, t.Credit, b.Balance
FROM Statement as t CROSS apply
(SELECT Balance = SUM(Debit) - SUM(Credit)
FROM Statement as x
WHERE (x.date < t.date or
x.date = t.date
) AND
x.InvoiceID = t.InvoiceID
AND x.CustID = t.CustID
) b
WHERE t.CustID ='1' and date between '2015-01-01' and '2016-01-12'
order by InvoiceID, Type desc, Date





share|improve this answer















you can use the following statement for your correct results:



SELECT t.[InvoiceID], t.S_Type as Type,
t.Date, t.Debit, t.Credit, b.Balance
FROM Statement as t CROSS apply
(SELECT Balance = SUM(Debit) - SUM(Credit)
FROM Statement as x
WHERE (x.date < t.date or
x.date = t.date
) AND
x.InvoiceID = t.InvoiceID
AND x.CustID = t.CustID
) b
WHERE t.CustID ='1' and date between '2015-01-01' and '2016-01-12'
order by InvoiceID, Type desc, Date






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 12 '16 at 13:10

























answered Jan 12 '16 at 13:00









Akshay ghavntaAkshay ghavnta

112




112












  • thanks but please see the update in question when i change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID

    – Ayman
    Jan 12 '16 at 13:03











  • okay i'm seeing into this ...

    – Akshay ghavnta
    Jan 12 '16 at 13:05











  • add both of these conditions together in where

    – Akshay ghavnta
    Jan 12 '16 at 13:11












  • still have the issue

    – Ayman
    Jan 12 '16 at 13:14

















  • thanks but please see the update in question when i change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID

    – Ayman
    Jan 12 '16 at 13:03











  • okay i'm seeing into this ...

    – Akshay ghavnta
    Jan 12 '16 at 13:05











  • add both of these conditions together in where

    – Akshay ghavnta
    Jan 12 '16 at 13:11












  • still have the issue

    – Ayman
    Jan 12 '16 at 13:14
















thanks but please see the update in question when i change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID

– Ayman
Jan 12 '16 at 13:03





thanks but please see the update in question when i change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID

– Ayman
Jan 12 '16 at 13:03













okay i'm seeing into this ...

– Akshay ghavnta
Jan 12 '16 at 13:05





okay i'm seeing into this ...

– Akshay ghavnta
Jan 12 '16 at 13:05













add both of these conditions together in where

– Akshay ghavnta
Jan 12 '16 at 13:11






add both of these conditions together in where

– Akshay ghavnta
Jan 12 '16 at 13:11














still have the issue

– Ayman
Jan 12 '16 at 13:14





still have the issue

– Ayman
Jan 12 '16 at 13:14













0














The results look correct to me. The first row of the results is returning invoiceId = 3 and the balance column is including the value from invoiceId=5 which happened on the same day for the same client and therefore should be included. If you only want the values by invoice then change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID






share|improve this answer























  • when i changed x.CustID = t.CustID to x.InvoiceID = t.InvoiceID i tried with another customer that has all sales invoices the balance give the same debit value it dose not sum the debit values

    – Ayman
    Jan 12 '16 at 12:35











  • I think perhaps you are misunderstanding your data and how you are querying it. What is your requirement for the balance column? Are you looking for a running balance for the client at the time of the invoice? If so then your initial results are correct: on the 09/06 the overall balance for the client is 225.00 because both invoices happened on the same day. Same with the 04/01.

    – Sean
    Jan 12 '16 at 13:09















0














The results look correct to me. The first row of the results is returning invoiceId = 3 and the balance column is including the value from invoiceId=5 which happened on the same day for the same client and therefore should be included. If you only want the values by invoice then change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID






share|improve this answer























  • when i changed x.CustID = t.CustID to x.InvoiceID = t.InvoiceID i tried with another customer that has all sales invoices the balance give the same debit value it dose not sum the debit values

    – Ayman
    Jan 12 '16 at 12:35











  • I think perhaps you are misunderstanding your data and how you are querying it. What is your requirement for the balance column? Are you looking for a running balance for the client at the time of the invoice? If so then your initial results are correct: on the 09/06 the overall balance for the client is 225.00 because both invoices happened on the same day. Same with the 04/01.

    – Sean
    Jan 12 '16 at 13:09













0












0








0







The results look correct to me. The first row of the results is returning invoiceId = 3 and the balance column is including the value from invoiceId=5 which happened on the same day for the same client and therefore should be included. If you only want the values by invoice then change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID






share|improve this answer













The results look correct to me. The first row of the results is returning invoiceId = 3 and the balance column is including the value from invoiceId=5 which happened on the same day for the same client and therefore should be included. If you only want the values by invoice then change x.CustID = t.CustID to x.InvoiceID = t.InvoiceID







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 12 '16 at 12:26









SeanSean

689829




689829












  • when i changed x.CustID = t.CustID to x.InvoiceID = t.InvoiceID i tried with another customer that has all sales invoices the balance give the same debit value it dose not sum the debit values

    – Ayman
    Jan 12 '16 at 12:35











  • I think perhaps you are misunderstanding your data and how you are querying it. What is your requirement for the balance column? Are you looking for a running balance for the client at the time of the invoice? If so then your initial results are correct: on the 09/06 the overall balance for the client is 225.00 because both invoices happened on the same day. Same with the 04/01.

    – Sean
    Jan 12 '16 at 13:09

















  • when i changed x.CustID = t.CustID to x.InvoiceID = t.InvoiceID i tried with another customer that has all sales invoices the balance give the same debit value it dose not sum the debit values

    – Ayman
    Jan 12 '16 at 12:35











  • I think perhaps you are misunderstanding your data and how you are querying it. What is your requirement for the balance column? Are you looking for a running balance for the client at the time of the invoice? If so then your initial results are correct: on the 09/06 the overall balance for the client is 225.00 because both invoices happened on the same day. Same with the 04/01.

    – Sean
    Jan 12 '16 at 13:09
















when i changed x.CustID = t.CustID to x.InvoiceID = t.InvoiceID i tried with another customer that has all sales invoices the balance give the same debit value it dose not sum the debit values

– Ayman
Jan 12 '16 at 12:35





when i changed x.CustID = t.CustID to x.InvoiceID = t.InvoiceID i tried with another customer that has all sales invoices the balance give the same debit value it dose not sum the debit values

– Ayman
Jan 12 '16 at 12:35













I think perhaps you are misunderstanding your data and how you are querying it. What is your requirement for the balance column? Are you looking for a running balance for the client at the time of the invoice? If so then your initial results are correct: on the 09/06 the overall balance for the client is 225.00 because both invoices happened on the same day. Same with the 04/01.

– Sean
Jan 12 '16 at 13:09





I think perhaps you are misunderstanding your data and how you are querying it. What is your requirement for the balance column? Are you looking for a running balance for the client at the time of the invoice? If so then your initial results are correct: on the 09/06 the overall balance for the client is 225.00 because both invoices happened on the same day. Same with the 04/01.

– Sean
Jan 12 '16 at 13:09

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34743550%2fcalculating-balance-using-sql%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко