【发布时间】: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