Skip to content
Advertisement

MSSQL COALESCE function with multiple non null values

I am trying to get data (T_Stamp, Destination, Weight, Line) from two tables using variable time intervals and a destination selection. This is going into an Ignition SCADA. My SQL code below works for most cases except when there are entries into both tables with the same timestamp. In those cases it shows only the data from table A. This is giving me results with the incorrect destination. I understand that the COALESCE function is returning the first Non-null value, but I don’t know how else to write this logic.

Expected results:

t_stamp Destination Weight Line
10:05:01 1 30.01 A
10:05:05 1 25.11 B
10:05:07 1 26.32 B

Actual Results:

t_stamp Destination Weight Line
10:05:01 1 30.01 A
10:05:05 1 25.11 B
10:05:07 2 25.46 A

Sample Data Table A: | t_stamp | Destination | A_Weight | | ——– | ———– | ——– | | 10:05:01 | 1 | 30.01 | | 10:05:07 | 2 | 32.32 |

Table B: | t_stamp | Destination | B_Weight | | ——– | ———– | ——– | | 10:05:03 | 1 | 24.01 | | 10:05:07 | 1 | 26.46 |

Advertisement

Answer

I suspect what you want is something like the following, which uses a UNION instead of a JOIN.

This gets the results from a with the correct destination and timestamps, gets the results from b with the correct destination and timestamps, then sorts them all together by timestamp. So if a timestamp is in a and b, both rows are returned one after the other. I used UNION ALL rather than just UNION since the hardcoded ‘A’/’B’ in the Line column means there won’t be duplicates and the UNION ALL could be more efficient.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement