1

I have query resulting me 1 column of strings, result example:

NAME:
-----
SOF
OTP
OTP
OTP
SOF
VIL
OTP
SOF
GGG

I want to be able to get SOF, OTP, VIL - the first 3 unique top,
I tried using DISTINCT and GROUP BY, but it is not working, the sorting is damaged..

The query building this result is :

SELECT DISTINCT d.adst 
FROM   (SELECT a.date              adate, 
               b.date              bdate, 
               a.price + b.price   total, 
               ( b.date - a.date ) days, 
               a.dst               adst 
        FROM   flights a 
               JOIN flights b 
                 ON a.dst = b.dst 
        ORDER  BY total) d 

I have "flights" table with details, and I need to get the 3 (=n) cheapest destinations.

Thanks

3
  • 2
    What RDBMS are you using?
    – Mureinik
    Oct 11, 2014 at 9:29
  • Edit your question with your query that generates this result.
    – Mihai
    Oct 11, 2014 at 9:33
  • can you also explain what you mean by "first 3 unique top"? Why would the query return VIL but not GGG (both have 1 occurrence)?
    – Mureinik
    Oct 11, 2014 at 9:34

6 Answers 6

3

This can easily be done using window functions:

select *
from (
       SELECT a.date as adate, 
              b.date as bdate, 
              a.price + b.price as total, 
              dense_rank() over (order by a.price + b.price) as rnk,
              b.date - a.date as days, 
              a.dst  as adst 
        FROM  flights a 
          JOIN flights b ON a.dst = b.dst 
) t
where rnk <= 3
order by rnk;

More details on window functions can be found in the manual:
http://www.postgresql.org/docs/current/static/tutorial-window.html

0

Find a way to do it.

I am selecting the DST and the PRICE, grouping by DST with MIN function on Price and limiting 3.

do I have better way to do it?

SELECT d.adst , min(d.total) mttl
FROM   (SELECT a.date              adate, 
               b.date              bdate, 
               a.price + b.price   total, 
               ( b.date - a.date ) days, 
               a.dst               adst 
    FROM   flights a 
           JOIN flights b 
             ON a.dst = b.dst 
    ORDER  BY total) d 
group by adst order by mttl;
0
select 
   name
from 
   testname 
where 
   name in (
   select distinct(name) from testname) 
group by name order by min(ctid) limit 3

SQLFIDDLE DEMO

0
0

You can tweak your query to return the correct result, by adding where days > 0 and limit 3 in the outer query like this:

select *
from 
(
   select 
     a.date adate, 
     b.date bdate, 
     (a.price + b.price) total, 
     (b.date - a.date) days , 
     a.dst adst
   from flights a 
   join flights b on a.dst = b.dst 
   order by total
) d
where days > 0
limit 3;

This assuming that the second entry is the return flight with date greater than the first entry. So that you got positive days difference.


Note that, your query without days > 0 will give you a cross join between the table and it self, for each flight you will get 4 rows, two with it self with days = 0 and other row with negative days so I used days > 0 to get the correct row.

I recommend that you add a new column, an Id Flight_Id as a primary key, and another foreign key something like From_Flight_Id. So the primary flight would have a null From_Flight_Id, and the returning flight will have a From_Flight_Id equal to the flight_id of the primary filght, this way you can join them properly instead.

-1
SELECT DISTINCT(`EnteredOn`) FROM `rm_pr_patients` Group By `EnteredOn`
2
  • sorry forget to add your attributes Oct 11, 2014 at 9:39
  • distinct is NOT a function. And using ` for identifiers is invalid sql. Plus the distinct is useless if you also do a goup by on the column. And there is no rm_pr_patients table in the question.
    – user330315
    Oct 11, 2014 at 9:43
-1
SELECT DISTINCT ON (column_name) FROM table_name order by name LIMIT 3;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.