<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bank &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/bank/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Mon, 24 Jun 2019 19:38:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; To generate the account balance column for Bank Accounts</title>
		<link>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/</link>
					<comments>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 17 Jun 2019 16:16:59 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[bank]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[transaction]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5860</guid>

					<description><![CDATA[Check the below input data and expected output to prepare the report on bank account transaction like generate the data of AccountBalance column where for CR plus the transaction value and for DR minus the transaction value. Input Data: [crayon-6a8f48878d712316416265/] Expected Output: [crayon-6a8f48878d715726060328/] Create a table with sample data: [crayon-6a8f48878d716627147578/] Solution: [crayon-6a8f48878d717508990966/] Please try the [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" data-a2a-url="https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/" data-a2a-title="SQL Puzzle: SQL Advance Query – To generate the account balance column for Bank Accounts"></a></p><p>Check the below input data and expected output to prepare the report on bank account transaction like generate the data of AccountBalance column where for CR plus the transaction value and for DR minus the transaction value.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">TransactionDate AccName    Type Amount
--------------- ---------- ---- -------------
2017-01-01      Anvesh     CR   60000.00
2017-02-01      Anvesh     DB   8000.00
2017-03-01      Anvesh     CR   8000.00
2017-04-01      Anvesh     DB   5000.00
2017-01-01      Nupur      CR   10000.00
2017-02-02      Nupur      CR   8000.00
2017-03-03      Nupur      DB   8000.00</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">TransactionDate AccName    Type Amount                AccountBalance
--------------- ---------- ---- --------------------- ---------------
2017-01-01      Anvesh     CR   60000.00              60000.00
2017-02-01      Anvesh     DB   8000.00               52000.00
2017-03-01      Anvesh     CR   8000.00               60000.00
2017-04-01      Anvesh     DB   5000.00               55000.00
2017-01-01      Nupur      CR   10000.00              10000.00
2017-02-02      Nupur      CR   8000.00               18000.00
2017-03-03      Nupur      DB   8000.00               10000.00</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE AccountBalance
(
	TransactionDate DATE
	,AccName VARCHAR(10)
	,Type VARCHAR(2)
	,Amount MONEY
)
GO

INSERT INTO AccountBalance
VALUES
('2017-01-01','Anvesh','CR','60000')
,('2017-02-01','Anvesh','DB','8000')
,('2017-03-01','Anvesh','CR','8000')
,('2017-04-01','Anvesh','DB','5000')
,('2017-01-01','Nupur','CR','10000')
,('2017-02-02','Nupur','CR','8000')
,('2017-03-03','Nupur','DB','8000')
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE AS
(
    SELECT TransactionDate , AccName , Type 
            , CASE WHEN Type = 'DB' THEN AMOUNT * -1 ELSE AMOUNT END Amount, Amount Amt
            ,ROW_NUMBER() OVER (PARTITION BY AccName ORDER BY (SELECT NULL)) rnk
    FROM AccountBalance
)
SELECT  TransactionDate , AccName , Type , Amt Amount
, (SELECT SUM(Amount) FROM CTE c2 WHERE c2.AccName = c1.AccName AND c2.rnk &lt;= c1.rnk ) AccountBalance
FROM CTE c1</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
