SQL SERVER 2008计算字段查询

时间:2014-12-15 20:20:05

标签: sql-server self-join

我一直在尝试编写这个复杂的查询,我必须按行业按年计算总体FTE。所以例如在2011年的org1中,我们有2个DataEntryC,其总FTE = 50.95 + 70.95但是现在我还需要在2011年FTE中为org1工作的管理员增加20%。最后的数字将是数据输入克拉克的总FTE。基本上在一年内一个组织内的员工FTE =所有员工FTE +管理人员FTE的20%。 所以,对于2011年的Org1数据录入,克拉克应该有141.90 FTE (50.95 + 70.95 +20)

enter image description here 我已经编写了如下所示的查询,但只有当只有一个组织列出了2个职业时才能使用。我认为selfjoin可能就是答案......

update test
        set calculated_adjustment= (select calculated_adjustment from test where profession='Admin')
        where profession='DataEntryC'

脚本:

CREATE TABLE [dbo].[test](
    [year] [varchar](5) NULL,
    [org_name] [varchar](10) NULL,
    [profession] [varchar](10) NULL,
    [fte] [float] NULL,
    [calculated_adjustment] [float] NULL
) 
GO


GO
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org1', N'DataEntryC', 50.95, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org1', N'DataEntryC', 70.95, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org1', N'Admin', 200, 20)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org2', N'Admin', 150, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2012', N'org1', N'DataEntryC', 20, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2012', N'org2', N'Admin', 15, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org1', N'Support', 50, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org1', N'Support', 100, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2012', N'org2', N'Support', 200, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2012', N'org2', N'Admin', 150, NULL)
对于2011年的Org1数据录入,克拉克应该有141.90 FTE

1 个答案:

答案 0 :(得分:0)

您的样本数据不足以让我估计我得到了答案(即这可能仍然是错误的 - 需要更广泛的测试),所以我使用了这些数据:

INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org1', N'DataEntryC', 50.95, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org1', N'DataEntryC', 70.95, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org1', N'Admin', 200, 20)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org2', N'Admin', 150, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2012', N'org1', N'DataEntryC', 20, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2012', N'org2', N'Admin', 15, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org1', N'Support', 50, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2011', N'org1', N'Support', 100, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2012', N'org2', N'Support', 200, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2012', N'org2', N'Admin', 150, 15)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2012', N'org2', N'DataEntryC', 73.27, NULL)
INSERT [dbo].[test] ([year], [org_name], [profession], [fte], [calculated_adjustment]) VALUES (N'2012', N'org2', N'DataEntryC', 20, NULL)

然后我做了一些研究并想出了:

;WITH x (yr, prof, org, fteSum) AS
(
SELECT DISTINCT
    T.year AS yr
    , T.profession AS prof
    , T.org_name AS org
    , SUM(T.fte) OVER (PARTITION BY T.year)
        +
        (
        SELECT SUM(COALESCE(calculated_adjustment, 0))
        FROM test
        WHERE T.year = year
        AND T.org_name = org_name
        AND profession = 'Admin'
        ) AS fteSum
FROM test AS T
JOIN test AS S
ON S.year = T.year
WHERE T.profession = 'DataEntryC'
GROUP BY T.year, T.org_name, T.fte, T.profession
)

--select yr, prof, org, fteSum FROM x

UPDATE test
SET calculated_adjustment = x.fteSum
FROM x
where test.year = x.yr
AND test.org_name = x.org
AND test.profession = 'DataEntryC'

给出了

year    org_name    profession  fte calculated_adjustment
2011    org1    DataEntryC  50.95   141.9
2011    org1    DataEntryC  70.95   141.9
2011    org1    Admin   200 20
2011    org1    Support 50  NULL
2011    org1    Support 100 NULL
2011    org2    Admin   150 NULL
2012    org1    DataEntryC  20  NULL
2012    org2    Admin   15  NULL
2012    org2    Support 200 NULL
2012    org2    Admin   150 15
2012    org2    DataEntryC  73.27   128.27
2012    org2    DataEntryC  20  128.27

这可能是您之后的结果。

请注意已注释掉的SELECT。这就是我用来确保WITH thingy(又名临时命名结果集,称为公用表表达式(CTE))返回正确结果的方法,这是我收集的一种技术来自answer by Aaron Bertrand