|
sqlserverlearner
Starting Member
21 Posts |
Posted - 2009-01-16 : 17:58:05
|
| SELECT DISTINCT B.Location_Code,B.FY_Month,B.FY_Year,B.Fiscal_Month_Id, (CASE WHEN (HP.Hires) IS NOT NULL THEN SUM(HP.Hires)OVER(PARTITION BY B.Location_Code,B.FY_Month,B.Fiscal_Month_Id,B.FY_Year) ELSE 0 END)AS Hires, (CASE WHEN (HP.Separations_Voluntary) IS NOT NULL THEN SUM(HP.Separations_Voluntary)OVER(PARTITION BY B.Location_Code,B.FY_Month,B.Fiscal_Month_Id,B.FY_Year) ELSE 0 END) AS SepV, (CASE WHEN (HP.Separations_Involuntary)IS NOT NULL THEN SUM(HP.Separations_Involuntary)OVER(PARTITION BY B.Location_Code,B.FY_Month,B.Fiscal_Month_Id,B.FY_Year) ELSE 0 END)AS SepInv, (CASE WHEN (HP.Transfers_In)IS NOT NULL THEN SUM(HP.Transfers_In)OVER(PARTITION BY B.Location_Code,B.FY_Month,B.Fiscal_Month_Id,B.FY_Year)ELSE 0 END)AS TIn, (CASE WHEN (HP.Transfers_Out)IS NOT NULL THEN SUM(HP.Transfers_Out)OVER(PARTITION BY B.Location_Code,B.FY_Month,B.Fiscal_Month_Id,B.FY_Year)ELSE 0 END) AS TOut, (CASE WHEN (HP.Promotions_In)IS NOT NULL THEN SUM(HP.Promotions_In)OVER(PARTITION BY B.Location_Code,B.FY_Month,B.Fiscal_Month_Id,B.FY_Year)ELSE 0 END) AS PIn, (CASE WHEN (HP.Promotions_Out)IS NOT NULL THEN SUM(HP.Promotions_Out)OVER(PARTITION BY B.Location_Code,B.FY_Month,B.Fiscal_Month_Id,B.FY_Year)ELSE 0 END)AS POut, (CASE WHEN (HP.Leave_Of_Absence_In)IS NOT NULL THEN SUM(HP.Leave_Of_Absence_In)OVER(PARTITION BY B.Location_Code,B.FY_Month,B.Fiscal_Month_Id,B.FY_Year)ELSE 0 END) AS LOAIn, (CASE WHEN (HP.Leave_Of_Absence_Out)IS NOT NULL THEN SUM(HP.Leave_Of_Absence_Out)OVER(PARTITION BY B.Location_Code,B.FY_Month,B.Fiscal_Month_Id,B.FY_Year)ELSE 0 END)AS LOAOut, (CASE WHEN (HP.Other)IS NOT NULL THEN SUM(HP.Other)OVER(PARTITION BY B.Location_Code,B.FY_Month,B.Fiscal_Month_Id,B.FY_Year)ELSE 0 END) AS Other, B.Total_Hours AS TotalHours FROM (SELECT DISTINCT t.BUId,t.BUCode,t.Location_Code,FisM.FY_Month,FisM.FY_Year,t.Location_Id,FisM.Fiscal_Month_Id,FisM.Total_HoursFROM (Select DISTINCT BU.Business_Unit_Id AS BUId,BU.Business_Unit_Code AS BUCode,L.Location_Code,HP.Location_Id FROM Location L INNER JOIN HeadCount_Planned HP ON L.Location_Id = HP.Location_Id AND L.Location_Code IN('1413A','0533B') INNER JOIN Business_Unit BU ON BU.Business_Unit_Id = HP.Business_Unit_Id AND BU.Business_Unit_Code IN('US017','US313') AND Planning_Period_Id = (Select Planning_Period_id FROM Planning_Period WHERE FY_Year = '2009'))tCROSS JOIN Fiscal_Month FisM WHERE Fiscal_Month_Id BETWEEN (Select Fiscal_Month_Id FROM Fiscal_Month WHERE FY_Year = '2008' AND FY_Month = 'MAR') AND (Select Max(Fiscal_Month_Id) FROM Fiscal_Month WHERE FY_Year = '2009'))BLEFT JOIN HeadCount_Planned HPON HP.Fiscal_Month_Id = B.Fiscal_Month_Id AND HP.Location_Id = B.Location_Id AND HP.Business_Unit_Id = B.BUIdAND HP.Planning_Period_Id = (Select Planning_Period_id FROM Planning_Period WHERE FY_Year = '2009' AND is_Current = 1)ORDER BY B.Location_Code,B.FY_Year,B.FY_Month,B.Fiscal_Month_IdWhen in query (Select DISTINCT BU.Business_Unit_Id AS BUId,BU.Business_Unit_Code AS BUCode,L.Location_Code,HP.Location_Id FROM Location L INNER JOIN HeadCount_Planned HP ON L.Location_Id = HP.Location_Id AND L.Location_Code IN('1413A','0533B') INNER JOIN Business_Unit BU ON BU.Business_Unit_Id = HP.Business_Unit_Id AND BU.Business_Unit_Code IN('US017','US313') AND Planning_Period_Id = (Select Planning_Period_id FROM Planning_Period WHERE FY_Year = '2009'))returns no row, I get NULL but I need to still get the BU and Loc displayed but with 0 values. |
 |
|