473,388 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

SQL - SUM in a sub-query in Access '03

I hope my question here is clear. I have a table GovVotes that contains the following fields: Year/State/ElectionType/Candidate/Party/PopularVotes.

Via net-searching, I found a code for a query I wanted to perform, basically take all those fields and display what Rank each candidate was in each particular race, based on Year/State/ElectionType. I then added a query field called Result based on the Rank created field.

The SQL for this query looks like this:

SELECT CS.Year, CS.State, CS.ElectionType, CS.Candidate, CS.Party, CS.PopularVotes, (SELECT COUNT(*) + 1 FROM GovVotes AS C WHERE (C.PopularVotes > [CS.PopularVotes] AND C.State = CS.State AND C.Year = CS.Year AND C.ElectionType = CS.ElectionType)) AS Rank, IIf([Rank]=1,"Win","Loss") AS Result
FROM GovVotes AS CS
ORDER BY CS.Year, CS.State, CS.ElectionType, CS.PopularVotes DESC;


The CS & C are the designations the snippet of code I got from the internet contained, they hold no meaning to me.

What I want to try to add is a sum of all votes cast in each election. For instance, my data set at this point looks like this:

Year State ElectionType Candidate Party PopularVotes Rank Result
2006 Maryland General O'Malley, Martin Democrat 942279 1 Win
2006 Maryland General Ehrlich, Robert L. Republican 825464 2 Loss
2006 Maryland General Boyd, Ed Green 15551 3 Loss
2006 Maryland General Driscoll, Christopher A. Populist 3481 4 Loss
2006 Maryland General Write-In 1541 5 Loss
2008 New York General TestData Test 10000 1 Win

I want to add a column called ElectionVotes that would display the total votes cast in each Year/State/ElectionType. And I have zero clue how the SQL would work for this. I'm trying to learn SQL, but I'm easily stumped. Any help would be appreciated. If it's necessary to re-do the existing SQL, that's more than welcome at this point as well.

Joseph
May 30 '08 #1
1 1585
Okay, I believe I found some code to make this work

(Select Sum([PopularVotes]) From [GovVotes] T Where T.Year = [CS].Year and T.State = [CS].State AND T.ElectionType = [CS].ElectionType) AS ElectionVotes.

Again, it's a web-snippet that I found so I might be doing this wrong, if anyone has a better way, I'll appreciate the more sound manner, but as of now, this is working for me.
May 30 '08 #2

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

Similar topics

2
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
5
by: Colleyville Alan | last post by:
I have a sub that can do two different tasks based on the value of one variable. So I'd like to have two different buttons on the same form run this, but each one setting a flag so that the...
3
by: Kathy Burke | last post by:
Hi, I'm tired, so this question may be silly. I have a fairly long sub procedure. Based on one condition, I load another sub with the following: If Session("GRN") = "complete" Then txtScan.Text...
10
by: tmaster | last post by:
When I try to dynamically add a second sub menu item to this ContextMenu item, I get an error 'Specified argument was out of the range of valid values'. Private Sub mnuTopics_Show_Select(ByVal...
12
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to...
4
by: dbuchanan | last post by:
Is the following behavior normal? Both the 'Protected sub' in the inherited form and the 'Private Shadows sub' in the derived form fires. My interpretation of MSDN help on the topic "Shadows"...
4
by: excelleinc.com | last post by:
Hello, I'm trying to put sub that's shared between all forms in my app in module so when I make change I just change it one time. This sub should execute and then invoke sub defaults() that's...
6
by: Bob | last post by:
Hi, I found this code here below (about cartitems and shoppingcart) and I have two questions about sub New(). In the first class CartItem, there is two times sub New(): Public Sub New() End...
6
by: Greg Strong | last post by:
Hello All, Is is possible to use an ADO recordset to populate an unbound continuous Subform? I've done some Googling without much luck, so this maybe impossible, but let me try to explain...
6
by: Thom Little | last post by:
Using C# 3.5 I have a form that calls many other sub-forms. Typically there will be five forms open at the same time. If the main form is closed all the sub forms are also closed. Is there...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.