[英]SQL Count elements in pairs
I have a simple table 我有一张简单的桌子
CREATE TABLE aaa AS
(
ogid integer NOT NULL,
ocolor character varying(80) NOT NULL,
vgid integer NOT NULL,
vcolor character varying(80) NOT NULL,
)
aa contains some values, like in this example aa包含一些值,例如本示例
Ogid OColor Vgid VColor
1 v 1 v
1 v 5 r
1 v 8 g
2 r 5 r
3 g 7 r
4 g 5 r
5 r 7 r
5 r 9 g
6 g 6 g
I need to count the number of elements in every couple (OColor, VColor) grouping together simmetric couples ( for example the elements of gr and rg go together ). 我需要计算每对夫妇(OColor,VColor)中将模拟对分组在一起的元素数(例如gr和rg的元素合在一起)。
I need a result similar to: 我需要类似以下结果:
OColor Vcolor nelement
b b 10
b g 16
g v 2
If I understand correctly, you can just use least()
and greatest()
: 如果我理解正确,则可以只使用
least()
和greatest()
:
select least(ocolor, vcolor), greatest(ocolor, vcolor), count(*)
from table t
group by least(ocolor, vcolor), greatest(ocolor, vcolor);
However, your question is confusing because the results seem to have nothing to do with the sample data. 但是,您的问题令人困惑,因为结果似乎与样本数据无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.