September 4, 2015 at 8:45 am
I have a dataset that contains all the pay records for the current year. I'd like to add a year-to-date and a month-to-date and hopefully not have to sum them separately and then join the results. Here is my data:
CREATE TABLE [dbo].[_Test](
[Id] [int] IDENTITY(1,1) NOT NULL,
[EmployeeID] [nvarchar](30) NULL,
[CheckDate] [date] NULL,
[Units] [decimal](19, 5) NULL,
[Rate] [decimal](19, 5) NULL,
[Amount] [decimal](19, 5) NULL,
[TaxAmount] [decimal](19, 5) NULL,
[DeductAmount] [decimal](19, 5) NULL,
[BenefitAmount] [decimal](19, 5) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[_Test] ON
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (1, N'1', CAST(N'2015-01-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (2, N'2', CAST(N'2015-01-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (3, N'1', CAST(N'2015-02-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (4, N'2', CAST(N'2015-02-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (5, N'1', CAST(N'2015-03-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (6, N'2', CAST(N'2015-03-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (7, N'1', CAST(N'2015-04-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (8, N'2', CAST(N'2015-04-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (9, N'1', CAST(N'2015-05-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (10, N'2', CAST(N'2015-05-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (11, N'1', CAST(N'2015-06-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (12, N'2', CAST(N'2015-06-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (13, N'1', CAST(N'2015-07-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (14, N'2', CAST(N'2015-07-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (15, N'1', CAST(N'2015-08-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (16, N'2', CAST(N'2015-08-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (17, N'1', CAST(N'2015-09-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (18, N'2', CAST(N'2015-09-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (19, N'1', CAST(N'2015-01-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (20, N'2', CAST(N'2015-01-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (21, N'1', CAST(N'2015-02-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (22, N'2', CAST(N'2015-02-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (23, N'1', CAST(N'2015-03-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (24, N'2', CAST(N'2015-03-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (25, N'1', CAST(N'2015-04-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (26, N'2', CAST(N'2015-04-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (27, N'1', CAST(N'2015-05-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (28, N'2', CAST(N'2015-05-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (29, N'1', CAST(N'2015-06-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (30, N'2', CAST(N'2015-06-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (31, N'1', CAST(N'2015-07-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (32, N'1', CAST(N'2015-08-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
GO
INSERT [dbo].[_Test] ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (33, N'2', CAST(N'2015-08-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
GO
SET IDENTITY_INSERT [dbo].[_Test] OFF
GO
I want to return a paycheck's data along with the month to date and year to date values included given a single date. I'm thinking something like this might work, but i'm having trouble setting up my window.
select employeeid, CheckDate, Units
sum(Units) over (Partition by employeeid, CheckDate
ORDER BY CheckDate
ROWS between unbounded preceding and current row) [MTD Units],
sum(Units) over (Partition by EmployeeId, CheckDate
ORDER BY CheckDate
ROWS between unbounded preceding and current row) [YTD Units],
, Rate, Amount,
sum(Amount) over (Partition by employeeid, CheckDate
ORDER BY CheckDate
ROWS between unbounded preceding and current row) [MTD Amount],
sum(Amount) over (Partition by EmployeeId, CheckDate
ORDER BY CheckDate
ROWS between unbounded preceding and current row) [YTD Amount],
from _Test
where CheckDate = '6/15/15'
The reason I was thinking using the Over would work, because if i take off my WHERE clause and do the following, I do get a sum of all the items on every line.
select employeeid, CheckDate, Units
, Rate, Amount
,sum(Amount) over (Partition byEmployeeId) [All]
from _Test
empidCheckDateUnitsRateAmountAll
12015-01-0140.0010.00400.006800.00
12015-02-0140.0010.00400.006800.00
12015-03-0140.0010.00400.006800.00
22015-01-1540.0012.00480.007680.00
22015-02-1540.0012.00480.007680.00
22015-03-1540.0012.00480.007680.00
I was thinking I could restrict it with a window.
My desired output is as such:
IdEmpIDCheckDateUnitsMTD UnitsYTD UnitsRateAmountMTD AmountYTD AmountTaxAmountDeductAmountBenefitAmount
2912015-06-1540.0080.00520.0010.00400.00800.005200.00100.0050.0050.00
3022015-06-1540.0080.00520.0012.00480.00960.006240.00120.0060.0060.00
Any ideas?
Thanks,
Brian
September 4, 2015 at 11:43 am
Something like this maybe...
Also check out Jeff Modden's article on cross tab/pivot... I think the part about aggregates is useful..
http://www.sqlservercentral.com/articles/T-SQL/63681/
declare @EmpID int = 1
declare @ChkDate date = '2015-6-15'
declare @test-2 as table(
[Id] [int] NOT NULL,
[EmployeeID] [nvarchar](30) NULL,
[CheckDate] [date] NULL,
[Units] [decimal](19, 5) NULL,
[Rate] [decimal](19, 5) NULL,
[Amount] [decimal](19, 5) NULL,
[TaxAmount] [decimal](19, 5) NULL,
[DeductAmount] [decimal](19, 5) NULL,
[BenefitAmount] [decimal](19, 5) NULL)
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (1, N'1', CAST(N'2015-01-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (2, N'2', CAST(N'2015-01-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (3, N'1', CAST(N'2015-02-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (4, N'2', CAST(N'2015-02-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (5, N'1', CAST(N'2015-03-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (6, N'2', CAST(N'2015-03-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (7, N'1', CAST(N'2015-04-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (8, N'2', CAST(N'2015-04-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (9, N'1', CAST(N'2015-05-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (10, N'2', CAST(N'2015-05-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (11, N'1', CAST(N'2015-06-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (12, N'2', CAST(N'2015-06-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (13, N'1', CAST(N'2015-07-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (14, N'2', CAST(N'2015-07-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (15, N'1', CAST(N'2015-08-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (16, N'2', CAST(N'2015-08-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (17, N'1', CAST(N'2015-09-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (18, N'2', CAST(N'2015-09-01' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (19, N'1', CAST(N'2015-01-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (20, N'2', CAST(N'2015-01-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (21, N'1', CAST(N'2015-02-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (22, N'2', CAST(N'2015-02-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (23, N'1', CAST(N'2015-03-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (24, N'2', CAST(N'2015-03-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (25, N'1', CAST(N'2015-04-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (26, N'2', CAST(N'2015-04-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (27, N'1', CAST(N'2015-05-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (28, N'2', CAST(N'2015-05-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (29, N'1', CAST(N'2015-06-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (30, N'2', CAST(N'2015-06-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (31, N'1', CAST(N'2015-07-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (32, N'1', CAST(N'2015-08-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(10.00000 AS Decimal(19, 5)), CAST(400.00000 AS Decimal(19, 5)), CAST(100.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)), CAST(50.00000 AS Decimal(19, 5)))
INSERT @test-2 ([Id], [EmployeeID], [CheckDate], [Units], [Rate], [Amount], [TaxAmount], [DeductAmount], [BenefitAmount]) VALUES (33, N'2', CAST(N'2015-08-15' AS Date), CAST(40.00000 AS Decimal(19, 5)), CAST(12.00000 AS Decimal(19, 5)), CAST(480.00000 AS Decimal(19, 5)), CAST(120.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)), CAST(60.00000 AS Decimal(19, 5)))
select t.employeeid, CheckDate, Units
,agrT.TotalUnits MTD
,agrT.TotalUnits YTD
, Rate, Amount
,agrT.TotalAmt [MTD Amount]
,agrT.TotalAmt [YTD Amount]
from @test-2 t
Cross apply (select employeeid
,YEAR(checkdate) CheckYear
, sum(Units) TotalUnits
, sum(Amount) TotalAmt
from @test-2
where EmployeeID = t.EmployeeID
and YEAR(checkdate) = YEAR(t.checkdate)
and CheckDate <= t.CheckDate
group by EmployeeID, YEAR(checkdate)) agrT
where t.EmployeeID = @EmpID
and t.CheckDate = @ChkDate
---------------------------------------------------------------
Mike Hahn - MCSomething someday:-)
Right way to ask for help!!
http://www.sqlservercentral.com/articles/Best+Practices/61537/
I post so I can see my avatar :hehe:
I want a personal webpage 😎
I want to win the lotto 😀
I want a gf like Tiffa :w00t: Oh wait I'm married!:-D
September 4, 2015 at 12:47 pm
As you're reading all the rows from YTD, I guess a single scan should perform better.
Be sure not to get lost on what this is doing. Since you're using 2008, there's no last_value function available, so I had to simulate it (kind of).
DECLARE @Date date = '6/15/15'
SELECT MAX( CASE WHEN CheckDate = @Date THEN Id END) Id,
EmployeeID ,
MAX( CASE WHEN CheckDate = @Date THEN CheckDate END) CheckDate,
MAX( CASE WHEN CheckDate = @Date THEN [Units] END) [Units],
SUM( CASE WHEN CheckDate >= DATEADD( MM, DATEDIFF(MM, 0, @Date),0)
AND CheckDate <= @Date THEN [Units] END) [MTD Units],
SUM( CASE WHEN CheckDate >= DATEADD( YY, DATEDIFF(YY, 0, @Date),0)
AND CheckDate <= @Date THEN [Units] END) [YTD Units],
Rate,
MAX( CASE WHEN CheckDate = @Date THEN [Amount] END) [Amount],
SUM( CASE WHEN CheckDate >= DATEADD( MM, DATEDIFF(MM, 0, @Date),0)
AND CheckDate <= @Date THEN Amount END) [MTD Amount],
SUM( CASE WHEN CheckDate >= DATEADD( YY, DATEDIFF(YY, 0, @Date),0)
AND CheckDate <= @Date THEN Amount END) [YTD Amount],
MAX( CASE WHEN CheckDate = @Date THEN [TaxAmount] END) [TaxAmount],
MAX( CASE WHEN CheckDate = @Date THEN [DeductAmount] END) [DeductAmount],
MAX( CASE WHEN CheckDate = @Date THEN [BenefitAmount] END) [BenefitAmount]
FROM [_Test]
where CheckDate > DATEADD( YY, DATEDIFF(YY, 0, @Date),0)
AND CheckDate <= @Date
GROUP BY EmployeeID, Rate
September 4, 2015 at 2:00 pm
Luis Cazares (9/4/2015)
As you're reading all the rows from YTD, I guess a single scan should perform better.Be sure not to get lost on what this is doing. Since you're using 2008, there's no last_value function available, so I had to simulate it (kind of).
DECLARE @Date date = '6/15/15'
SELECT MAX( CASE WHEN CheckDate = @Date THEN Id END) Id,
EmployeeID ,
MAX( CASE WHEN CheckDate = @Date THEN CheckDate END) CheckDate,
MAX( CASE WHEN CheckDate = @Date THEN [Units] END) [Units],
SUM( CASE WHEN CheckDate >= DATEADD( MM, DATEDIFF(MM, 0, @Date),0)
AND CheckDate <= @Date THEN [Units] END) [MTD Units],
SUM( CASE WHEN CheckDate >= DATEADD( YY, DATEDIFF(YY, 0, @Date),0)
AND CheckDate <= @Date THEN [Units] END) [YTD Units],
Rate,
MAX( CASE WHEN CheckDate = @Date THEN [Amount] END) [Amount],
SUM( CASE WHEN CheckDate >= DATEADD( MM, DATEDIFF(MM, 0, @Date),0)
AND CheckDate <= @Date THEN Amount END) [MTD Amount],
SUM( CASE WHEN CheckDate >= DATEADD( YY, DATEDIFF(YY, 0, @Date),0)
AND CheckDate <= @Date THEN Amount END) [YTD Amount],
MAX( CASE WHEN CheckDate = @Date THEN [TaxAmount] END) [TaxAmount],
MAX( CASE WHEN CheckDate = @Date THEN [DeductAmount] END) [DeductAmount],
MAX( CASE WHEN CheckDate = @Date THEN [BenefitAmount] END) [BenefitAmount]
FROM [_Test]
where CheckDate > DATEADD( YY, DATEDIFF(YY, 0, @Date),0)
AND CheckDate <= @Date
GROUP BY EmployeeID, Rate
DATEADD( YY, DATEDIFF(YY, 0, @Date),0)
Hi Luis, I've never seen this before and I don't see it in the online documentation... how does it work? I always thought you needed a date of some kind in the startdate argument same for the 0 in the DATEADD function... thanks, I'll remember this next time. 🙂
---------------------------------------------------------------
Mike Hahn - MCSomething someday:-)
Right way to ask for help!!
http://www.sqlservercentral.com/articles/Best+Practices/61537/
I post so I can see my avatar :hehe:
I want a personal webpage 😎
I want to win the lotto 😀
I want a gf like Tiffa :w00t: Oh wait I'm married!:-D
September 4, 2015 at 5:58 pm
Want a cool Sig (9/4/2015)
Hi Luis, I've never seen this before and I don't see it in the online documentation... how does it work? I always thought you needed a date of some kind in the startdate argument same for the 0 in the DATEADD function... thanks, I'll remember this next time. 🙂
It's actually a very old trick. "0" is the "Date Serial Number" for 1900-01-01. The DATEDIFF counts the difference between that date and today's date in years (Year "boundaries", actually). The DATEADD adds those number of years back to 1900-01-01 to come up with the first date of this year, which is 2015-01-01.
A "Date Serial Number" is defined as "The number of days since midnight of 1900-01-01 for dates after that date and the number of days until 1900-01-01 (expressed as a negative number) for dates prior to that "0" date.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 4, 2015 at 9:28 pm
Thanks Jeff... that helps me remember it if I understand why it's doing what it does.. 🙂
---------------------------------------------------------------
Mike Hahn - MCSomething someday:-)
Right way to ask for help!!
http://www.sqlservercentral.com/articles/Best+Practices/61537/
I post so I can see my avatar :hehe:
I want a personal webpage 😎
I want to win the lotto 😀
I want a gf like Tiffa :w00t: Oh wait I'm married!:-D
September 8, 2015 at 7:45 am
Want a cool Sig (9/4/2015)
DATEADD( YY, DATEDIFF(YY, 0, @Date),0)
Hi Luis, I've never seen this before and I don't see it in the online documentation... how does it work? I always thought you needed a date of some kind in the startdate argument same for the 0 in the DATEADD function... thanks, I'll remember this next time. 🙂
Maybe it would be clearer if I write the full date like this.
DATEADD( YY, DATEDIFF(YY, '19000101', @Date),'19000101')
As Jeff explained, 1900-01-01 is the zero date, so this is completely equivalent.
It uses datediff to get the years from 1900 to the date and then add that number of years to 1900-01-01. This will help you yo get the start of the different date parts, such as year, month, week, quarter, semester, etc. Here are some examples: http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/
September 11, 2015 at 11:54 am
Very Clever on the datediff stuff. Thanks for the rest as well. It appears to be what I needed.
Thanks,
Brian
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply