Language: T-SQL
Server: SQL Server 2008 R2 - SQL Server 2014
I have what, based on searching here an elsewhere, appears to either be a unique problem or I can't properly verbalize what I'm trying to accomplish. I'd like to query across multiple dissimilar tables that have dissimilar field structures and JOIN them to a single other table. We have a table of ASSETS tb_assets
and a table of LICENSES tb_licenses
. I'd like to query across both of these and JOIN them to the table of VENDORS tb_vendors
. I can't join tb_assets
and tb_licenses
together as they have no commonality. They do, however, reference the vendor GUID from tb_vendors
.
Like this:
-------------------------- ---------------------------
| TB_ASSETS | | TB_LICENSES |
-------------------------- ---------------------------
| f_assetvendorGUID | <~~~ ~~~> | f_licensevendorGUID |
| f_assettag | | | | f_licensename |
| f_assetname | | | | f_licenseexpirationdate |
| | | | | f_licensequantity |
-------------------------- | | ---------------------------
| |
~~~~~~~~ ~~~~~~~~~~
| |
| ---------------------- |
| | TB_VENDORS | |
| ---------------------- |
~~> | f_vendorGUID | <~~
| f_vendorname |
----------------------
For a short example, I want to search for "Dell" across a few fields. I'd like to query against tb_assets
as well as against tb_licenses
for their vendor as well as those table's name columns. Note that I am aware that the example below is the outdated JOIN style - I'm only showing this here as an illustration of what I'm trying to accomplish rather than something that actually works to try to better illustrate the problem.
SELECT
f_assetvendor AS 'AssetVendor', f_assettag AS 'AssetTag', f_assetname AS 'AssetName'
f_licensevendor as 'LicenseVendor', f_licensename AS 'LicenseName', f_licenseexpirationdate AS 'LicenseExpirationDate', f_licensequantity AS 'LicenseQuantity'
FROM
tb_assets, tb_licenses
LEFT JOIN
tb_vendors ON f_vendorGUID = f_assetvendor
LEFT JOIN
tb_vendors ON f_vendorGUID = f_licensevendor
WHERE
f_vendorname LIKE '%Dell%' or f_assetname LIKE '%Dell%' or f_licensename LIKE '%Dell'
I'd prefer the results to appear like the following so that I can differentiate the items in code to show the results to the users in a favorable way. Note below that I have a result from each of the three WHERE statements above - hence the searching across all three tables.
AssetVendor AssetTag AssetName LicenseVendor LicenseName LicenseExpirationDate LicenseQuantity
=================================================================================================================================================================
Dell 1001 OptiPlex 790
Amazon 1002 Dell Printer
Dell Microsoft Office 2013 12/31/2016 15
Compellent Dell Storage Manager 02/31/2017 1
Worst case I have to run two distinct queries to accomplish this, but I'm hoping someone here could highlight a solution that lets me use a single query so I can learn something today. Forgive me if I didn't explain this very well, I've spend the better part of a day searching the Internet and can't seem to find exactly what I'm describing so my fear is that I'm describing it poorly because I'm surely not the first to want to accomplish something like this.
Thanks,
Matt
**Regarding my title stating "not UNION", I can't use a UNION here because with UNION column names for the final result set are taken from the first query, the columns must have the same data types, and both tables must have the same number of columns - none of which work for me here.