Initial Sql For P L Drill Down
DECLARE @gltype01 VARCHAR(10)
 SET @gltype01 = 'A'
DECLARE @gltype02 VARCHAR(10)
 SET @gltype02 = 'A'
DECLARE @gltype03 VARCHAR(10)
 SET @gltype03 = 'A'
DECLARE @gltype01a VARCHAR(10)
DECLARE @gltype02a VARCHAR(10)
DECLARE @gltype03a VARCHAR(10)

DECLARE @budvers01 int
SET @budvers01 = 0
DECLARE @currency01 varchar(4)
SET @currency01 = 'USD'
DECLARE @glyr01 int
SET @glyr01 = 2003

DECLARE @budvers02 int
SET @budvers02 = 0
DECLARE @currency02 varchar(4)
SET @currency02 = ''
DECLARE @glyr02 int
SET @glyr02 = 0

DECLARE @budvers03 int
SET @budvers03 = 0
DECLARE @currency03 varchar(4)
SET @currency03 = ''
DECLARE @glyr03 int
SET @glyr03 = 0

IF (@gltype01 IN('A','C'))  SET @gltype01a = 'U'  ELSE SET @gltype01a = @gltype01
IF (@gltype02 IN('A','C'))  SET @gltype02a = 'U'  ELSE SET @gltype02a = @gltype02
IF (@gltype03 IN('A','C'))  SET @gltype03a = 'U'  ELSE SET @gltype03a = @gltype03
DECLARE @natsegnum int
SET @natsegnum = 0
SELECT @natsegnum = segnum FROM df_seg_control WHERE segtable = 'glnat'
SELECT
  df_fp_glacct_to_pl_lineitems.glaccount,
  gldes,
  df_fp_pl_lineitems.linetype,
  df_fp_glacct_to_pl_lineitems.linenum,
  df_fp_actuals_segvals.glyr,
  df_fp_actuals_segvals.budvers,
  df_fp_actuals.currency,
  df_fp_pl_lineitems.linedesc,
  SUM(df_fp_actuals.mthamt01) AS mthamt01,
  SUM(df_fp_actuals.mthamt02) AS mthamt02,
  SUM(df_fp_actuals.mthamt03) AS mthamt03,
  SUM(df_fp_actuals.mthamt04) AS mthamt04,
  SUM(df_fp_actuals.mthamt05) AS mthamt05,
  SUM(df_fp_actuals.mthamt06) AS mthamt06,
  SUM(df_fp_actuals.mthamt07) AS mthamt07,
  SUM(df_fp_actuals.mthamt08) AS mthamt08,
  SUM(df_fp_actuals.mthamt09) AS mthamt09,
  SUM(df_fp_actuals.mthamt10) AS mthamt10,
  SUM(df_fp_actuals.mthamt11) AS mthamt11,
  SUM(df_fp_actuals.mthamt12) AS mthamt12,
  SUM(df_fp_actuals.mthamt13) AS mthamt13
FROM df_fp_actuals_segvals
INNER JOIN df_fp_glacct_to_pl_lineitems ON
  (df_fp_actuals_segvals.segval = df_fp_glacct_to_pl_lineitems.glaccount) AND
  (df_fp_actuals_segvals.segnum = @natsegnum)
INNER JOIN df_fp_pl_lineitems ON
  (df_fp_glacct_to_pl_lineitems.linenum = df_fp_pl_lineitems.linenum)
INNER JOIN df_fp_actuals ON
  (df_fp_actuals_segvals.glactid = df_fp_actuals.glactid)
INNER JOIN glnat ON
  (df_fp_glacct_to_pl_lineitems.glaccount = natcode)
WHERE
  ((df_fp_actuals_segvals.glyr = @glyr01) AND
   (df_fp_actuals_segvals.gltype IN(@gltype01,@gltype01a)) AND
   (df_fp_actuals_segvals.budvers = @budvers01) AND
   (df_fp_actuals.currency = @currency01))
OR
  ((df_fp_actuals_segvals.glyr = @glyr02) AND
   (df_fp_actuals_segvals.gltype IN(@gltype02,@gltype02a)) AND
   (df_fp_actuals_segvals.budvers = @budvers02) AND
   (df_fp_actuals.currency = @currency02))
OR
  ((df_fp_actuals_segvals.glyr = @glyr03) AND
   (df_fp_actuals_segvals.gltype IN(@gltype03,@gltype03a)) AND
   (df_fp_actuals_segvals.budvers = @budvers03) AND
   (df_fp_actuals.currency = @currency03))
GROUP BY
  df_fp_glacct_to_pl_lineitems.glaccount,
  gldes,
  df_fp_glacct_to_pl_lineitems.linenum,
  df_fp_pl_lineitems.linedesc,
  df_fp_pl_lineitems.linetype,
  df_fp_actuals_segvals.glyr,
  df_fp_actuals_segvals.budvers,
  df_fp_actuals.currency
ORDER BY
  CASE df_fp_pl_lineitems.linetype
    WHEN 'R' THEN 1
    WHEN 'C' THEN 2
    WHEN 'O' THEN 3
    WHEN 'P' THEN 4
    ELSE 5 END,
  df_fp_pl_lineitems.linetype,
  df_fp_glacct_to_pl_lineitems.linenum