简体   繁体   English

SQL Server-比较同一表中的值

[英]SQL Server - Compare values from the same table

In SQL Server, I have one table with following data (tblUserSettings): 在SQL Server中,我有一个包含以下数据(tblUserSettings)的表:

| CountryID | CityID  | UserType | Value1 | Value2 | Value3 |
| 9         | 3       | 1        | 5      | 5      | 5      |
| 9         | 3       | 2        | NULL   | NULL   | NULL   |
| 9         | 3       | 3        | 5      | 5      | 5      |
| 9         | 3       | 4        | 5      | 5      | 5      |
| 9         | 20      | 1        | 5      | 5      | 5      |
| 9         | 20      | 2        | NULL   | NULL   | NULL   |
| 9         | 20      | 3        | 5      | 5      | 5      |
| 9         | 20      | 4        | 0      | 0      | 0      |

I need to compare all the values for all UserTypes from CityID = 20 with all the values for corresponding UserTypes from CityID = 3. The CountryID = 9. The columns to compare are: Value1, Value2, Value3. 我需要将CityID = 20的所有UserTypes的所有值与CityID = 3的对应UserTypes的所有值进行比较。CountryID =9。要比较的列是:Value1,Value2,Value3。

I just need to know if all of them are matched to each other or not. 我只需要知道它们是否彼此匹配即可。 I tried to do something as follows: 我尝试执行以下操作:

SELECT CASE WHEN ISNULL(t1.Value1, 0) = ISNULL(t2.Value1, 0) THEN 1 ELSE 0 END AS Match1,
        CASE WHEN ISNULL(t1.Value2, 0) = ISNULL(t2.Value2, 0) THEN 1 ELSE 0 END AS Match2,
        CASE WHEN ISNULL(t1.Value3, 0) = ISNULL(t2.Value3, 0) THEN 1 ELSE 0 END AS Match3
FROM tblUserSettings t1
INNER JOIN tblUserSettings t2 ON t1.CountryID = t2.CountryID 
           AND t1.UserType = t2.UserType
           AND t1.CityID = 3
           AND t2.CityID = 20
WHERE t1.CountryID = 9

And it gives me following result which I have to process further to define if everything matches or not. 它给了我以下结果,我必须进一步处理以定义是否所有内容都匹配。

| Match1 | Match2  | Match3 |
| 1      | 1       | 1      |
| 1      | 1       | 1      |
| 1      | 1       | 1      |
| 0      | 0       | 0      |

Can I do this in a way to have only one column and row in output - just receive either 1 for all the matches or 0 if at least one doesn't match? 我能以某种方式使输出中只有一列和一行吗?-对于所有匹配项,只接收1;如果至少一个不匹配,则只接收0?

If you are looking to get only one column with 1 when all the values match and 0 if atleast one doesn't, use, 如果您希望在所有值都匹配时只获得包含1一列,如果不匹配则返回0 ,请使用,

SELECT 
CASE WHEN ISNULL(t1.Value1, 0) = ISNULL(t2.Value1, 0) 
      AND ISNULL(t1.Value2, 0) = ISNULL(t2.Value2, 0) 
      AND ISNULL(t1.Value3, 0) = ISNULL(t2.Value3, 0) 
THEN 1 ELSE 0 END AS Match
FROM tblUserSettings t1
INNER JOIN tblUserSettings t2 ON t1.CountryID = t2.CountryID 
           AND t1.UserType = t2.UserType
           AND t1.CityID = 3
           AND t2.CityID = 20
WHERE t1.CountryID = 9

If you are looking to compare all cities rather than just two you should be able to do this by grouping rather than joining. 如果您想比较所有城市而不只是两个城市,则应该能够通过分组而不是加入来进行比较。

Something like: 就像是:

SELECT 
CASE WHEN
       max(Value1)-min(Value1) = 0
       AND max(Value2)-min(Value2) = 0
       AND max(Value3)-min(Value3) = 0
THEN 1 ELSE 0 AS Match
FROM tblUserSettings
GROUP BY CountryID,UserType

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

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