Tuesday, October 9, 2018

Create Nested Selects instead of Joining a Reference Table in ODBC

The example below shows a sample query linking the VENDOR table to the VENDOR_TYPES table.
 
SELECT
  A.VENDOR_ID,
  A.VENDOR_EXTID,
  A.VENDOR_TYPE_ID,
  (SELECT B.NAME FROM VENDOR_TYPES AS B WHERE B.VENDOR_TYPE_ID = A.VENDOR_TYPE_ID) AS 'Vendor Type'
FROM
  VENDORS AS A
WHERE
  A.VENDOR_ID = 139
 
Query above shows the following results:
 
VENDOR_ID   |   VENDOR_EXTID   |   VENDOR_TYPE_ID   |   Vendor Type
139                     NULL                         3                                 Tax Agency
 
For visual purposes, the VENDOR_TYPES Table has the following entries:
 
ISINACTIVE   |   NAME            |   PARENT_ID   |   VENDOR_TYPE_EXTID   |   VENDOR_TYPE_ID
No                      Tax Agency       NULL                  NULL                                   3
 
and VENDORS has (SELECT VENDOR_ID, VENDOR_TYPE_ID FROM VENDORS WHERE VENDOR_ID = 139):
 
VENDOR_ID   |   VENDOR_TYPE_ID
139                     3
 
Note: Nested selects is an alternative to Joins. The ODBC user determines which approach to use.

No comments:

Post a Comment