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

Access '07: refine SQL so query shows unique values with totals instead of all values

17
It has been a while since my last visit, but first off, I would like to thank everyone who helped me creating my first movie database some two years ago. Although it worked pretty good, it made me realize that it didn't follow the rules of normalization at all. A typical first timer mistake I guess. So now I've made a new one that does and it works perfectly. I'm just having trouble with creating the right queries that I want. One of these problems is:

The following query returns a list of all the directors in the database and the movie they made.

Expand|Select|Wrap|Line Numbers
  1. SELECT Director.Lastname, Director.Firstname, LinkFilmDirector.[Film ID], LinkFilmDirector.[Director ID], Film.Title
  2. FROM Director INNER JOIN (Film INNER JOIN LinkFilmDirector ON Film.[Film ID] = LinkFilmDirector.[Film ID]) ON Director.[Director ID] = LinkFilmDirector.[Director ID];

The will give a result like:

Lastname Firstname Film ID Director ID Title
Spielberg Steven 1 55 Close Encounters Of The Third Kind
Spielberg Steven 2 55 Color Purple, The
Spielberg Steven 3 55 Amistad
Spielberg Steven 4 55 Catch Me If You Can

What I would like is a query that returns the name of a director just once and next to that name, in a new table, a number that shows the amount of movies.

In layman's terms:

Lastname Firstname Director ID Amount
Spielberg Steven 55 4

I hope I have made myself clear.

Thank you in advance. Margie
Jan 10 '10 #1

✓ answered by nico5038

For this you need a "group by" query like:
Expand|Select|Wrap|Line Numbers
  1. SELECT Director.Lastname, Director.Firstname, LinkFilmDirector.[Director ID], count(*)
  2. FROM Director INNER JOIN (Film INNER JOIN LinkFilmDirector ON Film.[Film ID] = LinkFilmDirector.[Film ID]) ON Director.[Director ID] = LinkFilmDirector.[Director ID]
  3. group by Director.Lastname, Director.Firstname, LinkFilmDirector.[Director ID];
Nic;o)

4 1869
nico5038
3,080 Expert 2GB
For this you need a "group by" query like:
Expand|Select|Wrap|Line Numbers
  1. SELECT Director.Lastname, Director.Firstname, LinkFilmDirector.[Director ID], count(*)
  2. FROM Director INNER JOIN (Film INNER JOIN LinkFilmDirector ON Film.[Film ID] = LinkFilmDirector.[Film ID]) ON Director.[Director ID] = LinkFilmDirector.[Director ID]
  3. group by Director.Lastname, Director.Firstname, LinkFilmDirector.[Director ID];
Nic;o)
Jan 10 '10 #2
Margie
17
Works perfectly and what quick response!
Now to study it and work with it a little so that I also understand the code instead of just accepting the answer.

Many thanks! Margie
Jan 10 '10 #3
nico5038
3,080 Expert 2GB
Check out some theory about the Group By query like http://www.blueclaw-db.com/accessque...clause_sql.htm.
Basically it's always a number of "group by" fields and one or more aggregation functions like Count(), Max(), etc.

Success !

Nic;o)
Jan 10 '10 #4
NeoPa
32,556 Expert Mod 16PB
Furthermore, all results in the SELECT clause must either be relative to an item in the GROUP BY list, or be aggregated in some way. Aggregation functions are things like Sum(), First(), Max() etc. Any reference to a field must fit either of these stipulations.

PS. Finding Jet SQL Help may be worth a look too. Access does have help on SQL. It's just not too easy to find.

Welcome (back) to Bytes!
Jan 11 '10 #5

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

Similar topics

14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
3
by: CSDunn | last post by:
Hello, I have a situation with MS Access 2000 in which I need to display report data in spreadsheet orientation (much like a datasheet view for a form). If you think of the report in terms of what...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
2
by: Phil Longworth | last post by:
Im very new to Access 97 and Im sure I should be able to do this but cant work out how. Im bulding a database for my stamp collection. I have two tables; one with details of all the individual...
3
by: Eric Ellsworth | last post by:
Hi all, Does anyone have any bright ideas for Access' tendency to add square brackets when it parses queries, then tell you that the query syntax is invalid. In my case, I'm trying to do a LEFT...
4
by: Nicolae Fieraru | last post by:
Hi All, I have a Table1 with ID, Name, Address, Purchase. I want to build a query which shows all the records from Table1 (only fields ID, Name, Address), but there shouldn't be listed records...
2
by: sara | last post by:
I am helping a non-profit track their elder clients and care given to the clients. The organization would like a report that shows various info on ALL clients, such as: # in each town, # and...
2
by: markcarroll | last post by:
I have a rather complicating query (the SQL is about a page long) so I hope I can solve this without needing to get into specifics. Basically, the database I am working on has information about...
5
by: colin spalding | last post by:
How do I design a totals query that instead of the following result YearOfAcc PremiumGBP 2004 9,142,306.95 2004 1,481,153.21 2005 11,981,987.85 2006 20,653,195.20 2006 ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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...
0
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,...
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...

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.