简体   繁体   English

如何在MSSQL中将两个不同的列进行比较

[英]How to compare two different column for opposite of one another in mssql

I have a result set that is a relationship table. 我有一个关系表的结果集。 I am converting their data from one schema to another. 我正在将他们的数据从一种模式转换为另一种模式。 There relationship table as a relationship row for each person. 有关系表作为每个人的关系行。 so for example if bill knows bob there is a row that has contactid and contactid1. 因此,例如,如果Bill知道bob,则存在一行具有contactid和contactid1的行。

Contactid being the primary person in the relationship. Contactid是关系中的主要人员。 In their system Bob would need to have a separate row with him being the primary. 在他们的系统中,鲍勃需要有一个单独的行,以他为主行。

so the rows in their DB would look like this 所以他们数据库中的行看起来像这样

Contactid | Contactid1 | Description
123       | 456        |Gold Club Dinner
456       | 123        |Gold Club Dinner

Whereas in my schema the relationship looks like this 而在我的架构中,关系看起来像这样

Contactid | Contactid1 | Description
123       | 456        |Gold Club Dinner

I need to know a way to get the top 1 for each group in the first db so that I don't end up with thousands of duplicates in the table structure. 我需要知道一种在第一个数据库中获得每个组的前1名的方法,这样我就不会在表结构中得到成千上万个重复项。

Any ideas? 有任何想法吗?

Try this 尝试这个

select distinct
    case when contactID<contactID1 then contactID else contactID1 end , 
    case when contactID<contactID1 then contactID1 else contactID end ,
    description
from yourtable

I would argue, however, that both these designs are wrong, and should be normalised. 但是,我认为这两种设计都是错误的,应该规范化。

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

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