Compare every field in table to every other field in same table

Viewed 152

Imagine a table with only one column.

+------+
|  v   |
+------+
|0.1234|
|0.8923|
|0.5221|
+------+

I want to do the following for row K:

  1. Take row K=1 value: 0.1234
  2. Count how many values in the rest of the table are less than or equal to value in row 1.

Iterate through all rows Output should be:

+------+-------+
| v    |output |
+------+-------+
|0.1234|   0   |
|0.8923|   2   | 
|0.5221|   1   | 
+------+-------+

Quick Update I was using this approach to compute a statistic at every value of v in the above table. The cross join approach was way too slow for the size of data I was dealing with. So, instead I computed my stat for a grid of v values and then matched them to the vs in the original data. v_table is the data table from before and stat_comp is the statistics table.

AS SELECT t1.* 
,CASE WHEN v<=1.000000 THEN pr_1 
WHEN v<=2.000000 AND v>1.000000 THEN pr_2 
FROM v_table  AS t1 
LEFT OUTER JOIN stat_comp AS t2
4 Answers
Related