Hiren
Hiren

Reputation: 1391

Set Probability With SQL query

I have a table like the following:

    Number   Occurrence
     1         12
     2         30
     3         15
     4         20

I want to calculate the probability according to each number's occurrence and then want to get the number with highest probability.

I would like the SQL query for this.

Upvotes: 1

Views: 7529

Answers (2)

Bassam Mehanni
Bassam Mehanni

Reputation: 14944

TSQL:

SELECT TOP(1) Number
FROM Table_name
ORDER BY Occurance DESC

MySql:

SELECT Number
FROM Table_name
ORDER BY Occurance DESC
LIMIT 1;

Upvotes: 1

Joe Stefanelli
Joe Stefanelli

Reputation: 135848

SELECT Number, Occurance, 
       Occurance*1.0/(SELECT SUM(Occurance) FROM YourTable) AS Probability
    FROM YourTable
    ORDER BY Occurance DESC

Upvotes: 5

Related Questions