Count the number of pairs in SQL -


i have column , count number of unique pairings of elements within column in sql, example, in col 1 number of unique pairings should 6: ([1,2],[1,3],[1,4],[2,3],[2,4],[3,4]). thanks!

    col 1,     1     2     3     4 

consider scenario in have dulpicates values in table

col1 1 1 2 3  4 5 

the total number of unique combinations 10:([1,2],[1,3],[1,4],[1,5],[2,3],[2,4],[2,5],[3,4][3,5],[4,5]).

but given query below giving me count of 14 because of dulplicate 1 counting 4 pairs [1,2],[1,3],[1,4],[1,5] twice.

select count(*) table t1 join  table t2  on t1.col1 < t2.col1; 

to modify defect have following query ensures duplicates removed , correct output.the table name have chosen countunique can store integer values in in column named col1.

select count(*) (select distinct col1 countunique) t1 join (select distinct col1 countunique) t2 on t1.col1<t2.col1 

sql fiddle reference sqlfiddle

hope answers question.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -