Sql 按具有切换值(字符列)的2列集合进行分组

Sql 按具有切换值(字符列)的2列集合进行分组,sql,Sql,我有一个绝望的问题:我想在sas中的proc sql中将数据按2列分组 这是一个例子 A B C -------------------------- John Smith 5 Smith John 4 Adam Gibbs 3 我想要的结果是: A B C --------------------------- John

我有一个绝望的问题:我想在sas中的proc sql中将数据按2列分组

这是一个例子

A           B          C
--------------------------    
John      Smith        5
Smith     John         4
Adam      Gibbs        3 
我想要的结果是:

A           B           C
---------------------------
John       Smith        9
Adam       Gibbs        3
我想对两列中可能具有切换值的值进行分组


无论值位于哪一侧,只要它们在两列中相同。这些是字符列,因此不能使用最小或最大函数。请帮忙!!!:

可以这样表示聚合:

proc sql;
    select (case when a < b then a else b end) as a,
           (case when a < b then b else a end) as b,
           sum(c)
    from t
    group by (case when a < b then a else b end),
             (case when a < b then b else a end);
请注意,这可能会返回不在原始数据中的对。如果这是一个问题,您可以使用:

proc sql;
    select a, b, sum(c)
    from ((select a, b
           from t
           where a < b or
                 not exists (select 1 from t t2 where t2.a = t.b and t2.b = t.a)
          ) union all
          (select b, a
           from t
           where b < a and
                 exists (select 1 from t t2 where t2.a = t.b and t2.b = t.a
          )
         ) t
    group by a, b;

到目前为止你试过什么?不知道怎么做。尝试标记具有相同A和B值的记录,但两个记录都会被明显标记。我想对号码进行分组,并删除同一组的一条记录。我希望John Smith或Smith John留下,但在C列中添加了两个记录中的值。我已经想了两天了!非常感谢戈登!它确实按照我想要的第二个解决方案工作。这是非常耗时,但工作!非常感谢你的帮助!