Search
 
SCRIPT & CODE EXAMPLE
 

SQL

HOW TO FIND MEDIAN IN SQL FOR BOTH IDD AND EVEN

/*
HACKER RANK MYSQL SOLUTION.
*/
SET @rowindex := -1;
 
SELECT
   ROUND(AVG(N.LAT_N),4)
FROM
   (SELECT @rowindex:=@rowindex + 1 AS rowindex,
           STATION.LAT_N AS LAT_N
    FROM STATION
    ORDER BY STATION.LAT_N) AS N
WHERE
N.rowindex IN (FLOOR(@rowindex / 2) , CEIL(@rowindex / 2));
Comment

find the median in sql

# FOR TABLES WITH ODD NUMBER OF ROWS

# For column1 in table1 with 'n' number of rows. Where 'n' is an odd number.
# 1. Change all column1 and table1 to your column and table name.
# 2. Calculate (n/2)+0.5, where n=number of rows, and set it as LIMIT for t1.

SELECT *
FROM (SELECT column1
      FROM table1
      ORDER BY column1
      LIMIT (n/2)+0.5) AS t1
ORDER BY column1 DESC
LIMIT 1;
Comment

how to find median of a column sql

SET @rowindex := -1;
 
SELECT
   AVG(g.grade)
FROM
   (SELECT @rowindex:=@rowindex + 1 AS rowindex,
           grades.grade AS grade
    FROM grades
    ORDER BY grades.grade) AS g
WHERE
g.rowindex IN (FLOOR(@rowindex / 2) , CEIL(@rowindex / 2));
Comment

PREVIOUS NEXT
Code Example
Sql :: export data from excel to sql server 
Sql :: subquery in Insert 
Sql :: connect to remote mysql server 
Sql :: union and union all 
Sql :: postgresql auto increment not working 
Sql :: mysql 
Sql :: SQL sort on a calculation 
Sql :: delete in sql server 
Sql :: insert into table with only identity column 
Sql :: backup table mssql 
Sql :: postgresql get random data from table 
Sql :: mysql match in serialized data 
Sql :: retornar apenas o ano mysql date 
Sql :: enlever les doubles espaces dans les tables postgresql 
Csharp :: c# mark as deprecated 
Csharp :: unity how to change max fps 
Csharp :: c# how to run external program 
Csharp :: c# find start and end of month from object date 
Csharp :: open url in c# 
Csharp :: c# remove last character from string 
Csharp :: c# error messagebox 
Csharp :: unity on mousewheel down 
Csharp :: asp.net core .gitignore 
Csharp :: unity change text 
Csharp :: smooth rotation unity 
Csharp :: unity C# catch index out or range exception 
Csharp :: Error inflating class android.support.constraint.ConstraintLayout 
Csharp :: bitmap to byte array c# 
Csharp :: isprime c# 
Csharp :: C# list to string one line 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =