Reputation: 7394
I have a table like this:
col1 col2
2001 3001
2002 3001
2003 3001
2004 3002
2002 3003
2001 3003
i want to create it a query that finds out all the combination of items in col1 with a certain item in col1 if they occur to the same item in col2 and the frequency of occurrence. For example, if we want to explore item number "2001" in col1 the query should return:
col3 col4 col5
2001 2002 0.667
2001 2003 0.333
Can someone give me a hint? Thanks ahead.
Upvotes: 0
Views: 118
Reputation: 13521
Thinking of it this way, you are trying to relate the table to itself through col2, so we'll start off with a join onto itself:
select left.col1, right.col1/*, ...*/ from table left inner join table right on left.col2 = right.col2 where left.col1 = '2001' /* ... */
Now for your aggregate. You want to aggregate all right.col1 to get the count for that column:
select left.col1, right.col1, COUNT(*) from table left inner join table right on left.col2 = right.col2 where left.col1 = '2001' group by right.col2
Which I believe should come out with the raw counts. I believe you'll have to spin over the query and get a total to get the frequency.
As a note, this would be a lot more complicated if col1 weren't constant.
Edited to add: If you are looking for one query, instead of iterating over it in whatever language you are querying from, you would need to do this grouping twice:
SELECT abs_freq.col1, abs_freq.col2, abs_freq.freq / totals.total
FROM (SELECT
left.col1 AS col1,
COUNT(*) AS total
FROM TABLE LEFT
INNER JOIN TABLE RIGHT
ON left.col2 = right.col2
WHERE left.col1 = '2001'
GROUP BY left.col1
) totals
INNER JOIN (SELECT
left.col1 AS col1,
right.col1 AS col2,
COUNT(*) AS freq
FROM TABLE LEFT
INNER JOIN TABLE RIGHT
ON left.col2 = right.col2
WHERE left.col1 = '2001'
GROUP BY right.col2
) abs_freq
ON abs_freq.col1 = totals.col1
Upvotes: 1