Get 'balanced' column value as 'Y' if both rows has same amt and 'N' if not in SQL query

Tiger

I have number of CR records and DR records against the same id , I need to check sum of DR amount matches with the sum of CR amount for the same id if CR matches with DR then display Balanced column as "Y" else "N"

Required Result: Balanced" column value 'Y' if the Credit and Debit amt column matches and 'N' if they mismatch

Note: single query is needed for this:

enter image description here

As shown in above screen grab red highlighted result is required .

Script is given below :

USE [data]
GO
/****** Object:  Table [dbo].[BankData]    Script Date: 04-05-2018 3.54.46 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[BankData](
    [ID] [int] NOT NULL,
    [Name] [nchar](10) NULL,
    [Amt] [decimal](18, 0) NULL,
    [Type] [char](10) NULL,
    [TransId] [int] IDENTITY(1,1) NOT NULL,
    [CustId] [int] NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Customer]    Script Date: 04-05-2018 3.54.46 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Customer](
    [CustId] [int] NULL,
    [Address] [nvarchar](50) NULL
) ON [PRIMARY]

GO
SET IDENTITY_INSERT [dbo].[BankData] ON 

INSERT [dbo].[BankData] ([ID], [Name], [Amt], [Type], [TransId], [CustId]) VALUES (1, N'YASH      ', CAST(300 AS Decimal(18, 0)), N'DR        ', 1, 1)
INSERT [dbo].[BankData] ([ID], [Name], [Amt], [Type], [TransId], [CustId]) VALUES (1, N'YASH      ', CAST(300 AS Decimal(18, 0)), N'CR        ', 2, 1)
INSERT [dbo].[BankData] ([ID], [Name], [Amt], [Type], [TransId], [CustId]) VALUES (2, N'FALE      ', CAST(120 AS Decimal(18, 0)), N'DR        ', 3, 2)
INSERT [dbo].[BankData] ([ID], [Name], [Amt], [Type], [TransId], [CustId]) VALUES (2, N'FALE      ', CAST(140 AS Decimal(18, 0)), N'CR        ', 4, 2)
INSERT [dbo].[BankData] ([ID], [Name], [Amt], [Type], [TransId], [CustId]) VALUES (3, N'RAHUL     ', CAST(100 AS Decimal(18, 0)), N'CR        ', 5, 3)
INSERT [dbo].[BankData] ([ID], [Name], [Amt], [Type], [TransId], [CustId]) VALUES (3, N'RAHUL     ', CAST(100 AS Decimal(18, 0)), N'DR        ', 6, 3)
INSERT [dbo].[BankData] ([ID], [Name], [Amt], [Type], [TransId], [CustId]) VALUES (4, N'DINESH    ', CAST(900 AS Decimal(18, 0)), N'CR        ', 7, 4)
INSERT [dbo].[BankData] ([ID], [Name], [Amt], [Type], [TransId], [CustId]) VALUES (4, N'DINESH    ', CAST(900 AS Decimal(18, 0)), N'DR        ', 8, 4)
INSERT [dbo].[BankData] ([ID], [Name], [Amt], [Type], [TransId], [CustId]) VALUES (2, N'FALE      ', CAST(30 AS Decimal(18, 0)), N'DR        ', 9, 2)
INSERT [dbo].[BankData] ([ID], [Name], [Amt], [Type], [TransId], [CustId]) VALUES (2, N'FALE      ', CAST(130 AS Decimal(18, 0)), N'DR        ', 10, 2)
SET IDENTITY_INSERT [dbo].[BankData] OFF
INSERT [dbo].[Customer] ([CustId], [Address]) VALUES (1, N'Mumbai')
INSERT [dbo].[Customer] ([CustId], [Address]) VALUES (2, N'Delhi')
INSERT [dbo].[Customer] ([CustId], [Address]) VALUES (3, N'Pune')
INSERT [dbo].[Customer] ([CustId], [Address]) VALUES (4, N'Banglore')
INSERT [dbo].[Customer] ([CustId], [Address]) VALUES (5, N'Surat')
John Cappelletti

If 2012+, this is a small matter using the window function sum() over

Example

Select *
      ,Balanced = IIF(sum(Amt * IIF([Type]='CR',-1,1)) over (Partition By ID) =0,'Y','N')
 from [BankData]

Returns

ID  Name    Amt Type    TransId CustId  Balanced
1   YASH        300 DR          1   1   Y
1   YASH        300 CR          2   1   Y
2   FALE        120 DR          3   2   N
2   FALE        140 CR          4   2   N
2   FALE        30  DR          9   2   N
2   FALE        130 DR          10  2   N
3   RAHUL       100 CR          5   3   Y
3   RAHUL       100 DR          6   3   Y
4   DINESH      900 CR          7   4   Y
4   DINESH      900 DR          8   4   Y

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL query: same rows

From Dev

SQL query to add a value x in a column if a column has Y value

From Dev

MySql query : Get sum of N rows where N is defined in another column value

From Dev

Get two types of value in the same column SQL

From Dev

SQL - Get the index of column that has maximum value

From Dev

How to get all rows that has same ID but diffrent value in other column?

From Dev

In R Merging rows where a column has same value but different case

From Dev

Check column if has the same value on all the rows

From Dev

SQL rows sharing same column value and with where

From Dev

How to get SQL query HAVING count display BOTH records with the same value where INNER JOIN exists

From Dev

SQL Query - Return rows where based on dual conditioning of same column

From Dev

SQL query to get most prevalent value in a column

From Dev

SQL - SELECT a column that compares multiple rows of the same query

From Dev

mysql query select multiple rows where column value is same

From Dev

sql query to return all rows with same column value

From Dev

get all rows from table that have both variables in a same column

From Dev

SQL query: same rows

From Dev

SQL Query: Get rows where value of certain column is maximum

From Dev

Select rows if column has same value more times

From Dev

How to get SQL query HAVING count display BOTH records with the same value where INNER JOIN exists

From Dev

Get total value of the same column multiple rows and total of two different rows (mysql query)

From Dev

Fetch ids for two consecutive rows whose column has same value

From Dev

SQL query exclude a certain value from same column same table?

From Dev

sql: select rows which have the same column value in another column with a single query

From Dev

Query where table has multiple values for a column and the same value in another?

From Dev

Find row that has same value in one column over multiple rows while another column has different values

From Dev

SQL display rows with grouping rows with same one column value

From Dev

SQL Select item which has same value in all rows

From Dev

Return rows that of both table that has value in month column

Related Related

  1. 1

    SQL query: same rows

  2. 2

    SQL query to add a value x in a column if a column has Y value

  3. 3

    MySql query : Get sum of N rows where N is defined in another column value

  4. 4

    Get two types of value in the same column SQL

  5. 5

    SQL - Get the index of column that has maximum value

  6. 6

    How to get all rows that has same ID but diffrent value in other column?

  7. 7

    In R Merging rows where a column has same value but different case

  8. 8

    Check column if has the same value on all the rows

  9. 9

    SQL rows sharing same column value and with where

  10. 10

    How to get SQL query HAVING count display BOTH records with the same value where INNER JOIN exists

  11. 11

    SQL Query - Return rows where based on dual conditioning of same column

  12. 12

    SQL query to get most prevalent value in a column

  13. 13

    SQL - SELECT a column that compares multiple rows of the same query

  14. 14

    mysql query select multiple rows where column value is same

  15. 15

    sql query to return all rows with same column value

  16. 16

    get all rows from table that have both variables in a same column

  17. 17

    SQL query: same rows

  18. 18

    SQL Query: Get rows where value of certain column is maximum

  19. 19

    Select rows if column has same value more times

  20. 20

    How to get SQL query HAVING count display BOTH records with the same value where INNER JOIN exists

  21. 21

    Get total value of the same column multiple rows and total of two different rows (mysql query)

  22. 22

    Fetch ids for two consecutive rows whose column has same value

  23. 23

    SQL query exclude a certain value from same column same table?

  24. 24

    sql: select rows which have the same column value in another column with a single query

  25. 25

    Query where table has multiple values for a column and the same value in another?

  26. 26

    Find row that has same value in one column over multiple rows while another column has different values

  27. 27

    SQL display rows with grouping rows with same one column value

  28. 28

    SQL Select item which has same value in all rows

  29. 29

    Return rows that of both table that has value in month column

HotTag

Archive