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
3条答案
按热度按时间yyhrrdl81#
Please try the following solution based on the ideas of John Cappelletti. All credit goes to him.
SQL
Output
dsekswqp2#
A general approach here might be to just aggregate over the entire table and report the state of the counts:
s4n0splo3#
for future searchers - perfect implementation of Yitzhak Khabinsky answer / John Cappelletti idea
with simple usage