SQL - Find the horses that have run at ONLY 1 specific track










3














I have some horse data in a table and am practicing SQL while following my hobby. I would like to find an elegant way to solve this problem. Right now I have this convoluted way of getting the answer but I know there HAS to be an easier way.



Description:



I'll make it really simple (Assume these 8 rows are the entire table). I have a table with 4 columns. HORSE_ID, NAME, TRACK, Date



A horse might run at one track or many different tracks. The end goal is to find what only the horses who have ran/campaigned at only one specific track....in this case, I want to see the horses that have run all their races at SA (Santa Anita)



HORSE_ID NAME TRACK DATE 
1 JUSTIFY SA FEB-2018
2 JUSTIFY PIM MAY-2018
3 JUSTIFY BEL JUN-2018
4 KANTHAKA SA DEC-2017
5 KANTHAKA SA JAN-2018
7 THREE RULES GP JUL-2016
8 DABSTER SA JAN-2018


So if I ran this query with this data, the only horses I would expect to see are KANTHAKA, and DABSTER because they are the only horses that only ran all their races at Santa Anita track. So say next month KANTHAKA ran at ANOTHER DIFFERENT track, then the next time the query was run, only DABSTER would show up.



Does this make sense?










share|improve this question



















  • 1




    welcome to StackOverflow; please provide a minimal, complete, and verifiable example, to show us what you've tried, because that context is very helpful in knowing how to frame an answer that will be the most helpful to you
    – landru27
    Nov 12 '18 at 3:54










  • You may want to look at GROUP BY and HAVING.
    – fubar
    Nov 12 '18 at 3:55










  • It's funny how often people forget to give the name of the table, which makes it hard to compare answers because everyone has to invent a name for the table! The 'horse ID' column is odd, too, because it doesn't identify the horse; it identifies a horse that ran on a track on a date.
    – Jonathan Leffler
    Nov 12 '18 at 4:04











  • John you are 100% right. I have to rethink that.
    – Robin
    Nov 12 '18 at 4:32















3














I have some horse data in a table and am practicing SQL while following my hobby. I would like to find an elegant way to solve this problem. Right now I have this convoluted way of getting the answer but I know there HAS to be an easier way.



Description:



I'll make it really simple (Assume these 8 rows are the entire table). I have a table with 4 columns. HORSE_ID, NAME, TRACK, Date



A horse might run at one track or many different tracks. The end goal is to find what only the horses who have ran/campaigned at only one specific track....in this case, I want to see the horses that have run all their races at SA (Santa Anita)



HORSE_ID NAME TRACK DATE 
1 JUSTIFY SA FEB-2018
2 JUSTIFY PIM MAY-2018
3 JUSTIFY BEL JUN-2018
4 KANTHAKA SA DEC-2017
5 KANTHAKA SA JAN-2018
7 THREE RULES GP JUL-2016
8 DABSTER SA JAN-2018


So if I ran this query with this data, the only horses I would expect to see are KANTHAKA, and DABSTER because they are the only horses that only ran all their races at Santa Anita track. So say next month KANTHAKA ran at ANOTHER DIFFERENT track, then the next time the query was run, only DABSTER would show up.



Does this make sense?










share|improve this question



















  • 1




    welcome to StackOverflow; please provide a minimal, complete, and verifiable example, to show us what you've tried, because that context is very helpful in knowing how to frame an answer that will be the most helpful to you
    – landru27
    Nov 12 '18 at 3:54










  • You may want to look at GROUP BY and HAVING.
    – fubar
    Nov 12 '18 at 3:55










  • It's funny how often people forget to give the name of the table, which makes it hard to compare answers because everyone has to invent a name for the table! The 'horse ID' column is odd, too, because it doesn't identify the horse; it identifies a horse that ran on a track on a date.
    – Jonathan Leffler
    Nov 12 '18 at 4:04











  • John you are 100% right. I have to rethink that.
    – Robin
    Nov 12 '18 at 4:32













3












3








3







I have some horse data in a table and am practicing SQL while following my hobby. I would like to find an elegant way to solve this problem. Right now I have this convoluted way of getting the answer but I know there HAS to be an easier way.



Description:



I'll make it really simple (Assume these 8 rows are the entire table). I have a table with 4 columns. HORSE_ID, NAME, TRACK, Date



A horse might run at one track or many different tracks. The end goal is to find what only the horses who have ran/campaigned at only one specific track....in this case, I want to see the horses that have run all their races at SA (Santa Anita)



HORSE_ID NAME TRACK DATE 
1 JUSTIFY SA FEB-2018
2 JUSTIFY PIM MAY-2018
3 JUSTIFY BEL JUN-2018
4 KANTHAKA SA DEC-2017
5 KANTHAKA SA JAN-2018
7 THREE RULES GP JUL-2016
8 DABSTER SA JAN-2018


So if I ran this query with this data, the only horses I would expect to see are KANTHAKA, and DABSTER because they are the only horses that only ran all their races at Santa Anita track. So say next month KANTHAKA ran at ANOTHER DIFFERENT track, then the next time the query was run, only DABSTER would show up.



Does this make sense?










share|improve this question















I have some horse data in a table and am practicing SQL while following my hobby. I would like to find an elegant way to solve this problem. Right now I have this convoluted way of getting the answer but I know there HAS to be an easier way.



Description:



I'll make it really simple (Assume these 8 rows are the entire table). I have a table with 4 columns. HORSE_ID, NAME, TRACK, Date



A horse might run at one track or many different tracks. The end goal is to find what only the horses who have ran/campaigned at only one specific track....in this case, I want to see the horses that have run all their races at SA (Santa Anita)



HORSE_ID NAME TRACK DATE 
1 JUSTIFY SA FEB-2018
2 JUSTIFY PIM MAY-2018
3 JUSTIFY BEL JUN-2018
4 KANTHAKA SA DEC-2017
5 KANTHAKA SA JAN-2018
7 THREE RULES GP JUL-2016
8 DABSTER SA JAN-2018


So if I ran this query with this data, the only horses I would expect to see are KANTHAKA, and DABSTER because they are the only horses that only ran all their races at Santa Anita track. So say next month KANTHAKA ran at ANOTHER DIFFERENT track, then the next time the query was run, only DABSTER would show up.



Does this make sense?







sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 4:18









Jonathan Leffler

561k896661018




561k896661018










asked Nov 12 '18 at 3:49









RobinRobin

182




182







  • 1




    welcome to StackOverflow; please provide a minimal, complete, and verifiable example, to show us what you've tried, because that context is very helpful in knowing how to frame an answer that will be the most helpful to you
    – landru27
    Nov 12 '18 at 3:54










  • You may want to look at GROUP BY and HAVING.
    – fubar
    Nov 12 '18 at 3:55










  • It's funny how often people forget to give the name of the table, which makes it hard to compare answers because everyone has to invent a name for the table! The 'horse ID' column is odd, too, because it doesn't identify the horse; it identifies a horse that ran on a track on a date.
    – Jonathan Leffler
    Nov 12 '18 at 4:04











  • John you are 100% right. I have to rethink that.
    – Robin
    Nov 12 '18 at 4:32












  • 1




    welcome to StackOverflow; please provide a minimal, complete, and verifiable example, to show us what you've tried, because that context is very helpful in knowing how to frame an answer that will be the most helpful to you
    – landru27
    Nov 12 '18 at 3:54










  • You may want to look at GROUP BY and HAVING.
    – fubar
    Nov 12 '18 at 3:55










  • It's funny how often people forget to give the name of the table, which makes it hard to compare answers because everyone has to invent a name for the table! The 'horse ID' column is odd, too, because it doesn't identify the horse; it identifies a horse that ran on a track on a date.
    – Jonathan Leffler
    Nov 12 '18 at 4:04











  • John you are 100% right. I have to rethink that.
    – Robin
    Nov 12 '18 at 4:32







1




1




welcome to StackOverflow; please provide a minimal, complete, and verifiable example, to show us what you've tried, because that context is very helpful in knowing how to frame an answer that will be the most helpful to you
– landru27
Nov 12 '18 at 3:54




welcome to StackOverflow; please provide a minimal, complete, and verifiable example, to show us what you've tried, because that context is very helpful in knowing how to frame an answer that will be the most helpful to you
– landru27
Nov 12 '18 at 3:54












You may want to look at GROUP BY and HAVING.
– fubar
Nov 12 '18 at 3:55




You may want to look at GROUP BY and HAVING.
– fubar
Nov 12 '18 at 3:55












It's funny how often people forget to give the name of the table, which makes it hard to compare answers because everyone has to invent a name for the table! The 'horse ID' column is odd, too, because it doesn't identify the horse; it identifies a horse that ran on a track on a date.
– Jonathan Leffler
Nov 12 '18 at 4:04





It's funny how often people forget to give the name of the table, which makes it hard to compare answers because everyone has to invent a name for the table! The 'horse ID' column is odd, too, because it doesn't identify the horse; it identifies a horse that ran on a track on a date.
– Jonathan Leffler
Nov 12 '18 at 4:04













John you are 100% right. I have to rethink that.
– Robin
Nov 12 '18 at 4:32




John you are 100% right. I have to rethink that.
– Robin
Nov 12 '18 at 4:32












2 Answers
2






active

oldest

votes


















5














Try using GROUP BY with HAVING:



SELECT NAME
FROM yourTable
GROUP BY NAME
HAVING MIN(TRACK) = MAX(TRACK);


Writing the HAVING clause as above is preferable to writing HAVING COUNT(DISTINCT TRACK) = 1. The reason for this is that the above query can make use of an index on (NAME, TRACK).



If in addition you wish to restrict to a single track, then we can try:



SELECT NAME
FROM yourTable
GROUP BY NAME
HAVING MIN(TRACK) = MAX(TRACK) AND MIN(TRACK) = 'SA';





share|improve this answer






















  • This is great Tim and does what I wanted! In addition to providing all horses that have run at one single track (your first query). The second one let's me get a filtered list by track (in this case SA). Thank you sir.
    – Robin
    Nov 12 '18 at 4:28


















1














You can do a subquery requesting the count of distinct tracks to be 1 using GROUP BY and HAVING COUNT DISTINCT and then select those WHERE the track is 'SA':



SELECT NAME
FROM
(SELECT NAME, MIN(TRACK) as TRACK
FROM HORSES
GROUP BY 1
HAVING COUNT (DISTINCT TRACK) = 1) horses_one_race
WHERE
TRACK = 'SA'





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255721%2fsql-find-the-horses-that-have-run-at-only-1-specific-track%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    Try using GROUP BY with HAVING:



    SELECT NAME
    FROM yourTable
    GROUP BY NAME
    HAVING MIN(TRACK) = MAX(TRACK);


    Writing the HAVING clause as above is preferable to writing HAVING COUNT(DISTINCT TRACK) = 1. The reason for this is that the above query can make use of an index on (NAME, TRACK).



    If in addition you wish to restrict to a single track, then we can try:



    SELECT NAME
    FROM yourTable
    GROUP BY NAME
    HAVING MIN(TRACK) = MAX(TRACK) AND MIN(TRACK) = 'SA';





    share|improve this answer






















    • This is great Tim and does what I wanted! In addition to providing all horses that have run at one single track (your first query). The second one let's me get a filtered list by track (in this case SA). Thank you sir.
      – Robin
      Nov 12 '18 at 4:28















    5














    Try using GROUP BY with HAVING:



    SELECT NAME
    FROM yourTable
    GROUP BY NAME
    HAVING MIN(TRACK) = MAX(TRACK);


    Writing the HAVING clause as above is preferable to writing HAVING COUNT(DISTINCT TRACK) = 1. The reason for this is that the above query can make use of an index on (NAME, TRACK).



    If in addition you wish to restrict to a single track, then we can try:



    SELECT NAME
    FROM yourTable
    GROUP BY NAME
    HAVING MIN(TRACK) = MAX(TRACK) AND MIN(TRACK) = 'SA';





    share|improve this answer






















    • This is great Tim and does what I wanted! In addition to providing all horses that have run at one single track (your first query). The second one let's me get a filtered list by track (in this case SA). Thank you sir.
      – Robin
      Nov 12 '18 at 4:28













    5












    5








    5






    Try using GROUP BY with HAVING:



    SELECT NAME
    FROM yourTable
    GROUP BY NAME
    HAVING MIN(TRACK) = MAX(TRACK);


    Writing the HAVING clause as above is preferable to writing HAVING COUNT(DISTINCT TRACK) = 1. The reason for this is that the above query can make use of an index on (NAME, TRACK).



    If in addition you wish to restrict to a single track, then we can try:



    SELECT NAME
    FROM yourTable
    GROUP BY NAME
    HAVING MIN(TRACK) = MAX(TRACK) AND MIN(TRACK) = 'SA';





    share|improve this answer














    Try using GROUP BY with HAVING:



    SELECT NAME
    FROM yourTable
    GROUP BY NAME
    HAVING MIN(TRACK) = MAX(TRACK);


    Writing the HAVING clause as above is preferable to writing HAVING COUNT(DISTINCT TRACK) = 1. The reason for this is that the above query can make use of an index on (NAME, TRACK).



    If in addition you wish to restrict to a single track, then we can try:



    SELECT NAME
    FROM yourTable
    GROUP BY NAME
    HAVING MIN(TRACK) = MAX(TRACK) AND MIN(TRACK) = 'SA';






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 '18 at 4:08

























    answered Nov 12 '18 at 3:59









    Tim BiegeleisenTim Biegeleisen

    218k1388140




    218k1388140











    • This is great Tim and does what I wanted! In addition to providing all horses that have run at one single track (your first query). The second one let's me get a filtered list by track (in this case SA). Thank you sir.
      – Robin
      Nov 12 '18 at 4:28
















    • This is great Tim and does what I wanted! In addition to providing all horses that have run at one single track (your first query). The second one let's me get a filtered list by track (in this case SA). Thank you sir.
      – Robin
      Nov 12 '18 at 4:28















    This is great Tim and does what I wanted! In addition to providing all horses that have run at one single track (your first query). The second one let's me get a filtered list by track (in this case SA). Thank you sir.
    – Robin
    Nov 12 '18 at 4:28




    This is great Tim and does what I wanted! In addition to providing all horses that have run at one single track (your first query). The second one let's me get a filtered list by track (in this case SA). Thank you sir.
    – Robin
    Nov 12 '18 at 4:28













    1














    You can do a subquery requesting the count of distinct tracks to be 1 using GROUP BY and HAVING COUNT DISTINCT and then select those WHERE the track is 'SA':



    SELECT NAME
    FROM
    (SELECT NAME, MIN(TRACK) as TRACK
    FROM HORSES
    GROUP BY 1
    HAVING COUNT (DISTINCT TRACK) = 1) horses_one_race
    WHERE
    TRACK = 'SA'





    share|improve this answer



























      1














      You can do a subquery requesting the count of distinct tracks to be 1 using GROUP BY and HAVING COUNT DISTINCT and then select those WHERE the track is 'SA':



      SELECT NAME
      FROM
      (SELECT NAME, MIN(TRACK) as TRACK
      FROM HORSES
      GROUP BY 1
      HAVING COUNT (DISTINCT TRACK) = 1) horses_one_race
      WHERE
      TRACK = 'SA'





      share|improve this answer

























        1












        1








        1






        You can do a subquery requesting the count of distinct tracks to be 1 using GROUP BY and HAVING COUNT DISTINCT and then select those WHERE the track is 'SA':



        SELECT NAME
        FROM
        (SELECT NAME, MIN(TRACK) as TRACK
        FROM HORSES
        GROUP BY 1
        HAVING COUNT (DISTINCT TRACK) = 1) horses_one_race
        WHERE
        TRACK = 'SA'





        share|improve this answer














        You can do a subquery requesting the count of distinct tracks to be 1 using GROUP BY and HAVING COUNT DISTINCT and then select those WHERE the track is 'SA':



        SELECT NAME
        FROM
        (SELECT NAME, MIN(TRACK) as TRACK
        FROM HORSES
        GROUP BY 1
        HAVING COUNT (DISTINCT TRACK) = 1) horses_one_race
        WHERE
        TRACK = 'SA'






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 4:26

























        answered Nov 12 '18 at 3:59









        Franco PiccoloFranco Piccolo

        1,529611




        1,529611



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255721%2fsql-find-the-horses-that-have-run-at-only-1-specific-track%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo