【问题标题】:How to get opening balance for each month如何获得每个月的期初余额
【发布时间】:2022-01-22 00:17:31
【问题描述】:

这是Azure SQL trial balance previous month 的延续。我的查询现在基本上完成了,但是我正在努力弄清楚如何添加缺失的月份/期间。当一个账户有期初余额(财政周期类型 0)时,无论是否有交易,这都应该出现在每个月的结果中......我如何让一个账户每个月出现一个期间(月末) ?由于某种原因,我的期末余额计算似乎也给出了一些不正确的结果......

CREATE TABLE [dbo].[TrialBalanceTest](
    [Company] [int] NOT NULL,
    [LedgerAccount] [int] NULL,
    [FiscalPeriodType] [int] NOT NULL,
    [AccountingDate] [datetime] NOT NULL,
    [AccountingDebitAmount] [numeric](32, 6) NOT NULL,
    [AccountingCreditAmount] [numeric](32, 6) NOT NULL
) ON [PRIMARY]
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223423, 1, CAST(N'2017-09-30T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(34240.610000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223424, 1, CAST(N'2017-01-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(7.570000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223424, 1, CAST(N'2017-06-30T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(3.490000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-01-31T00:00:00.000' AS DateTime), CAST(18740.040000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-05-31T00:00:00.000' AS DateTime), CAST(23546.990000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-09-30T00:00:00.000' AS DateTime), CAST(210643.780000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 248123, 1, CAST(N'2017-01-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(3144.750000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-01-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(38.620000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-03-31T00:00:00.000' AS DateTime), CAST(36646.380000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-05-31T00:00:00.000' AS DateTime), CAST(15.330000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-07-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(791.160000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-08-31T00:00:00.000' AS DateTime), CAST(625.880000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-09-30T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(22184.370000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-11-30T00:00:00.000' AS DateTime), CAST(895.730000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-12-31T00:00:00.000' AS DateTime), CAST(26.620000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-02-28T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(12.240000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-05-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(15.330000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-07-31T00:00:00.000' AS DateTime), CAST(791.160000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-09-30T00:00:00.000' AS DateTime), CAST(15374.610000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223423, 1, CAST(N'2017-06-30T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(54643.560000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223424, 0, CAST(N'2017-01-01T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(421.210000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223424, 1, CAST(N'2017-04-30T00:00:00.000' AS DateTime), CAST(457.660000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 0, CAST(N'2017-01-01T00:00:00.000' AS DateTime), CAST(711715.490000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-04-30T00:00:00.000' AS DateTime), CAST(16111.980000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-08-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(103438.450000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-12-31T00:00:00.000' AS DateTime), CAST(8855.750000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-01-31T00:00:00.000' AS DateTime), CAST(38.620000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-03-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(36646.380000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-05-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(15.330000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-07-31T00:00:00.000' AS DateTime), CAST(791.160000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-08-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(625.890000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-09-30T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(625.900000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-11-30T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(895.730000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-12-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(26.620000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-01-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(12.240000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-04-30T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(10.780000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-06-30T00:00:00.000' AS DateTime), CAST(22227.140000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-09-30T00:00:00.000' AS DateTime), CAST(13563.780000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223423, 1, CAST(N'2017-03-31T00:00:00.000' AS DateTime), CAST(32788.870000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223423, 1, CAST(N'2017-12-31T00:00:00.000' AS DateTime), CAST(18283.750000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223424, 1, CAST(N'2017-03-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(457.660000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223424, 1, CAST(N'2017-08-31T00:00:00.000' AS DateTime), CAST(3.540000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-03-31T00:00:00.000' AS DateTime), CAST(9089.990000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-07-31T00:00:00.000' AS DateTime), CAST(28277.890000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-11-30T00:00:00.000' AS DateTime), CAST(17545.800000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-01-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(36912.220000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-02-28T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(307.580000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-04-30T00:00:00.000' AS DateTime), CAST(10.780000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-06-30T00:00:00.000' AS DateTime), CAST(7416.570000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-08-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(129.320000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-09-30T00:00:00.000' AS DateTime), CAST(625.910000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-10-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(21272.100000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-12-31T00:00:00.000' AS DateTime), CAST(350.650000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-01-31T00:00:00.000' AS DateTime), CAST(12.240000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-03-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(3.400000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-06-30T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(25194.070000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-09-30T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(15374.610000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223423, 0, CAST(N'2017-01-01T00:00:00.000' AS DateTime), CAST(101385.640000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223423, 1, CAST(N'2017-10-31T00:00:00.000' AS DateTime), CAST(80135.340000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223424, 1, CAST(N'2017-02-28T00:00:00.000' AS DateTime), CAST(428.780000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223424, 1, CAST(N'2017-07-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(0.050000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-02-28T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(14951.440000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-06-30T00:00:00.000' AS DateTime), CAST(12437.260000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 223425, 1, CAST(N'2017-10-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(120164.230000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-01-31T00:00:00.000' AS DateTime), CAST(36912.220000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-02-28T00:00:00.000' AS DateTime), CAST(307.580000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-04-30T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(10.780000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-06-30T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(7416.570000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-08-31T00:00:00.000' AS DateTime), CAST(129.320000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-09-30T00:00:00.000' AS DateTime), CAST(22184.370000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-10-31T00:00:00.000' AS DateTime), CAST(21272.100000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-12-31T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(350.650000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 0, CAST(N'2017-01-01T00:00:00.000' AS DateTime), CAST(0.000000 AS Numeric(32, 6)), CAST(464.760000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-02-28T00:00:00.000' AS DateTime), CAST(9.760000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-06-30T00:00:00.000' AS DateTime), CAST(25194.070000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO
INSERT [dbo].[TrialBalanceTest] ([Company], [LedgerAccount], [FiscalPeriodType], [AccountingDate], [AccountingDebitAmount], [AccountingCreditAmount]) VALUES (1234, 263123, 1, CAST(N'2017-08-31T00:00:00.000' AS DateTime), CAST(129.320000 AS Numeric(32, 6)), CAST(0.000000 AS Numeric(32, 6)))
GO

查询

WITH Months
  AS (SELECT MONTH AS [Month]
        FROM (   VALUES (1),
                        (2),
                        (3),
                        (4),
                        (5),
                        (6),
                        (7),
                        (8),
                        (9),
                        (10),
                        (11),
                        (12)) x (MONTH) ),
     Base
  AS (SELECT      Company,
                  LedgerAccount,
                  M.[Month],
                  CASE
                       WHEN GA.FiscalPeriodType = 0 THEN EOMONTH(AccountingDate)
                       ELSE GA.AccountingDate END AS AccountingDate,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 0 THEN GA.AccountingDebitAmount - GA.AccountingCreditAmount
                           ELSE 0 END) AS AccountingOpeningBalance,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 1 THEN GA.AccountingDebitAmount
                           ELSE 0 END) AS AccountingDebitAmount,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 1 THEN GA.AccountingCreditAmount
                           ELSE 0 END) AS AccountingCreditAmount,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 1 THEN GA.AccountingDebitAmount - GA.AccountingCreditAmount
                           ELSE 0 END) AS AccountingNetChangeAmount
        FROM      Months AS M
        LEFT JOIN dbo.TrialBalanceTest AS GA
          ON MONTH(GA.AccountingDate) = M.[Month]
       GROUP BY CASE
                     WHEN GA.FiscalPeriodType = 0 THEN EOMONTH(AccountingDate)
                     ELSE GA.AccountingDate END,
                GA.Company,
                GA.LedgerAccount,
                M.Month),
     Base1
  AS (SELECT Base.Company,
             Base.LedgerAccount,
             Base.Month,
             Base.AccountingDate,
             SUM(Base.AccountingOpeningBalance) OVER (PARTITION BY Base.LedgerAccount) AS AccountingOpeningBalance,
             Base.AccountingDebitAmount,
             Base.AccountingCreditAmount,
             Base.AccountingNetChangeAmount,
             SUM(Base.AccountingOpeningBalance) OVER (PARTITION BY Base.LedgerAccount) + Base.AccountingNetChangeAmount AS AccountingClosingBalance
        FROM Base),
     Final
  AS (SELECT Base1.Company,
             Base1.LedgerAccount,
             Base1.Month,
             Base1.AccountingDate,
             LAG(Base1.AccountingClosingBalance, 1, Base1.AccountingOpeningBalance) OVER (PARTITION BY LedgerAccount ORDER BY Base1.Month) AS AccountingOpeningBalance,
             Base1.AccountingDebitAmount,
             Base1.AccountingCreditAmount,
             Base1.AccountingNetChangeAmount,
             Base1.AccountingClosingBalance
        FROM Base1)
SELECT *
  FROM Final
 ORDER BY LedgerAccount,
          AccountingDate;

【问题讨论】:

  • 旁注:我真的建议养成使用单个INSERT INTO 并在VALUES 表结构中定义多行的习惯。许多单行的INSERT 语句明显比一个多行的INSERT 慢。
  • 是的,我真的在为堆栈溢出字符限制而苦苦挣扎,甚至无法获得一整月的时间来重新发布随机百分比数据选择...
  • 那么使用单个INSERT 将极大地帮助减少字符计数,@jhowe,因为您将使用about half 字符。
  • 您如何使用 SSMS 来执行此操作?我使用生成脚本表/数据...
  • 我采用了您现有的语句,并进行了一些查找和替换,然后只是在行的开头转储了空格并在需要的地方添加了逗号。带我about 30 seconds in ADS。 :)

标签: sql-server azure-sql-database


【解决方案1】:

破解它,CTE、累积 SUM 和 LAG(窗口函数)的组合应该足以让任何人解决此类查询。

DECLARE @Company NVARCHAR(20) = N'2061'
DECLARE @Year INT = 2017;

WITH AccountingPeriods --Recursive CTE gets all periods for year
  AS (SELECT MIN(DATEFROMPARTS(@Year, 01, 31)) AS AccountingPeriod
        FROM GeneralAccountEntry
      UNION ALL
      SELECT EOMONTH(AccountingPeriod, 1) AS AccountingPeriod
        FROM AccountingPeriods
       WHERE AccountingPeriods.AccountingPeriod < DATEFROMPARTS(@Year, 12, 31)),
     LedgerDimensionPeriod --Unique combinations of Ledger, Dimension, Period so that there are no empty periods for accounts
  AS (SELECT       DISTINCT GA.[Ledger],
                            GA.Company,
                            GA.PostingLayer,
                            GA.PostingLayerName,
                            AP.AccountingPeriod,
                            GA.LedgerDimension,
                            GA.AccountDisplayValue,
                            GA.TransactionCurrency,
                            GA.AccountingCurrency
        FROM       GeneralAccountEntry AS GA
       CROSS APPLY AccountingPeriods AS AP -- Apply every period to every account
       WHERE       GA.Company        = @Company
         AND       YEAR(GA.AccountingDate) = @Year
         AND       GA.FiscalPeriodType IN ( 0, 1 )),
     Base --Base calculations
  AS (SELECT      LDP.[Ledger],
                  LDP.Company,
                  LDP.PostingLayer,
                  LDP.PostingLayerName,
                  LDP.AccountingPeriod,
                  LDP.LedgerDimension,
                  LDP.AccountDisplayValue,
                  LDP.TransactionCurrency,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 0 THEN
                               GA.TransactionCurrencyDebitAmount - GA.TransactionCurrencyCreditAmount
                           ELSE 0 END) AS InitialTransactionOpeningBalance,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 1 THEN GA.TransactionCurrencyDebitAmount
                           ELSE 0 END) AS TransactionDebitAmount,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 1 THEN GA.TransactionCurrencyCreditAmount
                           ELSE 0 END) AS TransactionCreditAmount,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 1 THEN
                               GA.TransactionCurrencyDebitAmount - GA.TransactionCurrencyCreditAmount
                           ELSE 0 END) AS TransactionNetChange,
                  LDP.AccountingCurrency,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 0 THEN GA.AccountingDebitAmount - GA.AccountingCreditAmount
                           ELSE 0 END) AS InitialAccountingOpeningBalance,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 1 THEN GA.AccountingDebitAmount
                           ELSE 0 END) AS AccountingDebitAmount,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 1 THEN GA.AccountingCreditAmount
                           ELSE 0 END) AS AccountingCreditAmount,
                  SUM(CASE
                           WHEN GA.FiscalPeriodType = 1 THEN GA.AccountingDebitAmount - GA.AccountingCreditAmount
                           ELSE 0 END) AS AccountingNetChange
        FROM      LedgerDimensionPeriod AS LDP
        LEFT JOIN GeneralAccountEntry AS GA
          ON LDP.[Ledger]         = GA.[Ledger]
         AND LDP.PostingLayer     = GA.PostingLayer
         AND LDP.LedgerDimension  = GA.LedgerDimension
         AND LDP.AccountingPeriod = CASE --Group Jan transactions into one date period
                                         WHEN GA.FiscalPeriodType = 0 THEN EOMONTH(GA.AccountingDate)
                                         ELSE GA.AccountingDate END
       GROUP BY LDP.[Ledger],
                LDP.Company,
                LDP.PostingLayer,
                LDP.PostingLayerName,
                LDP.AccountingPeriod,
                LDP.LedgerDimension,
                LDP.AccountDisplayValue,
                LDP.TransactionCurrency,
                LDP.AccountingCurrency),
     ClosingBalance
  AS (SELECT B.[Ledger],
             B.Company,
             B.PostingLayer,
             B.PostingLayerName,
             B.AccountingPeriod,
             B.LedgerDimension,
             B.AccountDisplayValue,
             B.TransactionCurrency,
             B.InitialTransactionOpeningBalance,
             B.TransactionDebitAmount,
             B.TransactionCreditAmount,
             B.TransactionNetChange,
             SUM(B.InitialTransactionOpeningBalance + B.TransactionNetChange) OVER (PARTITION BY [Ledger], 
                                                                                                 B.LedgerDimension 
                                                                                      ORDER BY B.AccountingPeriod) AS TransactionClosingBalance,
             B.AccountingCurrency,
             B.InitialAccountingOpeningBalance,
             B.AccountingDebitAmount,
             B.AccountingCreditAmount,
             B.AccountingNetChange,
             SUM(B.InitialAccountingOpeningBalance + B.AccountingNetChange) OVER (PARTITION BY B.[Ledger],
                                                                                               B.LedgerDimension
                                                                                      ORDER BY B.AccountingPeriod) AS AccountingClosingBalance
        FROM Base AS B),
     OpeningBalance
  AS (SELECT CB.[Ledger],
             CB.Company,
             CB.PostingLayer,
             CB.PostingLayerName,
             CB.AccountingPeriod,
             CB.LedgerDimension,
             CB.AccountDisplayValue,
             CB.TransactionCurrency,
             LAG(CB.TransactionClosingBalance, 1, CB.InitialTransactionOpeningBalance) OVER (PARTITION BY CB.[Ledger],
                                                                                                          CB.LedgerDimension
                                                                                                 ORDER BY CB.AccountingPeriod) AS TransactionOpeningBalance, --LAG retrieves previous records by partition
             CB.TransactionDebitAmount,
             CB.TransactionCreditAmount,
             CB.TransactionNetChange,
             CB.TransactionClosingBalance,
             CB.AccountingCurrency,
             LAG(CB.AccountingClosingBalance, 1, CB.InitialAccountingOpeningBalance) OVER (PARTITION BY CB.[Ledger],
                                                                                                        CB.LedgerDimension
                                                                                                 ORDER BY CB.AccountingPeriod) AS AccountingOpeningBalance, --LAG retrieves previous records by partition
             CB.AccountingDebitAmount,
             CB.AccountingCreditAmount,
             CB.AccountingNetChange,
             CB.AccountingClosingBalance
        FROM ClosingBalance AS CB),
     Final
  AS (SELECT *
        FROM OpeningBalance AS OB)
SELECT *
  FROM Final AS F;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多