I have a primary table, REP_ASSIST.ES_LOG with multiple columns that are foreign keys to another table, REP_ASSIST.ES_DOCUMENT_STATUSES. Here is an example of what my main table looks like:
+-------------------------+-----------+-------------------------+-----------------------+
| created_at | filing_id | prior_dd_rcvd_status_id | new_dd_rcvd_status_id |
+-------------------------+-----------+-------------------------+-----------------------+
| 2019-09-04 10:58:48.000 | 988 | 2 | 2 |
| 2019-09-04 10:47:03.000 | 988 | 1 | 2 |
| 2019-08-28 23:56:47.000 | 988 | null | 1 |
+-------------------------+-----------+-------------------------+-----------------------+
Both the prior_dd_rcvd_status_id and new_dd_rcvd_status_id are foreign keys to the other table, a sample of which is here:
+------------------+--------------------+
| dd_doc_status_id | dd_doc_status_name |
+------------------+--------------------+
| 1 | RECEIVED |
| 2 | MISSING |
| 3 | NOT_NEEDED |
| 4 | UNKNOWN |
+------------------+--------------------+
I need to pull in the values from the lookup table for a printable report; right now I'm using a subquery to get the dd_doc_status_name as a user-friendly name instead of the dd_doc_status_id.
SELECT created_at,
filing_id,
(
SELECT dd_doc_status_name
FROM REP_ASSIST.ES_DOCUMENT_STATUSES
WHERE ES_DOCUMENT_STATUSES.dd_doc_status_id = prior_dd_rcvd_status_id
) AS 'prior_dd_status',
(
SELECT dd_doc_status_name
FROM REP_ASSIST.ES_DOCUMENT_STATUSES
WHERE ES_DOCUMENT_STATUSES.dd_doc_status_id = new_dd_rcvd_status_id
) AS 'new_dd_status'
FROM REP_ASSIST.ES_LOG
WHERE filing_id = 988;
However, I'm concerned that using subqueries like this may be a bit of a hack and affect performance (especially if I end up adding additional columns that need this information).
I'm getting the correct results (see below), but I'd like to know if there is a more efficient and more standard way.
+-------------------------+-----------+----------------------+--------------------+
| created_at | filing_id | prior_dd_rcvd_status | new_dd_rcvd_status |
+-------------------------+-----------+----------------------+--------------------+
| 2019-09-04 10:58:48.000 | 988 | MISSING | MISSING |
| 2019-09-04 10:47:03.000 | 988 | RECEIVED | MISSING |
| 2019-08-28 23:56:47.000 | 988 | null | RECEIVED |
+-------------------------+-----------+----------------------+--------------------+



left joins on yourES_DOCUMENT_STATUSEStable instead of the subqueries will be fine, I don't know aboutsql-serverso forgive me from putting a complete answer. \$\endgroup\$