473,397 Members | 2,116 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,397 software developers and data experts.

How can I sum the same column twice but have one count only unique values?

lilp32
43
I have a table in an Access 2010 database. The table contains the fields Subject and Test_Number. Each subject and test number are listed in one row. Subjects can have multiple tests so there are duplicates in the Subject column. Tests can have multiple results, so there are duplicates in the Test_Number field. I would like to end up with one row per subject with Test_Number counted as follows:

1. "Results" - the total count of Test_Number for each subject including duplicates.
2. "Tests" - the total count of unique Test_Number per subject. The code below only has the column with duplicates.

I have tried using a nested subquery with select distinct but have not been able to get the syntax right.

Expand|Select|Wrap|Line Numbers
  1. SELECT [TABLE1].Subject, Count([TABLE1].Test_Number) AS Results
  2. FROM [TABLE1]
  3. GROUP BY [TABLE1].Subject;
  4.  
Jan 29 '13 #1

✓ answered by Rabbit

Create a query that does a count on distinct records and join it to that query.

You should also know that your design is unnormalized and the results really should be its own separate table.

4 2650
Rabbit
12,516 Expert Mod 8TB
Create a query that does a count on distinct records and join it to that query.

You should also know that your design is unnormalized and the results really should be its own separate table.
Jan 29 '13 #2
NeoPa
32,556 Expert Mod 16PB
Rabbit's idea can also be used with a subquery (Subqueries in SQL).

Alternatively, you could use a subquery where the records are already GROUPed by [Subject] and use Sum() for [Results], but Count() for [Tests]. I think that's the right way around, but hopefully you get the picture.
Jan 30 '13 #3
lilp32
43
Thanks, I was able to get this working by creating a second query with the distinct count and joining it as Rabbit suggested. I am still trying to understand subqueries. Thanks again.
Jan 30 '13 #4
NeoPa
32,556 Expert Mod 16PB
Lilp32:
I am still trying to understand subqueries
You and many others. I first got into them after Rabbit posted something on this site a few years back. Now I use them quite extensively. That's not the only SQL pointer I've picked up from Rabbit either. He's really very good, if sometimes a little short on explanatory detail when posting :-D
Jan 31 '13 #5

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

Similar topics

2
by: Ori | last post by:
Hi, I have two tables such that in each table I need to make sure that column x in table A and column y in table B have a unique values meaning that a user cannot insert a value to column A if...
3
by: Eric | last post by:
Hi : From a crystal report i get a list of employee firstnames as a string into my store procedure. Why is it comming this way ? hmmmmmm it's a question for me too. ex: "e1,e2,e3" here are my...
3
by: Mihir | last post by:
All, I have this table: ClientName City Ram Mumbai Ram Cochin Ram Mumbai Ram Bangalore Lakhan Mumbai
2
by: Volition | last post by:
I have looked around and can't find any help for my SQL problem. I have a Table which lists people peopledb. _____________ id | Name 1 | Fred 2 | Bill 3 | Bob
11
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation a table with columns and rows
1
newnewbie
by: newnewbie | last post by:
Desperately need help in creating a query to count unique values in a table. I am a Business analyst with limited knowledge of Access….My boss got me ODBC connection to the underlying tables for our...
1
by: pooh | last post by:
Hello, I am fairly new at this and I have a question. I wanted know how i can take same values within a row and return a count of those values for example: Col1 Col2 1 ...
2
GTXY20
by: GTXY20 | last post by:
Hi all, I have an ArrayList1 of multiple values. Here is an exaple of the a potential ArrayList1: a,a,b,,c,a,b,c,c,c,a,b Does anyone have any suggestion whereby I could output the count for...
1
newnewbie
by: newnewbie | last post by:
Hi, Could somebody please help me with VBA code to count unique values in a Report? Report is based on a query that has grouped values. Basically, I would like to use formula...
3
by: Umesh Anant | last post by:
Hi, I have a table which have three fields. The three columns are: username lastname Timestamp Now I have to fetch all the entries for username separately which have lastname as 'Hello' and...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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...
0
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,...
0
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...

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.