SQL-89 Conversion to SQL-92

Status
Not open for further replies.

Crolis

Ars Legatus Legionis
20,242
Subscriptor
I'm having to convert someone's query from SQL-89 to SQL-92 and I thought I had it correct but am getting different counts and am slowly going crazy trying to figure it out. I don't have a lot of experience with SQL-89 so could be doing something incorrect.

SQL-89:

Code:
SELECT COUNT(*)
  FROM apps.ap_supplier_sites_all assa,
       apps.ap_suppliers asp,
       apps.ap_invoice_lines_all aila,
       apps.ap_invoices_all aia,
       apps.pa_cost_distribution_lines_all pcdl,
       apps.pa_expenditures_all pea,
       apps.pa_expenditure_items_all peia,
       apps.pa_tasks pt,
       apps.pa_projects_all ppa
 WHERE assa.vendor_site_id (+) = aia.vendor_site_id
   AND assa.vendor_id (+) = aia.vendor_id
   AND asp.vendor_id (+) = aia.vendor_id
   AND aila.line_number (+) = peia.document_line_number
   AND aila.invoice_id (+) = peia.document_header_id
   AND aia.invoice_id (+) = peia.document_header_id
   AND pcdl.expenditure_item_id = peia.expenditure_item_id
   AND pea.expenditure_id = peia.expenditure_id
   AND peia.task_id = pt.task_id
   AND pt.project_id = ppa.project_id
   AND ppa.template_flag = 'N'
ORDER BY ppa.project_id, pt.task_id;

SQL-92:
Code:
SELECT COUNT(*)
FROM apps.pa_expenditures_all pea
 CROSS JOIN apps.pa_expenditure_items_all peia 
 CROSS JOIN apps.pa_tasks pt 
 CROSS JOIN apps.pa_projects_all ppa 
 RIGHT JOIN apps.ap_invoices_all aia ON ( aia.invoice_id = peia.document_header_id)
 RIGHT JOIN apps.ap_invoice_lines_all aila ON ( aila.invoice_id = peia.document_header_id AND aila.line_number = peia.document_line_number)
 CROSS JOIN apps.pa_cost_distribution_lines_all pcdl
 RIGHT JOIN apps.ap_suppliers asp ON (asp.vendor_id = aia.vendor_id)
 RIGHT JOIN apps.ap_supplier_sites_all assa ON (assa.vendor_site_id = aia.vendor_site_id AND assa.vendor_id = aia.vendor_id)
 WHERE pea.expenditure_id = peia.expenditure_id
   AND peia.task_id = pt.task_id
   AND pt.project_id = ppa.project_id
   AND pcdl.expenditure_item_id = peia.expenditure_item_id
   AND ppa.template_flag = 'N'
ORDER BY ppa.project_id, pt.task_id;

The SQL-89 query returns 29853 results where as the SQL-92 query returns 26612 results. FYI this is done in Oracle if that matters. Any help would be appreciated.
 
I took a quick stab at it, converting the RIGHT JOINS to LEFT JOINS and rearranging the table order where it made sense in my head.

Untested obviously, but I'd be curious to see the result if you run this.
Code:
SELECT COUNT(*)
FROM apps.pa_expenditure_items_all peia
INNER JOIN apps.pa_cost_distribution_lines_all pcdl
  ON (peia.expenditure_item_id = pcdl.expenditure_item_id)
INNER JOIN apps.pa_expenditures_all pea 
  ON (peia.expenditure_id = pea.expenditure_id)
INNER JOIN apps.pa_tasks pt 
  ON (peia.task_id = pt.task_id)
INNER JOIN apps.pa_projects_all ppa 
  ON (pt.project_id = ppa.project_id)
LEFT JOIN apps.ap_invoices_all aia 
  ON (peia.document_header_id = aia.invoice_id)
LEFT JOIN apps.ap_invoice_lines_all aila 
  ON (peia.document_line_number = aila.line_number AND peia.document_header_id = aila.invoice_id)
LEFT JOIN apps.ap_supplier_sites_all assa
  ON (aia.vendor_site_id = assa.vendor_site_id AND aia.vendor_id = assa.vendor_id)
LEFT JOIN apps.ap_suppliers asp 
  ON (aia.vendor_id = asp.vendor_id)
WHERE ppa.template_flag = 'N'
ORDER BY ppa.project_id, pt.task_id;
 
Well the way you had it in your original query with the (+) on the left side of the equals is a RIGHT JOIN, which just means you want all the records from the table referenced on the RIGHT and any thing that matches the table on the left, with NULLS for anything that doesn't match anything on the right. I just changed them from RIGHT to LEFT joins by switching the order of the tables when converting it to ANSI 92 syntax. But using LEFT JOINS is just the way I have always done it because that is the way I look at the problem in my head. The SQL engine doesn't care. :)

I was wondering why you were trying to use a CROSS join. Normally that results in a Cartesian product of every result of the tables you were cross joining, but then you actually joined them together in your WHERE clause by a field, thus creating an INNER JOIN.
 
Status
Not open for further replies.