[英]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 ![]() |
NAME![]() |
TIME![]() |
---|---|---|
1 ![]() |
N1 ![]() |
0900 ![]() |
2 ![]() |
N1 ![]() |
0801 ![]() |
Output: 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.注意:实际上我对 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.