Search code examples
sqlsql-serversubquerywhere-clause

Add multiple criteria in select


I'm trying to complete my query with one additional criteria in select. This is how it looks now

SELECT
    c.hostname,
    s.executablepath,
    s.version,
    (
        SELECT TOP 1 filesize
        FROM [inventorydatabase].[inv].[softwareview]
        WHERE version = s.version
          AND PathName = s.ExecutablePath
    ) AS 'Size'
FROM tblApplications ca
INNER JOIN tblSoftware s ON s.Software = cs.Software
INNER JOIN tblComputer c ON ca.CompID  = c.CompID

So this query gives me out result if I have match in both version tables and both path tables.

...but I need to add additional criteria so that it give me result of 'Size' even if I have only match in both pathname = s.executablepath tables.

Because there some applications with no version available and it just gives me NULL under Size.

Hope someone help me to resolve this issue.


Solution

  • Assuming that "there some applications with no version available" means version is null, then the sub query would be:

    select top (1) filesize
    from [inventorydatabase].[inv].[softwareview]
    where pathName = s.ExecutablePath and (
        version = s.version or
        version is null
    )