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.
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:
Now for your aggregate. You want to aggregate all right.col1 to get the count for that column:
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: