Question : Converting ANSI-89 to ANSI-92 T-SQL queries

I am trying to convert the following query from ANSI-89 to ANSI-92..

With the query below I get 22 records, one of which has a non-null value in the ACC.AMOUNT field.

SELECT DISTINCT HDR.SERVICEPROVIDER, HDR.ACCOUNTNUMBER, DTL.OBLIGATIONID,
DTL.SEQUENCENUM, FP.FISCALPERIOD, FP.STARTDATE, OBL.REVENUEJO, OBL.BUDGETJO, ACC.AMOUNT as ACCRUALAMOUNT
      FROM ACCRUALTEMPLATEHEADER2 HDR, ACCRUALTEMPLATEDETAIL2 DTL, FISCALPERIOD2 FP, OBLIGATION OBL, vACCRUAL_DETAIL_OLD ACC
      WHERE HDR.ACCRUALTEMPLATEHEADERID = DTL.ACCRUALTEMPLATEHEADERID
      AND DTL.OBLIGATIONID *= OBL.OBLIGATIONID
      AND DTL.SEQUENCENUM *= OBL.SEQUENCENUM
      AND DTL.OBLIGATIONID *= ACC.OBLIGATIONID
      AND DTL.SEQUENCENUM *= ACC.SEQUENCENUM
      AND HDR.SERVICEPROVIDER *= ACC.SERVICEPROVIDER
      AND HDR.ACCOUNTNUMBER *= ACC.ACCOUNTNUMBER
      AND FP.FISCALPERIOD *= ACC.FISCALPERIOD
      AND DTL.OBLIGATIONID != ''
      AND DTL.SEQUENCENUM != ''
      AND FP.STARTDATE >= '3/01/2007'
      AND HDR.SERVICEPROVIDER = 'Allegheny Power'
      ORDER BY HDR.SERVICEPROVIDER, HDR.ACCOUNTNUMBER, DTL.OBLIGATIONID, DTL.SEQUENCENUM, FP.STARTDATE

I tried to write the above in ANSI-92 format but only pull the one record that has a non-null value in the ACC.Amount field.

SELECT DISTINCT HDR.SERVICEPROVIDER, HDR.ACCOUNTNUMBER, DTL.OBLIGATIONID, DTL.SEQUENCENUM
            , FP.FISCALPERIOD, FP.STARTDATE, OBL.REVENUEJO, OBL.BUDGETJO, ACC.AMOUNT as ACCRUALAMOUNT
      FROM ((((ACCRUALTEMPLATEHEADER2 HDR
            LEFT OUTER JOIN vACCRUAL_DETAIL ACC ON (HDR.SERVICEPROVIDER = ACC.SERVICEPROVIDER
                   AND HDR.ACCOUNTNUMBER = ACC.ACCOUNTNUMBER))
            INNER JOIN ACCRUALTEMPLATEDETAIL2 DTL ON HDR.ACCRUALTEMPLATEHEADERID = DTL.ACCRUALTEMPLATEHEADERID)
                  LEFT OUTER JOIN vACCRUAL_DETAIL ACC2 ON (DTL.OBLIGATIONID = ACC2.OBLIGATIONID
                   AND DTL.SEQUENCENUM = ACC2.SEQUENCENUM ))
            LEFT OUTER JOIN OBLIGATION OBL ON (DTL.OBLIGATIONID = OBL.OBLIGATIONID
                  AND DTL.SEQUENCENUM = OBL.SEQUENCENUM))
            LEFT OUTER JOIN FISCALPERIOD2 FP ON FP.FISCALPERIOD = ACC.FISCALPERIOD
      WHERE DTL.OBLIGATIONID != ''
            AND DTL.SEQUENCENUM != ''
            AND FP.STARTDATE >= '3/01/2007'
            AND HDR.SERVICEPROVIDER = 'Allegheny Power'
      ORDER BY HDR.SERVICEPROVIDER, HDR.ACCOUNTNUMBER, DTL.OBLIGATIONID, DTL.SEQUENCENUM, FP.STARTDATE

I have tried a lot of different things but can't seem to pull in all the records. Can someone give me an idea of why these two queries aren't pulling the same values?

Answer : Converting ANSI-89 to ANSI-92 T-SQL queries

The comment by Mark prompted me to review my query. I haven't really tried using the ANSI-89 syntax so had to do some testing.

"*=" = LEFT OUTER JOIN (as Mark has already mentioned) so it is important to know which is/are the "LEFT" table/s.

In this case, the LEFT tables are (I will be using aliases for simplicity) DTL, HDR and FP. Thus, using fields from these tables in the WHERE clause will not break the properties of using OUTER JOIN.

I also noticed that there is no direct relationship between FP and HDR/DTL which would result to a CROSS JOIN. The fields from the "RIGHT" tables will only be returned if it satisfies all the conditions.

Here is my attempt:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
SELECT DISTINCT 
  HDR.SERVICEPROVIDER
 ,HDR.ACCOUNTNUMBER
 ,DTL.OBLIGATIONID
 ,DTL.SEQUENCENUM
 ,FP.FISCALPERIOD 
 ,FP.STARTDATE
 ,OBL.REVENUEJO
 ,OBL.BUDGETJO
 ,ACC.AMOUNT as ACCRUALAMOUNT
FROM 
  ACCRUALTEMPLATEHEADER2 HDR INNER JOIN
  ACCRUALTEMPLATEDETAIL2 DTL ON HDR.ACCRUALTEMPLATEHEADERID = DTL.ACCRUALTEMPLATEHEADERID CROSS JOIN
  FISCALPERIOD2 FP LEFT JOIN
  vACCRUAL_DETAIL_OLD ACC ON DTL.OBLIGATIONID = ACC.OBLIGATIONID
                             AND DTL.SEQUENCENUM = ACC.SEQUENCENUM
                             AND HDR.SERVICEPROVIDER = ACC.SERVICEPROVIDER
                             AND HDR.ACCOUNTNUMBER = ACC.ACCOUNTNUMBER
                             AND FP.FISCALPERIOD = ACC.FISCALPERIOD LEFT JOIN
  OBLIGATION OBL ON DTL.OBLIGATIONID = OBL.OBLIGATIONID
                    AND DTL.SEQUENCENUM = OBL.SEQUENCENUM
WHERE 
  DTL.OBLIGATIONID != ''
  AND DTL.SEQUENCENUM != ''
  AND FP.STARTDATE >= '3/01/2007'
  AND HDR.SERVICEPROVIDER = 'Allegheny Power'
Random Solutions  
 
programming4us programming4us