Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Expression for weighted average, working with joined tables??

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Expression for weighted average, working with joined tables??

    I am looking for help writing a calculated expression (if possible) to calculate the weighted average using multiple joined tables, concerning a golf tournament.
    Each tournament will have many teams. Each team will consist of 2 (players) golfers. Each golfer has a handicap. I would like an expression that would look at each team, find each of the 2 golfer?s individual handicap, determine the lower of the 2 handicaps, multiply the lower handicap by 2 and then divide the result by 3.

    Tournaments Table
    Tourn_ID: 0002 Active: Yes
    Teams Table
    Team_ID: 0025 Tourn_ID: 0002 Team_Handicap: CALCULATED
    Players Table
    Player_ID: 0076 Team_ID: 0025 Golfer_ID: 0197
    Player_ID: 1005 Team_ID: 0025 Golfer_ID: 1967
    Golfers Table
    Golfer_ID: 0197 First_Name: John Last_Name: Smith Handicap: 5
    Golfer_ID: 1967 First_Name: Bill Last_Name: Jones Handicap: 9

    Tournament - Teams (one to many) Tourn_ID
    Teams ? Players (one to many) Team_ID
    Players ? Golfers (one to one) Golfer_ID
    The following Select statement returns the correct records and data needed:
    SELECT Tournaments.Tourn_ID, Tournaments.Active, Teams.Team_ID, Teams.Tourn_ID AS Tourn_ID1, Teams.Team_Handicap, Players.Player_ID, Players.Team_ID AS Team_ID1, Players.Golfer_ID, golfers.golfer_id AS golfer_id1, golfers.first_name, golfers.last_name, golfers.Handicap
    FROM Tournaments Tournaments
    INNER JOIN (Teams Teams
    INNER JOIN (Players Players
    INNER JOIN golfers golfers
    ON Players.Golfer_ID = golfers.golfer_id )
    ON Teams.Team_ID = Players.Team_ID )
    ON Tournaments.Tourn_ID = Teams.Tourn_ID
    WHERE Tournaments.Active = 'Yes'

    Microsoft SQL Server Management Server Studio 2014
    Alpha 4770 5018
    Thanks
    Tony
    Last edited by Lenny Forziati; 03-28-2019, 09:27 AM. Reason: 2 partial posts combined

  • #2
    Re: Expression for weighted average, working with joined tables??

    Hi Tony,
    Just a couple things here.
    How do you plan to use the answer to that expression?
    Can you provide table structures and test data for the tables for testing purposes?

    Gregg
    Gregg
    https://paiza.io is a great site to test and share sql code

    Comment


    • #3
      Re: Expression for weighted average, working with joined tables??

      You have a good start....

      I'm not sure how you wish to do this in your app, but maybe this will get you going:

      If that is a view/query that you can read, then you can do a custom query in Alpha something like (this is not accurate, you'll have to rewrite it properly:

      Select Max(Handicap) as TeamHandicap
      from GolfersThatMatch
      where (GolfersThatMatch.TeamID = :TeamID)

      Something like that. You could even have a list object populated with that. Then perform a javascript calculation such as:

      function mycalc(TeamHandicap) {

      ***your calculation code goes here
      return mycalc

      }

      I guess from there you can then create a new array and have that populate another list with all the values for each team in the tournament.

      Comment


      • #4
        Re: Expression for weighted average, working with joined tables??

        Hi Tony,

        I'm back for more fun. I rearranged things a bit to make them easier for me to read, and because I think it's what you were trying to accomplish.
        I haven't tested it, but that's because I don't have the tables setup, but I think this gives you a good start

        Code:
         SELECT t.Tourn_ID, tm.Team_ID,  tm.Team_Handicap, tm.Player1_ID,g1.handicap,g1.first_name, g1.last_name,
         tm.Player2_id,g2.handicap, g2.first_name, g2.last_name, g.Handicap ,
         (
        	case
        		when g1.handicap < g2.handicap then
        				g1.handicap * 2 / 3
        		when g2.handicap < g1.handicap then
        				g2.handicap * 2 / 3
        		else
        				g1.handicap * 2 / 3
        	end case			
         ) as `calcdColumn`
         From Tournaments t
         inner join teams tm on tm.tournament_ID = t.tournament_ID
         inner join golfers g1 on g1.golfer_ID = tm.player1_ID 
         inner join golfers g2 on g2.golfer_ID = tm.player2_ID
         where t.active = 'Yes'
        Gregg
        https://paiza.io is a great site to test and share sql code

        Comment


        • #5
          Re: Expression for weighted average, working with joined tables??

          Gregg and Sean
          Thanks for replying!

          Sorry for my late response, I ended up having to be on the road today.

          Gregg, from quickly looking at your example ... I'm not sure how to get a g1.handicap and a g2.handicap.

          I'll keep working on it tonight.

          Thanks
          Tony

          Comment


          • #6
            Re: Expression for weighted average, working with joined tables??

            g1.handicap would be in the golfers table.
            Let me finish dinner and I'll try to give a better explanation.
            Gregg
            https://paiza.io is a great site to test and share sql code

            Comment


            • #7
              Re: Expression for weighted average, working with joined tables??

              Sounds good ..... but don't be in a rush, I am in no hurry!

              Comment


              • #8
                Re: Expression for weighted average, working with joined tables??

                Here it comes. Took me a bit longer to do this with ms sql than it would have with MySQL.
                The seq columns are there due to my habit, but are not required to make any of this work.
                I opted to have the players handicap with the team (player1_handicap and player2_handicap) thinking that a golfer's handicap may change
                after any given tournament but the handicap used for any given tournament should not change.

                The tables and structure are in the tables imagetables.PNG.
                This code sets up the basic data for the tables.
                Code:
                insert into tournaments (tourn_ID,active) values ('0002','Yes');
                insert into teams (team_ID,tourn_ID) values ('0025','0002');
                insert into golfers (golfer_ID,first_name,last_name,handicap) values ('0197','John','Smith',5);
                insert into golfers (golfer_ID,first_name,last_name,handicap) values ('1967','Bill','Jones',9);
                
                update t
                set t.player2_id = g.golfer_id,t.player2_handicap=g.handicap
                from teams t, golfers g
                where g.seq=2;
                
                update t
                set t.player1_id = g.golfer_id,t.player1_handicap=g.handicap
                from teams t, golfers g
                where g.seq=1;
                and this is the code that should generate what you were looking for

                Code:
                SELECT t.Tourn_ID, tm.Team_ID,  tm.Player1_ID,tm.player1_handicap,g1.first_name, g1.last_name,
                 tm.Player2_id,tm.player2_handicap, g2.first_name, g2.last_name, tm.player2_handicap ,
                 (
                	case 
                		when tm.player1_handicap < tm.player2_handicap then	(tm.player1_handicap * 2) / 3
                		when tm.player2_handicap < tm.player1_handicap then	tm.player2_handicap * 2 / 3
                		else
                				tm.player1_handicap * 2 / 3
                	end 	
                 ) as calcColumn
                 From Tournaments t
                 inner join teams tm on tm.tourn_ID = t.tourn_ID
                 inner join golfers g1 on g1.golfer_ID = tm.player1_ID 
                 inner join golfers g2 on g2.golfer_ID = tm.player2_ID
                 where t.active = 'Yes'
                Gregg
                https://paiza.io is a great site to test and share sql code

                Comment


                • #9
                  Re: Expression for weighted average, working with joined tables??

                  Gregg
                  I will give this a try.
                  I not sure how it will work with having 2 different fields for handicaps, because team members rarely stay the same (they choose to play with whom ever might be available that weekend).
                  So, we would have to be careful that each team member used a different handicap. (one for Handicap_1 and the other for Handicap_2)
                  Thanks
                  Tony

                  Comment


                  • #10
                    Re: Expression for weighted average, working with joined tables??

                    Tony,
                    I fully expect you have more code involved than what I generated.
                    My final solution most likely would change with more knowledge about
                    the process for tournaments, team registrations, and individual handicap calculations.
                    Gregg
                    https://paiza.io is a great site to test and share sql code

                    Comment


                    • #11
                      Re: Expression for weighted average, working with joined tables??

                      Gregg
                      I totally understand, thanks for giving me a good start.
                      Tony

                      Comment


                      • #12
                        Re: Expression for weighted average, working with joined tables??

                        Hi Tony,

                        I've added some more data to help show what happens with the handicap.
                        This allows teams to register for any future tournaments, but will only update the player's handicap
                        in the team table if the tournament is active.
                        Feel free to run the code, then take a look at the teams table.

                        As an afterthought, I have no idea what the purpose of the weighted average is, nor do I know
                        what expression you are using to calculate the team handicap.

                        Code:
                        insert into golfers (golfer_ID,first_name,last_name,handicap) values ('2001','George','Washington',25);
                        insert into golfers (golfer_ID,first_name,last_name,handicap) values ('2002','Abraham','Lincoln',3);
                        insert into golfers (golfer_ID,first_name,last_name,handicap) values ('2003','Donald','Trump',7);
                        insert into golfers (golfer_ID,first_name,last_name,handicap) values ('2004','Tiger','Woods',0);
                        
                        update tournaments set active = 'No' where active = 'Yes';
                        insert into tournaments (tourn_ID,active) values ('0003', 'Yes');
                        insert into tournaments (tourn_ID,active) values ('0004','No');
                        update golfers set handicap = 13 where handicap = 9;
                        
                        insert into teams (tourn_ID,team_ID, player1_ID,player2_ID) values ('0004','0133','2002','2001');
                        insert into teams (tourn_ID,team_ID, player1_ID,player2_ID) values ('0003','0134','1967','2001');
                        insert into teams (tourn_ID,team_ID, player1_ID,player2_ID) values ('0003','0135','2003','2004');
                        insert into teams (tourn_ID,team_ID, player1_ID,player2_ID) values ('0003','0136','0197','2002');
                        
                        -- The next block of code will update team player handicaps for the current active tournament
                        update tm
                        		set tm.player1_handicap = p1.handicap, tm.player2_handicap = p2.handicap
                        from teams tm
                        inner join tournaments t on tm.tourn_ID = t.tourn_ID
                        inner join golfers p1 on tm.player1_ID = p1.golfer_ID
                        inner join golfers p2 on tm.player2_ID = p2.golfer_ID
                        where t.active = 'Yes' and tm.tourn_ID = t.tourn_ID;
                        
                        -- If you want to see the "calcColumn" value for active or completed tournaments, change "where t.active = 'Yes' " to "where tm.player1_handicap is not null "
                        SELECT t.Tourn_ID, tm.Team_ID,  tm.Player1_ID,tm.player1_handicap,g1.first_name, g1.last_name,
                         tm.Player2_id,tm.player2_handicap, g2.first_name, g2.last_name, 
                         (
                        	case 
                        		when tm.player1_handicap < tm.player2_handicap then	(tm.player1_handicap * 2) / 3
                        		when tm.player2_handicap < tm.player1_handicap then	tm.player2_handicap * 2 / 3
                        		else
                        				tm.player1_handicap * 2 / 3
                        	end 	
                         ) as calcColumn
                         From Tournaments t
                         inner join teams tm on tm.tourn_ID = t.tourn_ID
                         inner join golfers g1 on g1.golfer_ID = tm.player1_ID 
                         inner join golfers g2 on g2.golfer_ID = tm.player2_ID
                         where t.active = 'Yes';
                        Gregg
                        https://paiza.io is a great site to test and share sql code

                        Comment


                        • #13
                          Re: Expression for weighted average, working with joined tables??

                          Gregg
                          Thanks for the example ..... I'll play around with it this weekend.

                          The team handicap = ((the lowest team member's handicap x 2) + the higher team member's handicap)/3
                          So could be like 3 x 2 = 6 + 8 = 14 / 3 = 4.67 (Where 3 is the lowest team member handicap and 8 is the higher team member handicap

                          This is just a process to help us keep golfers honest and make it fair for everyone.
                          The thinking is that a golfer that can shoot a 3 over by himself, could go out and find a friend that has a handicap of 12 and by not using a weighted average, their handicap would be a 7.5.
                          With the weighted average their team handicap would be 6.
                          The team handicaps determines what group of teams you compete against.

                          Thanks
                          Tony

                          Comment


                          • #14
                            Re: Expression for weighted average, working with joined tables??

                            Tony,

                            You scared me for a minute. The expression for team handicap looked so close to the expression for the weighted handicap
                            that I thought I did it wrong.

                            This leads to that wonderful question of what the expression for team handicap should be when the golfers have the same handicap ?
                            Gregg
                            https://paiza.io is a great site to test and share sql code

                            Comment


                            • #15
                              Re: Expression for weighted average, working with joined tables??

                              Greg
                              Sorry for misleading you, but the team handicap and the weighted handicap are the same.
                              Tony

                              Comment

                              Working...
                              X