窗口分区功能需要帮助

时间:2015-09-04 15:49:26

标签: sql sql-server

我有一个数据集,其中包含当年的所有薪资记录。我想添加一个年初至今和一个月的日期,并希望不必单独总结它们然后加入结果。这是我的数据:

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

我希望返回薪水支票的数据以及包含单个日期的月初至今和年初至今的值。我认为这样的事情可能有用,但我在设置窗口时遇到了麻烦。

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'

我在考虑使用Over的原因是可行的,因为如果我取消我的WHERE子句并执行以下操作,我会得到每行上所有项目的总和。

select employeeid, CheckDate, Units
        , Rate, Amount
        ,sum(Amount) over (Partition by EmployeeId) [All]
from _Test

empidCheckDate  Units   Rate    Amount  All
1   2015-01-01  40.00   10.00   400.00  6800.00
1   2015-02-01  40.00   10.00   400.00  6800.00
1   2015-03-01  40.00   10.00   400.00  6800.00
2   2015-01-15  40.00   12.00   480.00  7680.00
2   2015-02-15  40.00   12.00   480.00  7680.00
2   2015-03-15  40.00   12.00   480.00  7680.00

我以为我可以用窗户限制它。

我想要的输出是这样的:

Id  EmpID   CheckDate   Units   MTD Units   YTD Units   Rate    Amount  MTD Amount  YTD Amount  TaxAmount   DeductAmount    BenefitAmount
29    1     2015-06-15  40.00   80.00       520.00      10.00   400.00  800.00      5200.00     100.00      50.00           50.00
30    2     2015-06-15  40.00   80.00       520.00      12.00   480.00  960.00      6240.00     120.00      60.00           60.00

有什么想法吗?

谢谢,

布赖恩

1 个答案:

答案 0 :(得分:0)

只需要调整PARTITION BY以便YEAR和/或MONTH将其分开:

select employeeid, CheckDate, Units,
    sum(Units) over (Partition by employeeid, YEAR(CheckDate),MONTH(CheckDate)
                        ORDER BY CheckDate 
                        ROWS between unbounded preceding and current row) [MTD Units],

    sum(Units) over (Partition by EmployeeId, YEAR(CheckDate)
                        ORDER BY CheckDate 
                        ROWS between unbounded preceding and current row) [YTD Units],
    , Rate, Amount,

    sum(Amount) over (Partition by employeeid, YEAR(CheckDate),MONTH(CheckDate)
                        ORDER BY CheckDate 
                        ROWS between unbounded preceding and current row) [MTD Amount],

    sum(Amount) over (Partition by EmployeeId, YEAR(CheckDate)
                        ORDER BY CheckDate 
                        ROWS between unbounded preceding and current row) [YTD Amount],

from _Test

这样,您创建的运行总金额将被限制在适当的时间段内,您不希望将上个月的金额添加到本月的运行总计中。如果您只想要特定日期的值,则需要在计算YTD和MTD后使用子查询/ cte进行过滤,因为如果将过滤器放在同一查询中,则必需的数量将不可用于聚合:

;with cte AS (select employeeid, CheckDate, Units,
    sum(Units) over (Partition by employeeid, YEAR(CheckDate),MONTH(CheckDate)
                        ORDER BY CheckDate 
                        ROWS between unbounded preceding and current row) [MTD Units],

    sum(Units) over (Partition by EmployeeId, YEAR(CheckDate)
                        ORDER BY CheckDate 
                        ROWS between unbounded preceding and current row) [YTD Units],
    , Rate, Amount,

    sum(Amount) over (Partition by employeeid, YEAR(CheckDate),MONTH(CheckDate)
                        ORDER BY CheckDate 
                        ROWS between unbounded preceding and current row) [MTD Amount],

    sum(Amount) over (Partition by EmployeeId, YEAR(CheckDate)
                        ORDER BY CheckDate 
                        ROWS between unbounded preceding and current row) [YTD Amount],

    from _Test
    )
SELECT *
FROM cte
WHERE CheckDate = '6/15/15'

修改:最初只调整了Amount次计算,添加了Units