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 :: mysql public key retrieval is not allowed 
Sql :: oracle asynchronous update 
Sql :: mysql persistence.xml 
Sql :: upper and lower in oracle sql 
Sql :: oracle explain plan 
Sql :: what is mysql_pconnect 
Sql :: mysql data types 
Sql :: show processlist mysql full query 
Sql :: use of now() in mysql 
Sql :: mysql create table like 
Sql :: where date major today mysql 
Sql :: like sql 
Sql :: uppercase and lowercase in sql 
Sql :: oracle db get table sizes 
Sql :: how to create table in sql 
Sql :: purge undo tablespace usage 
Sql :: delete a record from a table sqlite3 
Sql :: show data in table postgres 
Sql :: postgresql update between 2 tables 
Sql :: create table sqlite 
Sql :: pl sql disable trigger 
Sql :: postgres set default value 
Sql :: input in mysql 
Sql :: what is integrity constraints 
Sql :: select and condition in sql 
Sql :: calculate distance between two latitude longitude points sql 
Sql :: how to import large sql file in phpmyadmin in ubuntu 
Sql :: change database name psql 8 
Sql :: postgres float to int 
Sql :: mysql cashing error 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =