SSMS Linked Server from PostgreSQL query fails when adding WHERE clause

43 views Asked by At

I have Linked server from PostgreSQL 12 to my SSMS and trying to join two PostgreSQL tables and insert the result to my table with the stored procedure. The problem I have is when adding a WHERE clause it starts displaying NULLs in the second table and I know there are values in there. Please see the examples below:

First query:

SELECT
    a.hardware_id AS [HardwareID], 
    a.created_at AS CreatedAt,
    ISNULL(b.serial, NULL) AS [DiskSerial],
    ISNULL(b.model, NULL) AS [DiskModel],
    ISNULL(b.vendor, NULL) AS [DiskMake],
    ISNULL(b.type_display, NULL) AS [DiskType],
    ISNULL(b.transport, NULL) AS [DiskTransport],
    ISNULL(b.status_display, NULL) AS [DiskStatus],
    ISNULL(b.capacity_display, NULL) AS [DiskCapacity],
    ISNULL(CONVERT(DATETIME, b.erase_finish_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseFinishTime],
    ISNULL(b.device_id, NULL) AS [DiskEraseID],
    ISNULL(b.erase_algorithm_display, NULL) AS [DiskEraseMethod],
    ISNULL(b.erase_level, NULL) AS [DiskEraseLevel],
    ISNULL(CONVERT(DATETIME, b.erase_start_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseStartTime],
    ISNULL(b.erase_result_display, NULL) AS [DiskEraseResult]
FROM [LinkedServerName].[DatabaseName].[public].[hardware] AS a
LEFT JOIN [LinkedServerName].[DatabaseName].[public].[disk_device] AS b 
    ON a.hardware_id = b.hardware_id

Running this query returns 3393 rows: Result 3393 rows

Now when I add a WHERE clause to the query the second table is displayed as NULLs apart from the first record

Second query added WHERE:

SELECT
    a.hardware_id AS [HardwareID], 
    a.created_at AS CreatedAt,
    ISNULL(b.serial, NULL) AS [DiskSerial],
    ISNULL(b.model, NULL) AS [DiskModel],
    ISNULL(b.vendor, NULL) AS [DiskMake],
    ISNULL(b.type_display, NULL) AS [DiskType],
    ISNULL(b.transport, NULL) AS [DiskTransport],
    ISNULL(b.status_display, NULL) AS [DiskStatus],
    ISNULL(b.capacity_display, NULL) AS [DiskCapacity],
    ISNULL(CONVERT(DATETIME, b.erase_finish_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseFinishTime],
    ISNULL(b.device_id, NULL) AS [DiskEraseID],
    ISNULL(b.erase_algorithm_display, NULL) AS [DiskEraseMethod],
    ISNULL(b.erase_level, NULL) AS [DiskEraseLevel],
    ISNULL(CONVERT(DATETIME, b.erase_start_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseStartTime],
    ISNULL(b.erase_result_display, NULL) AS [DiskEraseResult]
FROM [LinkedServerName].[DatabaseName].[public].[hardware] AS a
LEFT JOIN [LinkedServerName].[DatabaseName].[public].[disk_device] AS b 
    ON a.hardware_id = b.hardware_id WHERE a.created_at >= '2023-10-10'

Running this query returns filtered data of 1551 rows but without values from the second table, I want to get this number of rows (filtered by a.created_at) with the data populated for the rest of the columns: enter image description here

I tried changing WHERE clause to AND and this does not filter results by a.created_at

Third query:

SELECT
    a.hardware_id AS [HardwareID], 
    a.created_at AS CreatedAt,
    ISNULL(b.serial, NULL) AS [DiskSerial],
    ISNULL(b.model, NULL) AS [DiskModel],
    ISNULL(b.vendor, NULL) AS [DiskMake],
    ISNULL(b.type_display, NULL) AS [DiskType],
    ISNULL(b.transport, NULL) AS [DiskTransport],
    ISNULL(b.status_display, NULL) AS [DiskStatus],
    ISNULL(b.capacity_display, NULL) AS [DiskCapacity],
    ISNULL(CONVERT(DATETIME, b.erase_finish_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseFinishTime],
    ISNULL(b.device_id, NULL) AS [DiskEraseID],
    ISNULL(b.erase_algorithm_display, NULL) AS [DiskEraseMethod],
    ISNULL(b.erase_level, NULL) AS [DiskEraseLevel],
    ISNULL(CONVERT(DATETIME, b.erase_start_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseStartTime],
    ISNULL(b.erase_result_display, NULL) AS [DiskEraseResult]
FROM [LinkedServerName].[DatabaseName].[public].[hardware] AS a
LEFT JOIN [LinkedServerName].[DatabaseName].[public].[disk_device] AS b 
    ON a.hardware_id = b.hardware_id AND a.created_at >= '2023-10-10'

Running this query returns 3270 rows for some reason and filtering does not work as well: enter image description here

0

There are 0 answers