开发者

DB Select - Logic Meltdown

开发者 https://www.devze.com 2023-04-05 09:42 出处:网络
I\'ve been struggling with this one for a while. [sample table] ITEMGROUP --------- 4A 7A 3A 8A 7B 6B 9B 0C 4C 2C 5C

I've been struggling with this one for a while.

[sample table]

ITEM  GROUP
----  -----
4     A
7     A
3     A
8     A
7     B
6     B
9     B
0     C
4     C
2     C
5     C
4     C
7     C
5     D
9     D
2     E
7     E
1     E
4     E
7     F
3     F
9     F
6     F
8     G
4     H
5     H
3     开发者_StackOverflowH
9     H
0     H
8     H

I need an sql query that will tell me how many times each "ITEM", on a range of SELECT DISTINCT ITEM appeared with one another in a particular group.

IE: Items 4 and 8 appeared 2 times (groups A and H). Items 0 and 4 appeared 2 times (groups C and H). Items 7, 6 and 9 appeared appeared 2 times (groups B and F). ..And do on. It's ok to ignore "rogue" items that only appear in one group.

Can it be done? Thanks


This will work for pairs of items:

SELECT T1.item, T2.item, COUNT(*)
FROM yourTable T1
JOIN yourTable T2
ON T1.item < T2.item
AND T1.group = T2.group
GROUP BY T1.item, T2.item
0

精彩评论

暂无评论...
验证码 换一张
取 消