简体   繁体   English

比较两行(都具有不同的 ID)并检查它们的列值是否完全相同。 所有行和列都在同一个表中

[英]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.我有一个名为“ROSTER”的表,在这个表中我有 22 列。

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.我想查询和比较该特定表的任何 2 行,目的是检查该 2 行的每一列的值是否完全相同。 ID column always has different values in each row so I will not include ID column for the comparing. ID 列在每一行中总是有不同的值,所以我不会包括 ID 列进行比较。 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.如果所有列值都相同:要么不显示任何内容(我更喜欢这个),要么只返回 2 行。

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 ID NAME姓名 TIME时间
1 1 N1 N1 0900 0900
2 2 N1 N1 0801 0801

Output: Output:

ID ID TIME时间
1 1 0900 0900
2 2 0801 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.注意:实际上我对 output 的任何结果或方式都可以,只要我能以任何方式知道这两行不一样。

What are the possible ways to do this in SQL Server?在 SQL 服务器中执行此操作的可能方法是什么?

I am using Microsoft SQL Server Management Studio 18, Microsoft SQL Server 2019-15.0.2080.9我正在使用 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.请根据 John Cappelletti 的想法尝试以下解决方案。 All credit goes to him.所有的功劳都归于他。

SQL 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 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:此处的一般方法可能是仅汇总整个表并报告计数的 state:

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较同一表中的列值/行 - How to compare column values/rows in same table 连接具有相同 id 但不同列值的两行值 - Concatenate two rows value with same id but different column values 表中有 2 个相同 ID 的元组,并且这两行的其他一些列的值不同 - There are 2 tuples for same ID in an table and the values of some other columns for those two rows are different SQL合并具有相同ID但不同列值的两行(Oracle) - SQL Merge two rows with same ID but different column values (Oracle) 如何将具有相同数据类型的两个不同列的结果放在一列中,以使两个表中的两行在目标表中都是唯一的 - How to put the result of two different columns with same data type within one column such that both rows from both tables are unique in target table Select 所有行在两列中具有相同的值,同时在另一列中具有不同的值? - Select all rows that have the same value in two columns, while simultaneously having different values in another column? 比较同一表格的行和列 - Compare rows and columns of same table 将同一张表的连续行中的同一列与多个ID进行比较 - Compare same column in consecutive rows in same table with multiple ID's 检查同一表的两个不同列中的相同值,然后在sql中进行比较 - Checking the same values in two different columns of same table and then compare in sql 如何在 SQL 服务器中比较具有相同 ID 的不同行之间的两个日期时间值? - How to compare two datetime values between different rows with the same ID in SQL Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM