【问题标题】:Get 'balanced' column value as 'Y' if both rows has same amt and 'N' if not in SQL query如果两行具有相同的 amt,则获取“平衡”列值为“Y”,如果不在 SQL 查询中,则获取“N”
【发布时间】:2018-05-04 21:35:10
【问题描述】:

我有多个针对相同 id 的 CR 记录和 DR 记录, 如果 CR 与 DR 匹配,我需要检查 DR 金额的总和是否与同一 ID 的 CR 金额的总和匹配,然后将 Balanced 列显示为“Y”否则为“N”

如果贷方和借方金额列匹配,则要求结果:平衡”列值“Y”,如果不匹配,则为“N”

注意:需要单个查询:

如上图所示,抓取红色突出显示的结果是必需的。

脚本如下:

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')

【问题讨论】:

  • 请显示您为尝试解决此问题而编写的查询。
  • 这看起来更像是一个项目简介而不是一个问题。 Stackoverflow 是一个问答网站,而不是免费的代码编写服务。如果您尝试自己解决问题,您会得到更积极的回应,如果遇到问题,您会提出更具体的问题。
  • 是的,我尝试过但未能使用逻辑在这种情况下究竟做了什么,为了多次忽略编辑问题,我发布了我正在使用的 sql db 脚本。

标签: sql sql-server tsql


【解决方案1】:

如果是2012+,这是使用窗口函数sum() over的小事

示例

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

退货

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

【讨论】:

  • 感谢它的工作。你能对上面的代码添加解释吗?
  • @Tiger 窗口函数是无价的。值得您花时间熟悉它们
【解决方案2】:

我使用 in-query 来获取 Balanced 列。我按 ID 分组,然后得到 Amt 的总和。如果类型是 CR,那么它是负数 (-1*Amt),如果 DR 是正数。然后我查询了表 bankData 并将其分配给它自己的 ID (t1.ID=t.ID)。如果总和为零,那么它是平衡的='Y',否则它不是 ('N')。

select [ID], [Name], [Amt], [Type], [TransId], [CustId],
(select case when sum(case when [Type]='DR' then [Amt] else -1*[Amt] end)=0 
   then 'Y' 
   else 'N' end 
from [dbo].[BankData] as t1
where t1.[ID] = t.[ID]
group by t1.[ID]) as Balanced
from [dbo].[BankData] as t
order by t.[ID], t.[TransId]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 2022-01-20
    • 2022-10-14
    • 2013-10-04
    • 2017-11-24
    相关资源
    最近更新 更多