Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql find rows with different column values

My database table is (it has millions of records)

    sitename        rank     date
    facebook.com    1       2016-2-13
    gmail.com       2       2016-2-13
    yahoo.com       3       2016-2-13
    aol.com         4       2016-2-13
    facebook.com    1       2016-2-14
    gmail.com       2       2016-2-14
    yahoo.com       4       2016-2-14
    aol.com         3       2016-2-14

I want to find sites whose ranking has changed. in the above illustration yahoo and aol has changed. I tried several queries but cant get it to work.

like image 911
johnyyy Avatar asked Jun 13 '26 13:06

johnyyy


1 Answers

Its a simple select, group by and having query like this:

SELECT sitename,MAX(rank) - MIN(rank) as changed
FROM YourTable
GROUP BY sitename
HAVING COUNT(DISTINCT rank) > 1
like image 64
sagi Avatar answered Jun 15 '26 04:06

sagi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!