I have been trying to write this complex query where I have to calculate a total FTE by prganization by profession by year . So e.g. in org1 for 2011 we have 2 DataEntryC whose total FTE = 50.95+70.95 but now i also have to add 20% of admin staff working for org1 in 2011 FTE to this . And this final figure would be the the total FTE for data entry clarks . Basically within an org in a year total staff FTE = all staff FTE +20% of admin staff FTE . So , for 2011 in Org1 data entry clark should have 141.90 FTE (50.95+70.95 +20)
I have written the query as below but this only work when there is only one organization an has 2 professions listed . I think selfjoin might be the answer ......
update test
set calculated_adjustment= (select calculated_adjustment from test where profession='Admin')
where profession='DataEntryC'
Scripts:
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)
for 2011 in Org1 data entry clark should have 141.90 FTE