简体   繁体   中英

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

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

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 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 |
+-----------+-----------+--------+--------------+--------------+

A general approach here might be to just aggregate over the entire table and report the state of the counts:

SELECT
    CASE WHEN COUNT(DISTINCT ID) = COUNT(*)   THEN 'Yes' ELSE 'No' END AS [ID same],
    CASE WHEN COUNT(DISTINCT NAME) = COUNT(*) THEN 'Yes' ELSE 'No' END AS [NAME same],
    CASE WHEN COUNT(DISTINCT TIME) = COUNT(*) THEN 'Yes' ELSE 'No' END AS [TIME same]
FROM yourTable;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM