SELECT
ISNULL(P.PROJECTID, 'N/A') "PROJECT",
GA.ACCOUNTNUMBER,
ISNULL(PB.AMOUNT,0) "AMOUNT",
ISNULL(PBD13.AMOUNT,0) "Period 1",
ISNULL(PBD14.AMOUNT,0) "Period 2",
ISNULL(PBD15.AMOUNT,0) "Period 3",
ISNULL(PBD16.AMOUNT,0) "Period 4",
ISNULL(PBD17.AMOUNT,0) "Period 5",
ISNULL(PBD18.AMOUNT,0) "Period 6",
ISNULL(PBD19.AMOUNT,0) "Period 7",
ISNULL(PBD20.AMOUNT,0) "Period 8",
ISNULL(PBD21.AMOUNT,0) "Period 9",
ISNULL(PBD22.AMOUNT,0) "Period 10",
ISNULL(PBD23.AMOUNT,0) "Period 11",
ISNULL(PBD24.AMOUNT,0) "Period 12"

FROM
GL7BUDGETSCENARIOS BS
LEFT OUTER JOIN GL7ACCOUNTBUDGETS AB ON AB.GL7BUDGETSCENARIOSID = BS.GL7BUDGETSCENARIOSID
LEFT OUTER JOIN GL7PROJECTBUDGETS PB ON PB.GL7ACCOUNTBUDGETSID = AB.GL7ACCOUNTBUDGETSID
LEFT OUTER JOIN GL7PROJECTS P ON P.GL7PROJECTSID = PB.GL7PROJECTSID
INNER JOIN GL7FISCALYEARS FY ON BS.GL7FISCALYEARSID = FY.GL7FISCALYEARSID
INNER JOIN GL7ACCOUNTS GA ON AB.GL7ACCOUNTSID = GA.GL7ACCOUNTSID
INNER JOIN TABLEENTRIES T ON BS.SCENARIOID = T.TABLEENTRIESID
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD13 ON PBD13.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD13.GL7FISCALPERIODSID = 13
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD14 ON PBD14.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD14.GL7FISCALPERIODSID = 14
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD15 ON PBD15.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD15.GL7FISCALPERIODSID = 15
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD16 ON PBD16.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD16.GL7FISCALPERIODSID = 16
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD17 ON PBD17.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD17.GL7FISCALPERIODSID = 17
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD18 ON PBD18.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD18.GL7FISCALPERIODSID = 18
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD19 ON PBD19.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD19.GL7FISCALPERIODSID = 19
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD20 ON PBD20.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD20.GL7FISCALPERIODSID = 20
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD21 ON PBD21.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD21.GL7FISCALPERIODSID = 21
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD22 ON PBD22.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD22.GL7FISCALPERIODSID = 22
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD23 ON PBD23.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD23.GL7FISCALPERIODSID = 23
LEFT OUTER JOIN GL7PROJECTBUDGETDETAILS PBD24 ON PBD24.GL7PROJECTBUDGETSID = PB.GL7PROJECTBUDGETSID AND PBD24.GL7FISCALPERIODSID = 24

ORDER BY
P.PROJECTID,
GA.ACCOUNTNUMBER