我有一張桌子:
ColumnA, ColumnB
C,F
F,C
A,C
C,A
C,F
C,F
C,A
C,F
F,C
X,Y
我需要查詢來獲得這個輸出:
ColumnA, CoulmnB, Count
C,F,5
A,C,3
X,Y,1
uj5u.com熱心網友回復:
使用least()和greatest()始終按字母順序獲取列對值:
select least(ColumnA, ColumnB), greatest(ColumnA, ColumnB), count(*) cnt
from tablename
group by least(ColumnA, ColumnB), greatest(ColumnA, ColumnB)
order by cnt desc
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/397706.html
