473,383 Members | 1,843 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,383 software developers and data experts.

SQL table join + count help

I have 3 tables: ARTISTS, ART and PAINTINGS
ARTISTS has columns (data):
userid (adam, betty, gil, jack, larry, steve, tyler)
full_name (firstname lastname)

ART has columns:
art_id (auto number created when new art_name added via application)
art_name (name of piece of art entered)
designed_by (populated with userid)

PAINTINGS has columns:
paint_id (auto number created when new painting_name added via application)
painting_name (name of painting entered)
painted_by (populated with userid)

I need a sql statement for a report that will return a list of the userids, counts(art_id), and count(paint_id).

Some userids will exist in both tables some will not. If not then count = 0.

Would like result set format to look like this:

artist ART_CNT PAINT_CNT
adam 1 5
betty 3 0
gil 4 4
jack 6 0
larry 2 2
steve 0 9
tyler 3 7

I can get the userids and counts from each table but don't know how to join or merge the sql to get the result set format of 3 columns from one query. Is this possible???

Here are the seperate select statements:

SELECT designed_by, Count(art_id)
FROM ART
GROUP BY designed_by

SELECT painted_by, Count(paint_id)
FROM PAINTINGS
GROUP BY painted_by

Thanks for your help in advance.
Jan 25 '08 #1
4 2532
Delerna
1,134 Expert 1GB
Try this
Expand|Select|Wrap|Line Numbers
  1. SELECT userid as artist,ART_CNT,PAINT_CNT
  2. FROM ARTISTS 
  3. LEFT JOIN
  4. (   SELECT designed_by, Count(art_id) as ART_CNT
  5.     FROM ART
  6.     GROUP BY designed_by
  7. ) b on a.userid = b.designed_by
  8. LEFT JOIN
  9. (    SELECT painted_by, Count(paint_id) as PAINT_CNT
  10.      FROM PAINTINGS
  11.      GROUP BY painted_by
  12. ) c on a.userid = c.painted_by
  13.  
LEFT JOIN is allowed in SQL server but I don't think it will work in access
I think it should be either LEFT OUTER JOIN or LEFT INNER JOIN
Can't remember which as I havn't used access in a while
Jan 25 '08 #2
Thanks for the fast reply. What do the a, b, c reference?

b on a.userid = b.designed_by

When executing it, an error comes back as invalid identifier "b"
Jan 25 '08 #3
Delerna
1,134 Expert 1GB
the a b and c are alias's for the table and subqueries
I did that just to make my typing easier in the post

the alias shuld be recognizable by access. Check your query carefully
it seems you may have left the alias off
Jan 25 '08 #4
Delerna
1,134 Expert 1GB
No, sorry I left the alias off the table

this bit
SELECT userid as artist,ART_CNT,PAINT_CNT
FROM ARTISTS

should be
SELECT userid as artist,ART_CNT,PAINT_CNT
FROM ARTISTS a
Jan 25 '08 #5

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

Similar topics

1
by: dan | last post by:
i have 2 tables that i want to count TABLE 1 categories --id --name forum_comments --id --category_id
6
by: Jason | last post by:
I need to populate a table from several sources of raw data. For a given security (stock) it is possible to only receive PARTS of information from each of the different sources. It is also...
2
by: Matik | last post by:
Hello everyone, Small and (I think) very simple quesiton;-) which makes me creazy. Let's say I have two tables listed below: T1 ==== IDX ==== 1
2
by: tdmailbox | last post by:
I have a database with three tables tbl_listings - listings of houses on for sale tbl_intersted - table which tracks if a user is interested in the listing, it has two columns mls(the key for...
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
4
by: Laura | last post by:
Here's the situation: I'm trying to use an update query to copy data from one row to another. Here is the situation: I have 5 companies that are linked to each other. I need to show all 5...
20
by: p175 | last post by:
Hi people, I have a stored procedure that creates many Global temporary session tables. Into each of these tables go the results of various processing using relational division all keyed and...
1
by: chiume | last post by:
First thank you very much for all your helps, and pls. Ok, this is what I am trying to do:, 1) I am trying to get the number of employees that has completed all their online training within...
4
by: teddysnips | last post by:
I have a simple table with the following data: fldYear fldCode1 fldCode2 2000 ABC1 ABC12 2000 ABC1 ABC13 2001 ABC1 ABC12 2002 ABC1 ABC12 2002 ...
2
by: filmar | last post by:
I have two tables and need to recieve counts of each groups in columns. The 1st table is necessary to recieve exactly 4 rows even if there no one match condition id the 2nd table. It have to count...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.