Loading ...
Sorry, an error occurred while loading the content.

QUestion

Expand Messages
  • Karl
    I m not sure what the proper term for this is. I have a table of baseball stats and what to get the top 10 batting averages. In SQL it would look like this:
    Message 1 of 5 , Aug 5 7:23 PM
    • 0 Attachment
      I'm not sure what the proper term for this is. I have a table of baseball stats and what to get the top 10 batting averages. In SQL it would look like this:

      select (hits / atbat) as bavg from batting_stats order by bavg desc

      Does TaffyDB provide this in anyway (I haven't seen anything on the website) or has anyone done an extension that can do this?

      Thanks,
      Karl
    • mwhit007400
      Karl, I would do something like this: var x = 0 var bat_avg = new Array(); //you may have to change the while statement to get it to stop when you want it to
      Message 2 of 5 , Aug 6 7:39 AM
      • 0 Attachment
        Karl,

        I would do something like this:
        var x = 0
        var bat_avg = new Array();
        //you may have to change the while statement to get it to stop when you want it to
        while( hits != "" ){
        var hits = db({"id":x}).select("hits")
        var bats = db({"id":x}).select("bats")
        bat_avg = hits/bats
        x++
        }
        //not sure how the sort function for arrays works in JS but I am sure you can figure it out
        top_10_bat_avg = bat_avg.sort()

        Hopefully this helps,
        mwhit74

        --- In taffydb@yahoogroups.com, "Karl" <karlcoleman@...> wrote:
        >
        > I'm not sure what the proper term for this is. I have a table of baseball stats and what to get the top 10 batting averages. In SQL it would look like this:
        >
        > select (hits / atbat) as bavg from batting_stats order by bavg desc
        >
        > Does TaffyDB provide this in anyway (I haven't seen anything on the website) or has anyone done an extension that can do this?
        >
        > Thanks,
        > Karl
        >
      • tacoman_cool
        You should be able to get to that by doing something like this: var top10 = stats().update(function () { this.bavg = this.hits/this.atbat;return this;
        Message 3 of 5 , Aug 6 11:29 AM
        • 0 Attachment
          You should be able to get to that by doing something like this:

          var top10 = stats().update(function () {
          this.bavg = this.hits/this.atbat;return this;
          }).order("bavg desc").limit(10).get()

          You are using update to create a new column and then sorting your query based on that column, limiting the results to 10, and then returns the records with .get();

          --- In taffydb@yahoogroups.com, "Karl" <karlcoleman@...> wrote:
          >
          > I'm not sure what the proper term for this is. I have a table of baseball stats and what to get the top 10 batting averages. In SQL it would look like this:
          >
          > select (hits / atbat) as bavg from batting_stats order by bavg desc
          >
          > Does TaffyDB provide this in anyway (I haven't seen anything on the website) or has anyone done an extension that can do this?
          >
          > Thanks,
          > Karl
          >
        • Karl
          Thanks for the idea. Going on that idea I m thinking something like this then. var bavg = []; batters().each(function(r) { var b_avg = r.hits / r.atbats; //
          Message 4 of 5 , Aug 6 12:00 PM
          • 0 Attachment
            Thanks for the idea. Going on that idea I'm thinking something like this then.

            var bavg = [];
            batters().each(function(r) {
            var b_avg = r.hits / r.atbats; // I'll round and format it
            bavg.push({id: r.id, avg: b_avg});

            });
            bavg.sort(function(a,b) { // sort by avg });

            Karl

            --- In taffydb@yahoogroups.com, "mwhit007400" <mwhit74@...> wrote:
            >
            > Karl,
            >
            > I would do something like this:
            > var x = 0
            > var bat_avg = new Array();
            > //you may have to change the while statement to get it to stop when you want it to
            > while( hits != "" ){
            > var hits = db({"id":x}).select("hits")
            > var bats = db({"id":x}).select("bats")
            > bat_avg = hits/bats
            > x++
            > }
            > //not sure how the sort function for arrays works in JS but I am sure you can figure it out
            > top_10_bat_avg = bat_avg.sort()
            >
            > Hopefully this helps,
            > mwhit74
            >
            > --- In taffydb@yahoogroups.com, "Karl" <karlcoleman@> wrote:
            > >
            > > I'm not sure what the proper term for this is. I have a table of baseball stats and what to get the top 10 batting averages. In SQL it would look like this:
            > >
            > > select (hits / atbat) as bavg from batting_stats order by bavg desc
            > >
            > > Does TaffyDB provide this in anyway (I haven't seen anything on the website) or has anyone done an extension that can do this?
            > >
            > > Thanks,
            > > Karl
            > >
            >
          • Karl
            That s good to know. I had no idea you could do that with the update() function. The first time the query is done it adds the column. This way might work
            Message 5 of 5 , Aug 6 12:24 PM
            • 0 Attachment
              That's good to know. I had no idea you could do that with the update() function. The first time the query is done it adds the column. This way might work better for me. I'll try this one first, otherwise I'll do the arrays idea.

              In case anyone is wondering why I don't just put it in the table to begin with, my host limits me on file size, so my data is barebones, and definitely nothing that I can calculate with already existing fields, thus no batting average, ERA, etc, columns.

              Thanks for the help guys.

              Karl

              --- In taffydb@yahoogroups.com, "tacoman_cool" <ian@...> wrote:
              >
              > You should be able to get to that by doing something like this:
              >
              > var top10 = stats().update(function () {
              > this.bavg = this.hits/this.atbat;return this;
              > }).order("bavg desc").limit(10).get()
              >
              > You are using update to create a new column and then sorting your query based on that column, limiting the results to 10, and then returns the records with .get();
              >
              > --- In taffydb@yahoogroups.com, "Karl" <karlcoleman@> wrote:
              > >
              > > I'm not sure what the proper term for this is. I have a table of baseball stats and what to get the top 10 batting averages. In SQL it would look like this:
              > >
              > > select (hits / atbat) as bavg from batting_stats order by bavg desc
              > >
              > > Does TaffyDB provide this in anyway (I haven't seen anything on the website) or has anyone done an extension that can do this?
              > >
              > > Thanks,
              > > Karl
              > >
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.