c# - SQL 查询中的期初余额和期末余额中的期末结果不正确?

标签 c# mysql sql sql-server sql-server-2008

我查找每日账户开户和结账余额,但当同一日期有多个记录时,它会给出错误的结果。这是我的查询

    SELECT cast(TransDate as date)
, SUM ([Total deposits]) As 'Total deposits'
, SUM ([Total withdrawals]) As 'Total withdrawals'
, SUM (ClosingBalance) AS ClosingBalance
FROM (
SELECT TransDate
, ISNULL (SUM (Ledger.Cr), 0) AS 'Total deposits'
, ISNULL (SUM (Ledger.Dr), 0) AS 'Total withdrawals'
, 0 AS ClosingBalance
FROM Ledger where Ledger.TransDate between '2014-02-14' and '2014-02-20'
GROUP BY Ledger.TransDate 
UNION ALL

SELECT TransDate
, 0 AS 'Total deposits'
, 0 AS 'Total withdrawals'
, ISNULL ((SELECT SUM (MT2.Cr) FROM Ledger MT2 WHERE MT2.TransDate  <= MT.TransDate), 0)
- ISNULL ((SELECT SUM (MT2.Dr) FROM Ledger MT2 WHERE cast(MT2.TransDate as date ) <= MT.TransDate), 0)

FROM Ledger MT where TransDate between '2014-02-14' and '2014-02-20'
GROUP BY Transdate
) AS X
GROUP BY cast(TRANSDATE as date)

查询结果

enter image description here

这是我的数据库文件脚本

USE [MDS]
GO
/****** Object:  Table [dbo].[Ledger]    Script Date: 03/26/2014 23:42:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Ledger](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [PrisonerID] [int] NULL,
    [TransDate] [datetime] NULL,
    [Dr] [money] NULL,
    [Cr] [money] NULL,
    [Partical] [varchar](50) NULL,
 CONSTRAINT [PK_Ledger] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Ledger] ON
INSERT [dbo].[Ledger] ([id], [PrisonerID], [TransDate], [Dr], [Cr], [Partical]) VALUES (1, 5, CAST(0x0000A2D200D9255B AS DateTime), 0.0000, 500.0000, N'payment recived')
INSERT [dbo].[Ledger] ([id], [PrisonerID], [TransDate], [Dr], [Cr], [Partical]) VALUES (2, 5, CAST(0x0000A2D300D9255B AS DateTime), 0.0000, 200.0000, N'withdraw')
INSERT [dbo].[Ledger] ([id], [PrisonerID], [TransDate], [Dr], [Cr], [Partical]) VALUES (3, 5, CAST(0x0000A2D400DBE47B AS DateTime), 20.0000, 0.0000, N'withdraw')
INSERT [dbo].[Ledger] ([id], [PrisonerID], [TransDate], [Dr], [Cr], [Partical]) VALUES (4, 5, CAST(0x0000A2D200000000 AS DateTime), 10.0000, 0.0000, N'withdraw')
INSERT [dbo].[Ledger] ([id], [PrisonerID], [TransDate], [Dr], [Cr], [Partical]) VALUES (5, 5, CAST(0x0000A2D700D9255B AS DateTime), 0.0000, 200.0000, N'payment revived')
INSERT [dbo].[Ledger] ([id], [PrisonerID], [TransDate], [Dr], [Cr], [Partical]) VALUES (6, 5, CAST(0x0000A2D20083D600 AS DateTime), 10.0000, 0.0000, N'withdraw')
SET IDENTITY_INSERT [dbo].[Ledger] OFF
/****** Object:  Default [DF_Ledger_Dr]    Script Date: 03/26/2014 23:42:04 ******/
ALTER TABLE [dbo].[Ledger] ADD  CONSTRAINT [DF_Ledger_Dr]  DEFAULT ((0.0000)) FOR [Dr]
GO
/****** Object:  Default [DF_Ledger_Cr]    Script Date: 03/26/2014 23:42:04 ******/
ALTER TABLE [dbo].[Ledger] ADD  CONSTRAINT [DF_Ledger_Cr]  DEFAULT ((0.0000)) FOR [Cr]
GO

最佳答案

有几个问题正在对你不利。首先,您对日期进行求和分组,但保留时间戳不变。然后,您的 UNION ALL 会导致这些内容在您的计算中重复。如果您在同一日期有其他存款,您也会看到这些数字也会膨胀。

您还可以将求和查询与一些相关子查询合并到一个 SELECT 中,如下所示:

SELECT dateadd(dd, datediff(dd, 0, a.TransDate), 0)
, ISNULL (SUM (a.Cr), 0) AS 'Total deposits'
, ISNULL (SUM (a.Dr), 0) AS 'Total withdrawals'
, ISNULL((SELECT SUM(Cr) from Ledger where dateadd(dd, datediff(dd, 0, TransDate), 0) <= dateadd(dd, datediff(dd, 0, a.TransDate), 0)), 0)
- ISNULL((SELECT SUM(Dr) from Ledger where dateadd(dd, datediff(dd, 0, TransDate), 0) <= dateadd(dd, datediff(dd, 0, a.TransDate), 0)), 0) as 'Closing Balance'
FROM Ledger a
where a.TransDate between '2014-02-14' and '2014-02-20'
GROUP BY dateadd(dd, datediff(dd, 0, a.TransDate), 0)

这是一个SQL Fiddle来演示。

此外,您可能还想根据 PrisonerID 进行分组,否则您会为每个囚犯得到相同的结果。

关于c# - SQL 查询中的期初余额和期末余额中的期末结果不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22670593/

相关文章:

c# - WPF 创建同级窗口并关闭当前窗口

c# - 我应该如何在移动约会时更新 iCal RRULE?

mysql - 更新 MySQL 中的 'ZERO' DATETIME 值

php - 两台MySQL服务器

sql - 如何将6列按数字顺序排序

java - 选择列并从其他选择中隐藏

c# - ASP.NET MVC 4.5 WebSocket 服务器在传输几条消息后失去连接

C# MVC。如何将新项目用作单独区域?

mysql - 为什么在添加列时所有行都获得空值?

mysql - 有没有更好的方法来编写这个特定的查询?