Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2019 June SQL Puzzle: SQL Advance Query – To generate the account balance column for Bank Accounts

SQL Puzzle: SQL Advance Query – To generate the account balance column for Bank Accounts

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

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:

1
2
3
4
5
6
7
8
9
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

Expected Output:

1
2
3
4
5
6
7
8
9
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

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

Solution:

1
2
3
4
5
6
7
8
9
10
;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 <= c1.rnk ) AccountBalance
FROM CTE c1

Please try the different solution for this puzzle and share it via comment...

Jun 17, 2019Anvesh Patel
PostgreSQL: Disable Non-Durable parameters and Improve Server PerformanceSQL Server: Script to check Network or Connect permission of User
Comments: 5
  1. Ankesh Patel
    July 15, 2019 at 9:01 am

    Update AccountBalance
    set amount=amount*-1
    where type=’DB’

    –then give rank based on the account in the given table itself

    select
    TransactionDate,
    Accname,
    Type,
    Amount,
    IDs,
    (select sum(amount) from AccountBalance a where a.accname=b.accname and a.rank<=b.rank) real_time_balance
    from AccountBalance b

    ReplyCancel
  2. Deepak Tomar
    July 18, 2019 at 10:06 am

    Hi Avnesh ,

    There is a window function that can do the same task as well.

    ;WITH cte AS (
    SELECT
    [TransactionDate]
    ,[AccName]
    ,[Type]
    ,[Amount]
    ,CASE WHEN [Type] = ‘DB’ THEN -1 * [Amount]
    ELSE [Amount] END AS [NewAmount]
    FROM AccountBalance

    )

    SELECT
    [TransactionDate]
    ,[AccName]
    ,[Type]
    ,[Amount]
    , SUM(cte.NewAmount) OVER (PARTITION BY [AccName] ORDER BY [TransactionDate] ROWS UNBOUNDED PRECEDING) AS [AccountBalance]
    FROM cte

    ReplyCancel
  3. Sai Nath
    August 11, 2019 at 7:18 am

    ; WITH CTE AS
    (
    SELECT
    *,
    ROW_NUMBER() OVER(PARTITION BY ACCNAME ORDER BY (SELECT 1)) AS RW
    FROM
    AccountBalance
    )

    SELECT
    *,
    SUM(CASE WHEN TYPE = ‘DB’ THEN -AMOUNT ELSE AMOUNT END) OVER(PARTITION BY ACCNAME ORDER BY RW)
    FROM
    CTE

    ReplyCancel
  4. Samrat
    October 24, 2019 at 7:45 am

    SELECT * ,
    SUM(CASE WHEN TYPE = ‘DB’ THEN Amount*-1 ELSE Amount END) OVER (PARTITION BY AccName ORDER BY AccName,TransactionDate ) AS BALANCE
    FROM dbo.ACCOUNTBALANCE

    ReplyCancel
  5. Anand Kumar
    May 25, 2020 at 3:25 am

    Is it possible to get a blank line after change in accname ?

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

June 17, 2019 5 Comments SQL PuzzleAnvesh Patel, bank, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks, transaction
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....