sql statement causing brain pain

Ask a Question related to Dreamweaver AppDev, Design and Development.

  1. #1

    Default sql statement causing brain pain

    Thanks to all of you who have helped me with other sql queries in the last
    couple weeks. And I have another one. I have managed to pretty much get things
    working as they should except for one query that I can neither get to work nor
    even really get my head around.

    This is an sql statement that does work except that it is missing one factor -
    dates. I think I can use only 2 tables for this query but perhaps a third is
    needed. As you can see from the statement, I am using the Institution and Stats
    table, related on the Institution_ID field. What I am looking for are
    institutions who have not entered in any data into the stats table for a given
    data_year which comes from a dropdown (Stats_Year_ID). The stats table can have
    multiple years of data in it and the current statement only works if the
    institution has never entered in any data. If they haven't entered in data for
    one particular year then there will not be that Stats_Year_ID in the stats
    table.

    "SELECT Institution_Name, Institution.Institution_ID FROM Institution LEFT
    OUTER JOIN Stats ON Stats.Institution_ID = Institution.Institution_ID WHERE
    Stats.Institution_ID IS NULL GROUP BY Institution_Name,
    Institution.Institution_ID"

    Thanks for any help.

    zCrow Guest

  2. Similar Questions and Discussions

    1. nextSibling Pain
      Im having a problem with using nextSibling. - I do not want to contiue to add nextSibling.nextSibling.nextSibling - I have tried unsuccessfuly to...
    2. Datagrid ... pain .... HELP!
      Hi every1, I have trying to learn datagrid to do simple things but since day 1 running into problems after problems for really simple things. ...
    3. Javascript being a pain...
      Hi... I have a javascript function that when an image is moused over it will display some text in a div elsewhere on the page. Problem is this...
    4. Installing php with gd what a pain!
      Hello Guys, I am trying to install php 4.3.2 with gd support. I have already installed zlib and jpeg-6b. My configure line options are as below:...
    5. for xml explicit is a pain!!!
      I am running a for xml explicit procedure with about 18 tags - tag 3 uses a table called projects for instance, tag 12 uses the projects table...
  3. #2

    Default Re: sql statement causing brain pain

    Don't kick yourself when you see how simple it is. :)

    SELECT Institution_Name, Institution.Institution_ID
    FROM Institution
    LEFT OUTER JOIN Stats ON Stats.Institution_ID =
    Institution.Institution_ID AND Stats.data_year=varYear
    WHERE Stats.Institution_ID IS NULL
    GROUP BY Institution_Name, Institution.Institution_ID

    varYear should be equal to Request.Form("Stats_Year_ID"), so do what you
    have to do in your setup to get that parameter in there. You can think of
    it as joining on a partial table. That is, it's equivalent to

    FROM Institution
    LEFT OUTER JOIN (SELECT * FROM Stats WHERE data_year=varYear) AS S ON
    Institution.Institution_ID=S.Institution_ID

    I just find it easier to read with a compound join condition than with a
    derived table.



    "zCrow" <webforumsuser@macromedia.com> wrote in message
    news:d6dbg0$j69$1@forums.macromedia.com...
    > Thanks to all of you who have helped me with other sql queries in the last
    > couple weeks. And I have another one. I have managed to pretty much get
    > things
    > working as they should except for one query that I can neither get to work
    > nor
    > even really get my head around.
    >
    > This is an sql statement that does work except that it is missing one
    > factor -
    > dates. I think I can use only 2 tables for this query but perhaps a third
    > is
    > needed. As you can see from the statement, I am using the Institution and
    > Stats
    > table, related on the Institution_ID field. What I am looking for are
    > institutions who have not entered in any data into the stats table for a
    > given
    > data_year which comes from a dropdown (Stats_Year_ID). The stats table can
    > have
    > multiple years of data in it and the current statement only works if the
    > institution has never entered in any data. If they haven't entered in data
    > for
    > one particular year then there will not be that Stats_Year_ID in the stats
    > table.
    >
    > "SELECT Institution_Name, Institution.Institution_ID FROM Institution LEFT
    > OUTER JOIN Stats ON Stats.Institution_ID = Institution.Institution_ID
    > WHERE
    > Stats.Institution_ID IS NULL GROUP BY Institution_Name,
    > Institution.Institution_ID"
    >
    > Thanks for any help.
    >

    Lionstone Guest

  4. #3

    Default Re: sql statement causing brain pain

    Hi again LionStone. I'm glad you hang out here! I haven't actually tried your
    sql but looking at it I don't think it will work. The query you sent is looking
    for an entry in the stats table where the Stats_Year_ID exists. However, what I
    am looking for is where the stats table does not have that Stats_Year_ID as in
    "find institutions who have NOT entered any data into the stats table for the
    selected data year" Basically I am looking for an entry that doesn't exist.

    zCrow Guest

  5. #4

    Default Re: sql statement causing brain pain

    I know.
    If there is one row for each year's stats, or even if the row only contains
    the year of the last update, that query will work.
    It's just like before - the LEFT JOIN keeps all records in the left hand
    table, in your case, institutions. The join conditions say to only keep
    rows where the institution IDs match (so you're only counting the stats
    entered for the proper institution) and where the stats record is from a
    certain year. If the stats record is not from that year, the row is
    stricken from the result set, BUT the rules of LEFT JOIN say that all
    Institution records must be kept (except those later stricken by the WHERE
    clause), so only the right-hand table's data is stricken. What does that
    mean?
    The stats part of your combined record will be NULL if there is no stats
    entry for the year you specify, so you can perform the left join and then
    ignore all results with a matching stats record, hence the IS NULL condition
    in the WHERE clause.


    "zCrow" <webforumsuser@macromedia.com> wrote in message
    news:d6ddnu$mdp$1@forums.macromedia.com...
    > Hi again LionStone. I'm glad you hang out here! I haven't actually tried
    > your
    > sql but looking at it I don't think it will work. The query you sent is
    > looking
    > for an entry in the stats table where the Stats_Year_ID exists. However,
    > what I
    > am looking for is where the stats table does not have that Stats_Year_ID
    > as in
    > "find institutions who have NOT entered any data into the stats table for
    > the
    > selected data year" Basically I am looking for an entry that doesn't
    > exist.
    >

    Lionstone Guest

  6. #5

    Default Re: sql statement causing brain pain

    My apologies for doubting you! I will give that one a go and see if that gives me what I am looking for.

    Thanks again for the follow through.
    zCrow Guest

  7. #6

    Default Re: sql statement causing brain pain

    Hi again LionStone. I hope you aren't feeling that I am taking advantage of
    your help but I tried using the sql statement you suggested and it came back
    with an error saying that "Join expression is not supported". It seems to have
    something to do with the "AND Stats.Stats_Year_ID = varID" bit. Here is the sql
    I am using:

    "SELECT Institution.Institution_Name, Institution.Institution_ID FROM
    Institution LEFT OUTER JOIN Stats ON Stats.Institution_ID =
    Institution.Institution_ID AND Stats.Stats_Year_ID = " + request.form("year") +
    " WHERE Stats.Institution_ID IS NULL GROUP BY Institution.Institution_Name,
    Institution.Institution_ID"

    I tried moving that section above on the other side of the WHERE clause and it
    no longer gave me an error but of course it didn't give me the correct answer
    either.



    zCrow Guest

  8. #7

    Default Re: sql statement causing brain pain

    Well, I don't remember what database you're using. First things first -
    make sure that Request.Form("year") always exists. A blank there will throw
    an error for sure.

    Second, try the derived table:
    FROM Institution LEFT OUTER JOIN (SELECT * FROM Stats S WHERE
    S.Stats_Year_ID = " + Request.Form("year") + ") AS Stats ON
    Institution.Institution_ID=Stats.Institution_ID

    If the database also doesn't support derived tables, use the IN clause.
    This is probably a bit slower, but that depends on the database optimizer.
    The general rule is that a join will outperform a subquery for large amounts
    of data.

    SELECT ID, Name
    FROM Institution
    WHERE Institution_ID NOT IN (SELECT Institution_ID FROM Stats WHERE
    Stats_Year_ID = " + Request.Form("year") + ")"


    "zCrow" <webforumsuser@macromedia.com> wrote in message
    news:d6dr03$aqs$1@forums.macromedia.com...
    > Hi again LionStone. I hope you aren't feeling that I am taking advantage
    > of
    > your help but I tried using the sql statement you suggested and it came
    > back
    > with an error saying that "Join expression is not supported". It seems to
    > have
    > something to do with the "AND Stats.Stats_Year_ID = varID" bit. Here is
    > the sql
    > I am using:
    >
    > "SELECT Institution.Institution_Name, Institution.Institution_ID FROM
    > Institution LEFT OUTER JOIN Stats ON Stats.Institution_ID =
    > Institution.Institution_ID AND Stats.Stats_Year_ID = " +
    > request.form("year") +
    > " WHERE Stats.Institution_ID IS NULL GROUP BY
    > Institution.Institution_Name,
    > Institution.Institution_ID"
    >
    > I tried moving that section above on the other side of the WHERE clause
    > and it
    > no longer gave me an error but of course it didn't give me the correct
    > answer
    > either.
    >
    >
    >

    Lionstone Guest

  9. #8

    Default Re: sql statement causing brain pain

    Once again, thankyou for the extremely thorough followup on this one.

    I tried the first one you suggested and while it did not give an error, it
    also did not retrieve the records I was looking for. It simply retrieved every
    entry in the stats table and then added in the institution that had not entered
    anything into the stats table - so basically, it seemed to retrieve all the
    records from the institution table.

    I then tried the second suggestion and woohoo, that one worked perfectly and
    gave me exactly the records I was looking for. I tested it under a few other
    circumstances and it worked perfectly.

    So, thankyou. Brilliant work, excellent results.

    zCrow Guest

  10. #9

    Default Re: sql statement causing brain pain

    I aim to please. :)

    "zCrow" <webforumsuser@macromedia.com> wrote in message
    news:d6g54u$qfd$1@forums.macromedia.com...
    > Once again, thankyou for the extremely thorough followup on this one.
    >
    > I tried the first one you suggested and while it did not give an error, it
    > also did not retrieve the records I was looking for. It simply retrieved
    > every
    > entry in the stats table and then added in the institution that had not
    > entered
    > anything into the stats table - so basically, it seemed to retrieve all
    > the
    > records from the institution table.
    >
    > I then tried the second suggestion and woohoo, that one worked perfectly
    > and
    > gave me exactly the records I was looking for. I tested it under a few
    > other
    > circumstances and it worked perfectly.
    >
    > So, thankyou. Brilliant work, excellent results.
    >

    Lionstone Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139