我有一个表,其中列调用者和被调用者具有以下值:

caller callee
999 888
888 999
999 555
555 333
555 999

现在我只希望返回一行
caller1    caller2  count
999        888      2
999        555      1
555        333      1
555        999      2

最佳答案

SELECT CASE
         WHEN caller < callee THEN callee
         ELSE caller
       END      AS caller1,
       CASE
         WHEN caller < callee THEN caller
         ELSE callee
       END      AS caller2,
       Count(*) AS [Count]
FROM   YourTable
GROUP  BY CASE
            WHEN caller < callee THEN callee
            ELSE caller
          END,
          CASE
            WHEN caller < callee THEN caller
            ELSE callee
          END

关于sql - 如果表中的两列都具有相同的值,则获取单行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12859736/

10-12 12:52