Giter Site home page Giter Site logo

top3distance's Introduction

Top3distance

The top3distance app can do the following:

  • store users location (latitude, longitude)
  • populate a database with generated random user locations (5 different nationalities, 1000 users for each)
  • has a query to show top 3 the most distant users for each nationality

How to run this app

  1. install Docker and Docker Compose
  2. run the following commands:
  $ git clone https://github.com/dastier/top3distance.git
  $ cd top3distance/
  $ docker-compose up --build

Query to get top 3 the most distant users for each nationality

Screenshot

WITH all_distances AS
  (SELECT DISTINCT ON (distance) nationality,
                      id AS user_id_1,
                      secondid AS user_id_2,
                      distance AS "distance (в метрах)"
   FROM
     (SELECT name,
             nationality,
             nat_points.id,
             (point(u.longitude, u.latitude)<@>point(nat_points.longitude, nat_points.latitude)) * 1609.344 AS distance,
             u.id AS secondid
      FROM users u,
           LATERAL
        (SELECT id,
                latitude,
                longitude
         FROM users
         WHERE nationality = u.nationality) AS nat_points
      WHERE u.id <> nat_points.id
        AND nationality = u.nationality
      ORDER BY distance DESC) AS g1
   ORDER BY distance DESC)
  (SELECT *
   FROM all_distances
   WHERE nationality = 'Belarusian'
   ORDER BY "distance (в метрах)" DESC FETCH FIRST 3 ROWS ONLY)
UNION
  (SELECT *
   FROM all_distances
   WHERE nationality = 'Polish'
   ORDER BY "distance (в метрах)" DESC FETCH FIRST 3 ROWS ONLY)
UNION
  (SELECT *
   FROM all_distances
   WHERE nationality = 'Lithuanian'
   ORDER BY "distance (в метрах)" DESC FETCH FIRST 3 ROWS ONLY)
UNION
  (SELECT *
   FROM all_distances
   WHERE nationality = 'Latvian'
   ORDER BY "distance (в метрах)" DESC FETCH FIRST 3 ROWS ONLY)
UNION
  (SELECT *
   FROM all_distances
   WHERE nationality = 'Ukrainian'
   ORDER BY "distance (в метрах)" DESC FETCH FIRST 3 ROWS ONLY)
ORDER BY "distance (в метрах)" DESC,
         nationality ASC;

top3distance's People

Contributors

dastier avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.