473,789 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help with query

15 New Member
I have a fantasy football league that I am keeping stats for in an Access Database. I need help coming up with a way to calclulate the winning % for each team for their entire career. What I have is a table with the following fields:
Fantasy Team
Season
Week
W/L
Own Score
Vs
Opponent
Opponent Score
Game type

The W/L field will either have a W or a L depending if they won or loss, the Vs field will either have a vs or a @ depending if its a home or away game, and the Game Type field tells if it was a regular season game, playoff game, Super bowl game, or Toilet bowl game.

What I have done so far is I created a query on the above table to sort out just the winners and this new table is called Winners. I then created a crosstab query called Winners_Crossta b that counts the number of Wins each team has had for each season. So now I have totals wins. I then did the same thing for losses to end up with a query called Loosers_Crossta b. The last thing I did was to create another query called WinPercentage and added the Winners_Crossta b and Loosers_Crossta b and linked the two tables by Fantasy team. The fields include:
Fantasy Team
Count of Wins
Count of Losses
Win%
The win% field is one I added manually and is a calculation of
wins/(wins+losses)*1 00

This works fine except for when a team has no wins or no losses. If a team went 14-0 then the team doesn't show up in the WinPercentage query. I understand why it doesn't but I don't know how to get around this. I hope I made this clear enough for you to give me some quidance. I am new to databases and just started learning Access about 4 months ago.

Thanks for your help,

Scott
Oct 10 '07 #1
28 1806
G8tors
15 New Member
Sorry, forgot to mention that I'm using Access 2003 and Windows XP professional.
Oct 10 '07 #2
MMcCarthy
14,534 Recognized Expert Moderator MVP
You need to change the crosstabs so all teams appear even if there are 0 winners or 0 losers. If you want to post the SQL of the crosstab queries I'll have a look at them.
Oct 11 '07 #3
G8tors
15 New Member
This is the Crosstab Query I have set up to count the number of superbowl wins for each team.
TRANSFORM Count([SB Wins].[W/L]) AS [CountOfW/L]
SELECT [SB Wins].[Fantasy Team], Count([SB Wins].[W/L]) AS [Total Of W]
FROM [SB Wins]
GROUP BY [SB Wins].[Fantasy Team]
PIVOT [SB Wins].Season;

This is the Crosstab Query I have set up to count the number of superbowl losses for each team.
TRANSFORM Count([SB Losses].[W/L]) AS [CountOfW/L]
SELECT [SB Losses].[Fantasy Team], Count([SB Losses].[W/L]) AS [Total Of L]
FROM [SB Losses]
GROUP BY [SB Losses].[Fantasy Team]
PIVOT [SB Losses].Season;

SB Wins query is setup with the W/L field criteria set to "W" and the SB Losses query is the setup with the W/L field criteria set to "L".

Thank you for taking the time to help me.
Oct 11 '07 #4
MMcCarthy
14,534 Recognized Expert Moderator MVP
OK the problem here is you have your wins and losses in separate tables. You will need to join both queries to the table which contains all the Fantasy team names. For this example I will use the name Teams for the table. Now change the crosstabs as follows:

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM nz(Count([SB Wins].[W/L]),0) AS [CountOfW/L]
  2. SELECT [Teams].[Fantasy Team], nz(Count([SB Wins].[W/L]),0) AS [Total Of W]
  3. FROM [Teams] LEFT JOIN [SB Wins]
  4. ON [Teams].[Fantasy Team]=[SB Wins].[Fantasy Team]
  5. GROUP BY [Teams].[Fantasy Team]
  6. PIVOT [SB Wins].Season;
  7.  
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM nz(Count([SB Losses].[W/L]),0) AS [CountOfW/L]
  2. SELECT [Teams].[Fantasy Team], nz(Count([SB Losses].[W/L]),0) AS [Total Of L]
  3. FROM [Teams] LEFT JOIN  [SB Losses]
  4. ON [Teams].[Fantasy Team]=[SB Losses].[Fantasy Team]
  5. GROUP BY [Teams].[Fantasy Team]
  6. PIVOT [SB Losses].Season;
  7.  
Oct 11 '07 #5
G8tors
15 New Member
Thanks MMCCARTHY. I haven't tried your code yet but I wanted to let you know that after I read your post about having all the teams appear that made sense. So I deleted the querries I made that seperated the superbowl winners and the super bowl losers (SB Wins and SB Losses) and created a query that contains both wins and losses called SB Games. I then just started trying things and came up with this crosstab query:

TRANSFORM Sum([SB Games].[W/L]="W")*(-1) AS CountOfW
SELECT [SB Games].[Fantasy Team], Sum([SB Games].[W/L]="W")*(-1) AS [Total Of W]
FROM [SB Games]
GROUP BY [SB Games].[Fantasy Team]
PIVOT [SB Games].Season;

The numbers were negative so I multiplied them by -1 and that seemed to work accept for one annoying problem. zeros show up with a negative sign in front of them on my report (ie -0). I did this before I saw your last post. Am I bettter off going with your newest code or do you know how to keep the zeros from showing up as negative zero?


Thanks again
Oct 11 '07 #6
G8tors
15 New Member
I went ahead and did what you suggested and it worked great. However, when I create my final query:

SELECT [SB Wins_CrossTab2].[Total Of W], [SB Losses_CrossTab 2].[Total Of L], [SB Wins_CrossTab2].[Fantasy Team], [Total Of W]/([Total Of W]+[Total Of L])*100 AS [win%]
FROM [SB Wins_CrossTab2] INNER JOIN [SB Losses_CrossTab 2] ON [SB Wins_CrossTab2].[Fantasy Team] = [SB Losses_CrossTab 2].[Fantasy Team]
ORDER BY [Total Of W]/([Total Of W]+[Total Of L])*100 DESC;

I get an overflow error. I supsect that is because I have teams that have never been in the superbowl and it is dividing by zero to calculate the win%?

We are close.
Oct 11 '07 #7
MMcCarthy
14,534 Recognized Expert Moderator MVP
Change it to:

Expand|Select|Wrap|Line Numbers
  1. SELECT [SB Wins_CrossTab2].[Total Of W], [SB Losses_CrossTab2].[Total Of L], [SB Wins_CrossTab2].[Fantasy Team], [Total Of W]/nz([Total Of W]+[Total Of L],1)*100 AS [win%]
  2. FROM [SB Wins_CrossTab2] INNER JOIN [SB Losses_CrossTab2] 
  3. ON [SB Wins_CrossTab2].[Fantasy Team] = [SB Losses_CrossTab2].[Fantasy Team]
  4. ORDER BY [Total Of W]/([Total Of W]+[Total Of L])*100 DESC;
  5.  
This will force a divide by 1 when no records found.
Oct 11 '07 #8
G8tors
15 New Member
I still get the overflow error. Can we not have zero in the numerator also?
Oct 11 '07 #9
MMcCarthy
14,534 Recognized Expert Moderator MVP
Try this ...

Expand|Select|Wrap|Line Numbers
  1. SELECT [SB Wins_CrossTab2].[Total Of W], [SB Losses_CrossTab2].[Total Of L], [SB Wins_CrossTab2].[Fantasy Team], [Total Of W]/nz([Total Of W]+[Total Of L],1)*100 AS [win%]
  2. FROM [SB Wins_CrossTab2] INNER JOIN [SB Losses_CrossTab2] 
  3. ON [SB Wins_CrossTab2].[Fantasy Team] = [SB Losses_CrossTab2].[Fantasy Team]
  4. ORDER BY [Total Of W]/nz([Total Of W]+[Total Of L],1)*100 DESC;
  5.  
Oct 11 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3058
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
9
3138
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
6
2430
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18. I need a query that will return the c date for TypeID 18 if it exist else the date for TypeID 1, for all jobs. the table structure is the following Job TypeID
3
1867
by: pw | last post by:
Hi, I am having a mental block trying to figure out how to code this. Two tables: "tblQuestions" (fields = quesnum, questype, question) "tblAnswers" (fields = clientnum, quesnum, questype, answer) They are related by quesnum and questype. There are records in
7
2372
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of which a part may be composed. I have a table of parts and their subparts. The problem is that each of those subparts may be composed of smaller component parts. The subpart would then be listed in the Part field linked to each of its subparts in...
3
10668
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems that are hard to find. The main problem I am having right now is that I have a report that is sorted by one of these lookup fields and it only displays the record's ID number. When I add the source table to the query it makes several records...
0
2263
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional assistance. I say additional because I've already had help which is greatly appreciated. I do try to take the time and understand the provided script in hopes on not having to trouble others on those. But here it goes...
10
2596
by: L. R. Du Broff | last post by:
I own a small business. Need to track a few hundred pieces of rental equipment that can be in any of a few dozen locations. I'm an old-time C language programmer (UNIX environment). If the only tool you know how to use is a hammer, every problem tends to look like a nail. That said, I could solve my problem in C, but it's not the right tool. I need to come into the Windows world, and I need to get this done in Access or something...
7
2037
by: Rnykster | last post by:
I know a little about Access and have made several single table databases. Been struggling for about a month to do a multiple table database with no success. Help! There are two tables. First has about 30 fields. Every entry in this table will be unique. Second table has about 7 fields and is for reference - strictly a look up type table. I want to use one field, say FAMILY in the first table to look up any one of the 400 items in the...
3
2562
by: pbd22 | last post by:
Hi. I need some help with structuring my query strings. I have a form with a search bar and some links. Each link is a search type (such as "community"). The HREF for the link's anchor looks like the following: <a href="?searchtype=2">Community</a>
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.