Compare two rows (both with different ID) & check if their column values are exactly the same. All rows & columns are in the same table

0

I have a table named "ROSTER" and in this table I have 22 columns.

I want to query and compare any 2 rows of that particular table with the purpose to check if each column's values of that 2 rows are exactly the same. ID column always has different values in each row so I will not include ID column for the comparing. I will just use it to refer to what rows will be used for the comparison.

If all column values are the same: Either just display nothing (I prefer this one) or just return the 2 rows as it is.

If there are some column values not the same: Either display those column names only or display both the column name and its value (I prefer this one).

Example:

ROSTER Table:

ID NAME TIME
1 N1 0900
2 N1 0801

Output:

ID TIME
1 0900
2 0801

OR

Display "TIME"

Note: Actually I'm okay with whatever result or way of output as long as I can know in any way that the 2 rows are not the same.

What are the possible ways to do this in SQL Server?

I am using Microsoft SQL Server Management Studio 18, Microsoft SQL Server 2019-15.0.2080.9

sql sql-server tsql
2021-11-24 03:55:43
1

3

Please try the following solution based on the ideas of John Cappelletti. All credit goes to him.

SQL

-- DDL and sample data population, start
DECLARE @roster TABLE (ID INT PRIMARY KEY, NAME VARCHAR(10), TIME CHAR(4));
INSERT IGNORE INTO @roster (ID, NAME, TIME) VALUES
(1,'N1','0900'),
(2,'N1','0801')
-- DDL and sample data population, end

DECLARE @source INT = 1
    , @target INT = 2;

SELECT id AS source_id, @target AS target_id
      ,[key] AS [column]
      ,source_Value = MAX( CASE WHEN Src=1 THEN Value END)
      ,target_Value = MAX( CASE WHEN Src=2 THEN Value END)
FROM (
        SELECT Src=1
              ,id 
              ,B.*
         FROM @roster AS A
         CROSS APPLY ( SELECT [Key]
                             ,Value
                       FROM OpenJson( (SELECT A.* For JSON Path,Without_Array_Wrapper,INCLUDE_NULL_VALUES)) 
                     ) AS B
        WHERE id=@source
        UNION ALL
        SELECT Src=2
              ,id = @source
              ,B.*
         FROM @roster AS A
         CROSS APPLY ( SELECT [Key]
                             ,Value
                       FROM OpenJson( (SELECT A.* For JSON Path,Without_Array_Wrapper,INCLUDE_NULL_VALUES)) 
                     ) AS B
         WHERE id=@target
      ) AS A
GROUP BY id, [key]
HAVING MAX(CASE WHEN Src=1 THEN Value END)
     <> MAX(CASE WHEN Src=2 THEN Value END)
    AND [key] <> 'ID'   -- exclude this PK column
ORDER BY id, [key];

Output

+-----------+-----------+--------+--------------+--------------+
| source_id | target_id | column | source_Value | target_Value |
+-----------+-----------+--------+--------------+--------------+
|         1 |         2 | TIME   |         0900 |         0801 |
+-----------+-----------+--------+--------------+--------------+
2021-11-24 06:12:31

I was trying to check this out and quite confused by the [key] part. May I know what was this 'key' and how should this be defined or use?
Lars

{key] is a part of JSON. (1) Key, (2) value, and (3) type, all three of them are automatically generated by JSON. Check it out here: bertwagner.com/posts/the-ultimate-sql-server-json-cheat-sheet
Yitzhak Khabinsky

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................