this is a continuation of Azure SQL trial balance previous month. My query is essentially complete now, however i'm struggling to figure out how to add in missing months/periods. When there is an openingbalance (fiscalperiodtype 0) for an account, this should appear in results every month regardless of whether there is a transaction or not... how do I get a period (end of month) to appear every month for an account? My closing balance calculation also seems to be giving some incorrect results for some reason...
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
Query
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;
Cracked it, combination of CTE, cumulative SUM and LAG (window functions) should be enough for anyone to resolve this type of query.
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;
Related
I Have a table temp_customer. Schema and some data like below-
CREATE TABLE [dbo].[temp_customer](
[id] [int] IDENTITY(1,1) NOT NULL,
[company_id] [int] NOT NULL,
[created_by] [int] NULL,
[created_at] [datetime] NULL,
[code] [varchar](25) NOT NULL,
[name] [varchar](50) NULL,
)
SET IDENTITY_INSERT [dbo].[temp_customer] ON
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1714, 1, 1, CAST(N'2018-02-14 11:43:00.757' AS DateTime), N'STET-00001', N'Stephan Taylor')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1715, 1, 1, CAST(N'2018-02-14 11:43:01.007' AS DateTime), N'TAMJ-00001', N'Tamala Jones')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1716, 1, 2, CAST(N'2018-02-14 11:43:01.457' AS DateTime), N'TARB-00001', N'Taran Blauman')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1718, 1, 1, CAST(N'2019-01-29 00:00:00.000' AS DateTime), N'VERD-00001', N'Veronica Dave')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1719, 1, 1, CAST(N'2018-02-14 11:43:02.190' AS DateTime), N'WAYD-00001', N'Wayne Dotson')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1720, 1, 2, CAST(N'2018-03-12 07:40:34.107' AS DateTime), N'LARJ-00001', N'Larry Jacobs')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1721, 1, 2, CAST(N'2018-03-28 03:40:52.673' AS DateTime), N'ROBM-00002', N'Robert Matte')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1722, 1, 1, CAST(N'2018-03-28 05:09:53.093' AS DateTime), N'MART-00001', N'Mark Taylor')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1723, 1, 1, CAST(N'2018-03-28 05:39:19.237' AS DateTime), N'TAYT-00001', N'ALEX BRUZZI')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1724, 1, 3, CAST(N'2018-04-04 04:37:29.547' AS DateTime), N'STOCK', N'STOCK')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1725, 1, 3, CAST(N'2018-04-04 06:51:10.797' AS DateTime), N'AMAT-00001', N'Amanda Trezza')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1726, 1, 1, CAST(N'2018-04-04 06:55:04.720' AS DateTime), N'ADRG-00001', N'Adriana Grande')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1727, 1, 3, CAST(N'2018-04-05 00:00:00.000' AS DateTime), N'ISSJ-00002', N'Issac Johnes')
INSERT [dbo].[temp_customer] ([id], [company_id], [created_by], [created_at], [code], [name]) VALUES (1728, 1, 2, CAST(N'2019-01-18 00:00:00.000' AS DateTime), N'JACJ-00001', N'Jacob Jensen')
it looks like -
I would like to get output group on created_by with total_customer_created and week in which customer created as individual columns. eg in excel like -
i.e on basis of Week(starting from monday) , total customer created by 1,2,3..... etc in required date range.
Any helps appreciated in advance.
I have found one solution, it is working.
firstly a function to find weeks(with date range)
CREATE FUNCTION [dbo].[fGetWeeksList]
(
#StartDate DATETIME
,#EndDate DATETIME
)
RETURNS
TABLE
AS
RETURN
(
SELECT DATEADD(DAY,-(DATEPART(DW,DATEADD(WEEK, x.number, #StartDate))-2),DATEADD(WEEK, x.number, #StartDate)) as [StartDate]
,DATEADD(DAY,-(DATEPART(DW,DATEADD(WEEK, x.number + 1, #StartDate))-1) ,DATEADD(WEEK, x.number + 1, #StartDate)) AS [EndDate]
FROM master.dbo.spt_values x
WHERE x.type = 'P' AND x.number <= DATEDIFF(WEEK, #StartDate, DATEADD(WEEK,0,CAST(#EndDate AS DATE)))
)
And then use it in below query
select cast(cast(fGetWeeksList.startdate as date) as varchar(max)) + ' To '+cast(cast(fGetWeeksList.enddate as date) as varchar(max)) as week_range,
*
into #week_table from fGetWeeksList('01-01-2022','06-02-2022')
select users.user_cd ,
users.first_name ,
users.last_name,
count(customers.id) cust_count,
#week_table.week_range
into #cust_count
from users
inner join customers on
users.id = customers.created_by
inner join #week_table on
cast(customers.created_at as date) between #week_table.StartDate and #week_table.EndDate
group by
users.user_cd ,users.first_name ,users.last_name,
#week_table.week_range
DECLARE #columns NVARCHAR(MAX), #sql NVARCHAR(MAX);
SET #columns = N'';
SELECT #columns += N', ' + QUOTENAME(week_range)
FROM (SELECT distinct #week_table.week_range FROM #week_table
GROUP BY #week_table.week_range) AS x;
print #columns
SET #sql = N'SELECT user_cd,p.first_name,p.last_name, ' + STUFF(#columns, 1, 2, '') + '
FROM
(
SELECT p.week_range, p.user_cd,p.first_name,p.last_name, p.cust_count FROM dbo.#cust_count AS p
) AS j
PIVOT
(
SUM(cust_count) FOR week_range IN ('+ STUFF(REPLACE(#columns, ', p.[', ',['), 1, 1, '') + ')
) AS p;';
PRINT #sql;
EXEC sp_executesql #sql;
drop table #week_table
drop table #cust_count
CREATE TABLE Files (
Id INT PRIMARY KEY,
[Name] NVARCHAR(100) NOT NULL,
Size DECIMAL(18, 2) NOT NULL,
ParentId INT NULL FOREIGN KEY REFERENCES Files(Id)
)
SET IDENTITY_INSERT [dbo].[Files] ON
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (1, N'Trade.idk', CAST(2598.00 AS Decimal(18, 2)), 1, 1)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (2, N'menu.net', CAST(9238.31 AS Decimal(18, 2)), 2, 2)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (3, N'Administrate.soshy', CAST(1246.93 AS Decimal(18, 2)), 3, 3)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (4, N'Controller.php', CAST(7353.15 AS Decimal(18, 2)), 4, 4)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (5, N'Find.java', CAST(9957.86 AS Decimal(18, 2)), 5, 5)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (6, N'Controller.json', CAST(14034.87 AS Decimal(18, 2)), 3, 6)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (7, N'Operate.xix', CAST(7662.92 AS Decimal(18, 2)), 7, 7)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (8, N'file.sick', CAST(10548.35 AS Decimal(18, 2)), 8, 8)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (9, N'config.dd', CAST(8745.77 AS Decimal(18, 2)), 9, 9)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (10, N'Index.java', CAST(6121.35 AS Decimal(18, 2)), 10, 10)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (11, N'compile.ivory', CAST(1185.04 AS Decimal(18, 2)), 11, 1)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (12, N'Model.MD', CAST(4753.67 AS Decimal(18, 2)), 3, 12)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (13, N'Beat.html', CAST(907.30 AS Decimal(18, 2)), 13, 13)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (14, N'READ.img', CAST(2627.60 AS Decimal(18, 2)), 14, 7)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (15, N'Search.py', CAST(8831.43 AS Decimal(18, 2)), 15, 15)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (16, N'Controller.intro', CAST(27302.85 AS Decimal(18, 2)), 11, 1)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (17, N'Login.html', CAST(2863.23 AS Decimal(18, 2)), 16, 17)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (18, N'Administrate.go', CAST(24612.57 AS Decimal(18, 2)), 9, 18)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (19, N'READ.html', CAST(2396.47 AS Decimal(18, 2)), 8, 1)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (20, N'index.net', CAST(9261.71 AS Decimal(18, 2)), 20, 20)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (21, N'Index.class', CAST(4001.15 AS Decimal(18, 2)), 21, 21)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (22, N'config.json', CAST(6049.09 AS Decimal(18, 2)), 22, 22)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (23, N'pipeline.dd', CAST(18407.72 AS Decimal(18, 2)), NULL, 19)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (24, N'Accelerate.dd', CAST(23042.88 AS Decimal(18, 2)), 24, 19)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (25, N'Database.dd', CAST(14905.56 AS Decimal(18, 2)), NULL, 25)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (26, N'Login.db', CAST(8015.83 AS Decimal(18, 2)), NULL, 21)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (27, N'Beat.bat', CAST(21431.98 AS Decimal(18, 2)), 25, 12)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (28, N'Jason.txt', CAST(10317.54 AS Decimal(18, 2)), NULL, 28)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (29, N'Jason.exe', CAST(28209.18 AS Decimal(18, 2)), 8, 25)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (30, N'Accelerate.idk', CAST(5520.30 AS Decimal(18, 2)), 30, 1)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (31, N'file.txt', CAST(5514.02 AS Decimal(18, 2)), 27, 1)
GO
INSERT [dbo].[Files] ([Id], [Name], [Size], [ParentId], [CommitId]) VALUES (32, N'Music.jpg', CAST(917.75 AS Decimal(18, 2)), 1, 3)
GO
SET IDENTITY_INSERT [dbo].[Files] OFF
GO
I am trying to select all of the files which are NOT a parent to any other file. This was my try:
SELECT f.Id, f.[Name], CONCAT(f.Size, 'KB') AS Size
FROM Files f
WHERE f.Id NOT IN
(SELECT f1.ParentId
FROM Files f1
WHERE f1.ParentId IS NOT NULL)
ORDER BY f.Id, f.[Name], Size DESC
Now I am trying to use joins to solve the same task.
SELECT *
FROM Files f
JOIN Files f1 ON f.ParentId = f1.Id
I am confused. When we use INNER JOIN, we get only the records where f.ParentId is not null. Can you help me? Some explanation will also be appreciated. Thank you in advance!
You could try with NOT EXISTS
This returns all rows that don't match the specified set in the not exists expression.
SELECT *
FROM Files f1
WHERE NOT EXISTS (
SELECT 1
FROM Files f2
WHERE f2.ParentId = f1.Id
)
SQLFiddle
SELECT *
FROM Files AS f1
FULL OUTER JOIN Files as f2
ON f1.ParentId = f2.Id
WHERE f1.Id IS NULL
I have a requirement to Pass Input params value into a LIKE Operator for a column called BillOfMaterialsID in tabe BillOfMaterials.
If we want to hard-code the values we shall use OR operator like below,
SELECT *
FROM [AdventureWorks2017].[Production].[BillOfMaterials]
Where StartDate>='2010-05-26 00:00:00.000' and StartDate<'2010-07-08 00:00:00.000'
AND (BillOfMaterialsID LIKE '%' + '308' + '%' OR BillOfMaterialsID LIKE '%' + '335' + '%' OR BillOfMaterialsID LIKE '%' + '27' + '%')
But If the input values are dynamically changing based on the Input Filter value, then how shall we handle such criteria in SQL Server?
For Example I have 3 Input Parameters in a Report Application says Start Date Time, End Date Time and BillOfMaterialsID.
Start Date Time and End Date Time values will be selected in Calender Pick and for entering BillOfMaterialsID we have text box.
So what ever Codes we are entering with comma separation, all the values should individually applied in query condition and the SQL statement that should pass
from the application to database would be like below,
SELECT *
FROM [AdventureWorks2017].[Production].[BillOfMaterials]
Where StartDate>='2010-05-26 00:00:00.000' and StartDate<'2010-07-08 00:00:00.000'
AND (BillOfMaterialsID LIKE '%' + '308' + '%' OR BillOfMaterialsID LIKE '%' + '335' + '%' OR BillOfMaterialsID LIKE '%' + '27' + '%')
ELSE any ways is highly appreciated as I'm hugged with the logic.
CREATE TABLE [Production].[BillOfMaterials](
[BillOfMaterialsID] [int] IDENTITY(1,1) NOT NULL,
[ProductAssemblyID] [int] NULL,
[ComponentID] [int] NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NULL,
[UnitMeasureCode] [nchar](3) NOT NULL,
[BOMLevel] [smallint] NOT NULL,
[PerAssemblyQty] [decimal](8, 2) NOT NULL,
[ModifiedDate] [datetime] NOT NULL
)
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (893, NULL, 749, CAST(N'2010-05-26T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-05-12T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (271, NULL, 750, CAST(N'2010-03-04T00:00:00.000' AS DateTime), CAST(N'2010-05-03T00:00:00.000' AS DateTime), N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-05-03T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (34, NULL, 750, CAST(N'2010-05-04T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-04-20T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (830, NULL, 751, CAST(N'2010-05-26T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-05-12T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2074, NULL, 752, CAST(N'2010-07-08T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-06-24T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (1950, NULL, 753, CAST(N'2010-06-19T00:00:00.000' AS DateTime), CAST(N'2010-08-18T00:00:00.000' AS DateTime), N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-08-18T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (1761, NULL, 753, CAST(N'2010-08-19T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-08-05T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3088, NULL, 754, CAST(N'2010-12-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3351, NULL, 755, CAST(N'2010-12-23T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-09T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3246, NULL, 756, CAST(N'2010-12-23T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-09T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2760, NULL, 757, CAST(N'2010-09-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-09-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2395, NULL, 758, CAST(N'2010-08-05T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-07-22T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3087, NULL, 759, CAST(N'2010-12-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3350, NULL, 760, CAST(N'2010-12-23T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-09T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2822, NULL, 761, CAST(N'2010-09-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-09-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3245, NULL, 762, CAST(N'2010-12-23T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-09T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2759, NULL, 763, CAST(N'2010-09-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-09-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2394, NULL, 764, CAST(N'2010-08-05T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-07-22T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3341, NULL, 765, CAST(N'2010-12-23T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-09T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2815, NULL, 766, CAST(N'2010-09-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-09-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2449, NULL, 767, CAST(N'2010-08-05T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-07-22T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2899, NULL, 768, CAST(N'2010-09-15T00:00:00.000' AS DateTime), CAST(N'2010-11-14T00:00:00.000' AS DateTime), N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-11-14T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2738, NULL, 768, CAST(N'2010-11-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-11-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2363, NULL, 769, CAST(N'2010-08-05T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-07-22T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (1265, NULL, 770, CAST(N'2010-06-09T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-05-26T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (1195, NULL, 771, CAST(N'2010-06-09T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-05-26T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3017, NULL, 772, CAST(N'2010-12-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3281, NULL, 773, CAST(N'2010-12-23T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-09T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2783, NULL, 774, CAST(N'2010-09-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-09-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (5, NULL, 775, CAST(N'2010-03-04T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-02-18T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2737, NULL, 776, CAST(N'2010-09-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-09-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2362, NULL, 777, CAST(N'2010-08-05T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-07-22T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (1264, NULL, 778, CAST(N'2010-06-09T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-05-26T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (1642, NULL, 779, CAST(N'2010-06-19T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-06-05T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (393, NULL, 780, CAST(N'2010-03-18T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-03-04T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (52, NULL, 781, CAST(N'2010-03-04T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-02-18T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3217, NULL, 782, CAST(N'2010-12-23T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-09T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (808, NULL, 783, CAST(N'2010-05-26T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-05-12T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2049, NULL, 784, CAST(N'2010-07-08T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-06-24T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (4, NULL, 785, CAST(N'2010-03-04T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-02-18T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2736, NULL, 786, CAST(N'2010-09-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-09-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2361, NULL, 787, CAST(N'2010-08-05T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-07-22T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (1263, NULL, 788, CAST(N'2010-06-09T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-05-26T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3247, NULL, 789, CAST(N'2010-12-23T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-09T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2761, NULL, 790, CAST(N'2010-09-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-09-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2396, NULL, 791, CAST(N'2010-08-05T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-07-22T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (3089, NULL, 792, CAST(N'2010-12-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-12-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2739, NULL, 793, CAST(N'2010-09-15T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-09-01T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (2364, NULL, 794, CAST(N'2010-08-05T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-07-22T00:00:00.000' AS DateTime))
GO
INSERT [Production].[BillOfMaterials] ([BillOfMaterialsID], [ProductAssemblyID], [ComponentID], [StartDate], [EndDate], [UnitMeasureCode], [BOMLevel], [PerAssemblyQty], [ModifiedDate]) VALUES (1266, NULL, 795, CAST(N'2010-06-09T00:00:00.000' AS DateTime), NULL, N'EA ', 0, CAST(1.00 AS Decimal(8, 2)), CAST(N'2010-05-26T00:00:00.000' AS DateTime))
GO
Use a table variable to pass the parameters.
...
DECLARE #billofmaterialsids TABLE
(billofmaterialsid integer);
INSERT INTO #billofmaterialsids
(billofmaterialsid)
VALUES (<billofmaterialsid_1>),
...
(<billofmaterialsid_n>);
...
(<billofmaterialsid_1> through <billofmaterialsid_n> needs to be replaced with the desired values of course.)
Then, in the query, you can use EXISTS and a correlated subquery to check if there's a match with any of the passed values.
...
FROM billofmaterials bom
...
WHERE EXISTS (SELECT *
FROM #billofmaterialsids bomi
WHERE bom.billofmaterialsid LIKE '%' + bomi.billofmaterialsid + '%')
...
This is probalby a simple question, but I don't fully understand what is going wrong..with the method I am using..SoI've gone back to the original two tables that need to be used. So if you can suggest how to tackle this from the outset, would be greatly appreciated.
One table is cases, that holds individual case details, with dates that can be used to calculate, how long a hire went on for and the rate applied. It has a Source ID, that it has in common with a budget table. Every case has a Source ID. The other table is budget by Source ID for each calendar month. Need to compare the actual income from the dates, and rates in cases and add them and compare, the actual against budget.
The dates used to calculate the duration is the actual start date to the actual end date, but to further complicate matters, only the days of hire in the month of interest are required to be returned. So if a hire started earlier , than the month in question, just those dates are to be compared against the budget for that month.
The only other requirement is to set the rate dependent on the name of the company, either contract rate or hire daily rate. But i tried to keep it simple as possible.
It's all demo data...
USE [AutoHires]
GO
/****** Object: Table [dbo].[tblBudget] Script Date: 22/01/2018 09:57:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblBudget](
[Ref] [int] IDENTITY(1,1) NOT NULL,
[SourceID] [int] NOT NULL,
[BudgetRevenue] [money] NULL,
[BudgetHires] [int] NULL,
[BudgetSold] [int] NULL,
[BudgetInstructions] [int] NULL,
[SourceGroup] [int] NULL,
[MonthFrom] [datetime] NULL,
[MonthTo] [datetime] NULL,
[Name] [nvarchar](50) NULL,
CONSTRAINT [PK_Budget] PRIMARY KEY CLUSTERED
(
[Ref] 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 IDENTITY_INSERT [dbo].[tblBudget] ON
INSERT [dbo].[tblBudget] ([Ref], [SourceID], [BudgetRevenue], [BudgetHires], [BudgetSold], [BudgetInstructions], [SourceGroup], [MonthFrom], [MonthTo], [Name]) VALUES (1, 1, 300.0000, 12, 45, 80, NULL, CAST(N'2017-12-01 00:00:00.000' AS DateTime), CAST(N'2017-12-31 00:00:00.000' AS DateTime), N'SOURCEA')
INSERT [dbo].[tblBudget] ([Ref], [SourceID], [BudgetRevenue], [BudgetHires], [BudgetSold], [BudgetInstructions], [SourceGroup], [MonthFrom], [MonthTo], [Name]) VALUES (2, 2, 400.0000, 15, 50, 90, NULL, CAST(N'2017-12-01 00:00:00.000' AS DateTime), CAST(N'2017-12-31 00:00:00.000' AS DateTime), N'SOURCEB')
INSERT [dbo].[tblBudget] ([Ref], [SourceID], [BudgetRevenue], [BudgetHires], [BudgetSold], [BudgetInstructions], [SourceGroup], [MonthFrom], [MonthTo], [Name]) VALUES (3, 3, 700.0000, 4, 6, 50, NULL, CAST(N'2017-12-01 00:00:00.000' AS DateTime), CAST(N'2017-12-31 00:00:00.000' AS DateTime), N'SOURCEH')
INSERT [dbo].[tblBudget] ([Ref], [SourceID], [BudgetRevenue], [BudgetHires], [BudgetSold], [BudgetInstructions], [SourceGroup], [MonthFrom], [MonthTo], [Name]) VALUES (4, 4, 323.0000, 6, 18, 50, NULL, CAST(N'2017-12-01 00:00:00.000' AS DateTime), CAST(N'2017-12-31 00:00:00.000' AS DateTime), N'SOURCEG')
INSERT [dbo].[tblBudget] ([Ref], [SourceID], [BudgetRevenue], [BudgetHires], [BudgetSold], [BudgetInstructions], [SourceGroup], [MonthFrom], [MonthTo], [Name]) VALUES (5, 5, 567.0000, 6, 45, 56, NULL, CAST(N'2017-12-01 00:00:00.000' AS DateTime), CAST(N'2017-12-31 00:00:00.000' AS DateTime), N'SOURCEF')
INSERT [dbo].[tblBudget] ([Ref], [SourceID], [BudgetRevenue], [BudgetHires], [BudgetSold], [BudgetInstructions], [SourceGroup], [MonthFrom], [MonthTo], [Name]) VALUES (6, 6, 566.0000, 12, 56, 67, NULL, CAST(N'2017-12-01 00:00:00.000' AS DateTime), CAST(N'2017-12-31 00:00:00.000' AS DateTime), N'SOURCEE')
INSERT [dbo].[tblBudget] ([Ref], [SourceID], [BudgetRevenue], [BudgetHires], [BudgetSold], [BudgetInstructions], [SourceGroup], [MonthFrom], [MonthTo], [Name]) VALUES (7, 7, 566.0000, 23, 12, 44, NULL, CAST(N'2017-12-01 00:00:00.000' AS DateTime), CAST(N'2017-12-31 00:00:00.000' AS DateTime), N'SOURCED')
INSERT [dbo].[tblBudget] ([Ref], [SourceID], [BudgetRevenue], [BudgetHires], [BudgetSold], [BudgetInstructions], [SourceGroup], [MonthFrom], [MonthTo], [Name]) VALUES (8, 8, 668.0000, 3, 44, 23, NULL, CAST(N'2017-12-01 00:00:00.000' AS DateTime), CAST(N'2017-12-31 00:00:00.000' AS DateTime), N'SOURCEC')
SET IDENTITY_INSERT [dbo].[tblBudget] OFF
Second table of cases.
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblCase](
[PK_RefNo] [int] IDENTITY(1,1) NOT NULL,
[SourceID] [int] NOT NULL,
[DateInstructed] [datetime] NOT NULL,
[DateClosed] [datetime] NULL,
[Insurer_tpi] [varchar](50) NULL,
[HireDailyRate] [money] NULL,
[ActualStartDate] [datetime] NULL,
[ActualFinishDate] [datetime] NULL,
[HireContractRate] [money] NULL,
[DailyRate] [money] NULL,
[AdditionalCosts] [money] NULL,
CONSTRAINT [PK_tblCase] PRIMARY KEY CLUSTERED
(
[PK_RefNo] 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].[tblCase] ON
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (1, 1, CAST(N'2017-10-09 00:00:00.000' AS DateTime), CAST(N'2017-12-13 00:00:00.000' AS DateTime), N'avg', 2.0000, CAST(N'2017-11-14 00:00:00.000' AS DateTime), CAST(N'2017-12-19 00:00:00.000' AS DateTime), 2.0000, 1.0000, 3.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (2, 1, CAST(N'2017-01-10 00:00:00.000' AS DateTime), CAST(N'2018-01-16 00:00:00.000' AS DateTime), N'Bull', 4.0000, CAST(N'2017-01-03 00:00:00.000' AS DateTime), NULL, 4.0000, 3.0000, 1.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (3, 3, CAST(N'2017-12-13 00:00:00.000' AS DateTime), CAST(N'2018-01-01 00:00:00.000' AS DateTime), N'bit', 3.0000, CAST(N'2017-12-13 00:00:00.000' AS DateTime), CAST(N'2017-12-27 00:00:00.000' AS DateTime), 5.0000, 3.0000, 1.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (4, 4, CAST(N'2017-12-02 00:00:00.000' AS DateTime), CAST(N'2018-01-01 00:00:00.000' AS DateTime), N'Avast', 5.0000, CAST(N'2017-12-15 00:00:00.000' AS DateTime), CAST(N'2017-12-27 00:00:00.000' AS DateTime), 5.0000, 4.0000, 2.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (5, 5, CAST(N'2017-12-08 00:00:00.000' AS DateTime), NULL, N'Kasp', 6.0000, CAST(N'2017-12-22 00:00:00.000' AS DateTime), CAST(N'2018-01-26 00:00:00.000' AS DateTime), 6.0000, 4.0000, 2.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (6, 1, CAST(N'2017-12-06 00:00:00.000' AS DateTime), CAST(N'2018-01-01 00:00:00.000' AS DateTime), N'avg', 5.0000, CAST(N'2017-10-10 00:00:00.000' AS DateTime), CAST(N'2017-12-27 00:00:00.000' AS DateTime), 6.0000, 4.0000, 2.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (7, 1, CAST(N'2017-09-01 00:00:00.000' AS DateTime), CAST(N'2018-01-01 00:00:00.000' AS DateTime), N'avg', 5.0000, CAST(N'2017-11-01 00:00:00.000' AS DateTime), CAST(N'2017-12-08 00:00:00.000' AS DateTime), 5.0000, 4.0000, 1.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (8, 4, CAST(N'2017-12-05 00:00:00.000' AS DateTime), CAST(N'2018-01-11 00:00:00.000' AS DateTime), N'Avast', 6.0000, CAST(N'2017-11-30 00:00:00.000' AS DateTime), CAST(N'2018-01-03 00:00:00.000' AS DateTime), 4.0000, 3.0000, 1.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (9, 4, CAST(N'2017-12-05 00:00:00.000' AS DateTime), CAST(N'2018-01-10 00:00:00.000' AS DateTime), N'Panda', 6.0000, CAST(N'2017-12-04 00:00:00.000' AS DateTime), CAST(N'2017-12-30 00:00:00.000' AS DateTime), 6.0000, 4.0000, 2.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (10, 5, CAST(N'2017-01-10 00:00:00.000' AS DateTime), CAST(N'2018-01-01 00:00:00.000' AS DateTime), N'Avast', 7.0000, CAST(N'2017-12-13 00:00:00.000' AS DateTime), CAST(N'2018-01-24 00:00:00.000' AS DateTime), 6.0000, 4.0000, 1.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (11, 8, CAST(N'2017-05-17 00:00:00.000' AS DateTime), CAST(N'2018-09-07 00:00:00.000' AS DateTime), N'Bull', 3.0000, CAST(N'2017-12-04 00:00:00.000' AS DateTime), CAST(N'2017-12-26 00:00:00.000' AS DateTime), 5.0000, 3.0000, 2.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (12, 2, CAST(N'2017-01-16 00:00:00.000' AS DateTime), CAST(N'2017-12-20 00:00:00.000' AS DateTime), N'bit', 5.0000, CAST(N'2017-12-16 00:00:00.000' AS DateTime), CAST(N'2017-12-20 00:00:00.000' AS DateTime), 6.0000, 5.0000, 2.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (13, 8, CAST(N'2017-12-05 00:00:00.000' AS DateTime), CAST(N'2017-12-20 00:00:00.000' AS DateTime), N'Avast', 6.0000, CAST(N'2001-03-01 00:00:00.000' AS DateTime), CAST(N'2017-12-27 00:00:00.000' AS DateTime), 6.0000, 1.0000, 2.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (14, 8, CAST(N'2017-12-12 00:00:00.000' AS DateTime), CAST(N'2017-12-19 00:00:00.000' AS DateTime), N'Kasp', 5.0000, CAST(N'2017-11-30 00:00:00.000' AS DateTime), CAST(N'2018-01-02 00:00:00.000' AS DateTime), 6.0000, 3.0000, 1.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (15, 8, CAST(N'2017-12-04 00:00:00.000' AS DateTime), CAST(N'2017-12-28 00:00:00.000' AS DateTime), N'Panda', 6.0000, CAST(N'2017-11-30 00:00:00.000' AS DateTime), CAST(N'2018-01-03 00:00:00.000' AS DateTime), 5.0000, 3.0000, 2.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (16, 2, CAST(N'2017-12-05 00:00:00.000' AS DateTime), CAST(N'2017-12-20 00:00:00.000' AS DateTime), N'Avast', 6.0000, CAST(N'2017-12-01 00:00:00.000' AS DateTime), CAST(N'2017-12-19 00:00:00.000' AS DateTime), 5.0000, 1.0000, 2.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (17, 6, CAST(N'2017-12-05 00:00:00.000' AS DateTime), CAST(N'2018-01-10 00:00:00.000' AS DateTime), N'Avast', 6.0000, CAST(N'2017-11-30 00:00:00.000' AS DateTime), CAST(N'2017-12-30 00:00:00.000' AS DateTime), 5.0000, 1.0000, 3.0000)
INSERT [dbo].[tblCase] ([PK_RefNo], [SourceID], [DateInstructed], [DateClosed], [Insurer_tpi], [HireDailyRate], [ActualStartDate], [ActualFinishDate], [HireContractRate], [DailyRate], [AdditionalCosts]) VALUES (18, 5, CAST(N'2017-12-05 00:00:00.000' AS DateTime), CAST(N'2018-01-10 00:00:00.000' AS DateTime), N'Avast', 6.0000, CAST(N'2017-11-30 00:00:00.000' AS DateTime), CAST(N'2018-01-03 00:00:00.000' AS DateTime), 6.0000, 3.0000, 1.0000)
SET IDENTITY_INSERT [dbo].[tblCase] OFF
If hope you understand the question.
Maybe this
select * from tblbudget
left join
(
select
[SourceID],
hirevalue = sum((datediff(d,
(case when actualstartdate < '2017-12-01' then '2017-12-01'
else actualstartdate
end),
(case when actualfinishdate > '2017-12-31' or actualfinishdate is null then '2017-12-31'
else actualfinishdate
end)
) + 1) * (case when Insurer_tpi = 'avg' then hiredailyrate else dailyrate end) )
from tblcase
where (actualstartdate >= '2017-12-01' and actualstartdate <='2017-12-31')
or
(actualfinishdate >= '2017-12-01' and actualfinishdate <='2017-12-31')
or
(actualstartdate < '2017-12-01' and actualfinishdate > '2017-12-31')
or
(actualstartdate < '2017-12-01' and actualfinishdate is null)
group by sourceid
) cases on cases.sourceid = tblbudget.Sourceid
Ref SourceID BudgetRevenue BudgetHires BudgetSold BudgetInstructions SourceGroup MonthFrom MonthTo Name SourceID hirevalue
----------- ----------- --------------------- ----------- ----------- ------------------ ----------- ----------------------- ----------------------- -------------------------------------------------- ----------- ---------------------
1 1 300.00 12 45 80 NULL 2017-12-01 00:00:00.000 2017-12-31 00:00:00.000 SOURCEA 1 306.00
2 2 400.00 15 50 90 NULL 2017-12-01 00:00:00.000 2017-12-31 00:00:00.000 SOURCEB 2 44.00
3 3 700.00 4 6 50 NULL 2017-12-01 00:00:00.000 2017-12-31 00:00:00.000 SOURCEH 3 45.00
4 4 323.00 6 18 50 NULL 2017-12-01 00:00:00.000 2017-12-31 00:00:00.000 SOURCEG 4 253.00
5 5 567.00 6 45 56 NULL 2017-12-01 00:00:00.000 2017-12-31 00:00:00.000 SOURCEF 5 209.00
6 6 566.00 12 56 67 NULL 2017-12-01 00:00:00.000 2017-12-31 00:00:00.000 SOURCEE 6 30.00
7 7 566.00 23 12 44 NULL 2017-12-01 00:00:00.000 2017-12-31 00:00:00.000 SOURCED NULL NULL
8 8 668.00 3 44 23 NULL 2017-12-01 00:00:00.000 2017-12-31 00:00:00.000 SOURCEC 8 282.00
(8 row(s) affected)
There are Multiple delays within ONE activity. Delays can be placed at the Boundaries and many can exists inside one Long activity. We need to give preference to DELAY records, thus we should disregard any activity records which appear inside a Delay record
I have two sets of records in one table namely ACTIVITY and DELAY for a particular machine in a particular shiftdate and shift. During some shiftdates and shifts OVERLAP occurs between Acitivty and Delay .The DELAY records occurring between ACTIVITY records should be given the priority and ACTIVITY records should split accordingly around DELAY records thus removing the OVERLAP between Activity and Delays
What approach should I use to break up Activity records based on delay and give priorities to DELAY over activity records.
The code for creating table is as follows
CREATE TABLE [dbo].[Activity_Delays](
[Type] [nvarchar](15) NOT NULL,
[Shiftdate] [datetime] NOT NULL,
[Shift#] [int] NOT NULL,
[MachineID] [nvarchar](10) NOT NULL,
[StartTime] [datetime] NOT NULL,
[EndTime] [datetime] NOT NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13B00000000 AS DateTime), 1, N'780', CAST(0x0000A13900735B40 AS DateTime), CAST(0x0000A1390083D600 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13B00000000 AS DateTime), 1, N'780', CAST(0x0000A1390083D600 AS DateTime), CAST(0x0000A139009450C0 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A13900761A60 AS DateTime), CAST(0x0000A1390078D980 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A139007B98A0 AS DateTime), CAST(0x0000A139007FB750 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A13900815D30 AS DateTime), CAST(0x0000A1390084A8F0 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A1390087F4B0 AS DateTime), CAST(0x0000A139008C1360 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A0071B560 AS DateTime), CAST(0x0000A13A00761A60 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A007B98A0 AS DateTime), CAST(0x0000A13A007FB750 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A0087F4B0 AS DateTime), CAST(0x0000A13A008C1360 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A008D72F0 AS DateTime), CAST(0x0000A13A008FA570 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A13900761A60 AS DateTime), CAST(0x0000A1390078D980 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A139007B98A0 AS DateTime), CAST(0x0000A139007FB750 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A13900815D30 AS DateTime), CAST(0x0000A1390084A8F0 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A1390087F4B0 AS DateTime), CAST(0x0000A139008C1360 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A0071B560 AS DateTime), CAST(0x0000A13A00761A60 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A007B98A0 AS DateTime), CAST(0x0000A13A007FB750 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A0087F4B0 AS DateTime), CAST(0x0000A13A008C1360 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A008D72F0 AS DateTime), CAST(0x0000A13A008FA570 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A13900735B40 AS DateTime), CAST(0x0000A1390083D600 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A1390083D600 AS DateTime), CAST(0x0000A139008ED280 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A00735B40 AS DateTime), CAST(0x0000A13A0083D600 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A0083D600 AS DateTime), CAST(0x0000A13A008ED280 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A13900735B40 AS DateTime), CAST(0x0000A1390083D600 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A1390083D600 AS DateTime), CAST(0x0000A139008ED280 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A00735B40 AS DateTime), CAST(0x0000A13A0083D600 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A0083D600 AS DateTime), CAST(0x0000A13A008ED280 AS DateTime))
Here is the query for your example output:
select *, Activity_Delays.StartTime SortStartTime
from Activity_Delays
where
Type = 'Delay'
union all
select distinct Activities.*, Delays.StartTime SortStartTime
from
Activity_Delays Delays
inner join Activity_Delays Activities on Activities.ShiftDate = Delays.ShiftDate and
Activities.MachineID = Delays.MachineID and
((Delays.EndTime between Activities.StartTime and Activities.EndTime) or
(Delays.StartTime between Activities.StartTime and Activities.EndTime))
where
Activities.Type = 'Activity' and
Delays.Type = 'Delay'
order by MachineID, ShiftDate, SortStartTime, 1 desc
Also here is an working output