Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL select in Access db. Trouble with a COUNT(*).

Posted on 2009-05-06
8
Medium Priority
?
263 Views
Last Modified: 2012-05-06
Hi all,

Trying to get this to work. I make a simple select from a table called 'SH_Products' and for each product I want a numberic value for the number of images that product has within another table called 'SH_ProdGals'.

The Join happens on:

SH_Products.Product_ID = SH_ProdGals.ProdGals_ProdID

I have this sort of thing working on SQL. However, I now need to replicate it and get it going on Access too.

Kind regards

Abiel M de Groot
strSQL = "SELECT SH_Products.Product_ID, "&_
"SH_Products.Product_ID, "&_
"SH_Products.Product_Name, "&_
"SH_Products.Product_Code, "&_
"SH_Products.Product_IconPic, "&_
"SH_Products.Product_ExMedia1, "&_
"SH_Products.Product_ExMedia2, "&_
"SH_Products.Product_ExMedia3, "&_
"SH_Products.Product_ExMedia4, "&_
"SH_Products.Product_Active, "&_
"SH_ProdGals.NumberOfImages "&_
"FROM SH_Products left outer join (select SH_ProdGals.ProdGals_PicID, COUNT(*) as NumberOfImages from  SH_ProdGals group by SH_ProdGals.ProdGals_PicID) SH_ProdGals on SH_Products.Product_ID = SH_ProdGals.ProdGals_ProdID WHERE SH_Products.Product_SubSiteID = " & Session("Subsite_ID") & " Order By SH_Products.Product_ID;"
Set rsData = MyConn.Execute(strSQL)
If Not rsData.EOF Then
ArrProds = rsData.GetRows()
End If
rsData.Close

Open in new window

0
Comment
Question by:Abiel de Groot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 25

Expert Comment

by:reb73
ID: 24318041
Try this -
strSQL = "SELECT SH_Products.Product_ID, "&_
"SH_Products.Product_ID, "&_
"SH_Products.Product_Name, "&_
"SH_Products.Product_Code, "&_
"SH_Products.Product_IconPic, "&_
"SH_Products.Product_ExMedia1, "&_
"SH_Products.Product_ExMedia2, "&_
"SH_Products.Product_ExMedia3, "&_
"SH_Products.Product_ExMedia4, "&_
"SH_Products.Product_Active, "&_
"SH_ProdGals.NumberOfImages = (SELECT COUNT(*) FROM SH_ProdGals AS PG WHERE PG.ProdGals_ProdID = SP.Product_ID) "&_
"FROM SH_Products AS SP
"WHERE SH_Products.Product_SubSiteID = " & Session("Subsite_ID") & " Order By SH_Products.Product_ID;"

Open in new window

0
 
LVL 5

Author Comment

by:Abiel de Groot
ID: 24318156
Hi reb73,

I have reduced the statement to only include teh stuff we need to get going. The rest I can put back later.


Gettting an error, but different than  before!
------------------------------------------------------
Microsoft JET Database Engine (0x80040E10)
No se han especificado valores para algunos de los parámetros requeridos.
(Don't have specific  values for some of the parameters requiered)

Line xxx
strSQL = "SELECT SH_Products.Product_ID, "&_
"SH_ProdGals.NumberOfImages = (SELECT COUNT(*) FROM SH_ProdGals AS PG WHERE PG.ProdGals_ProdID = SP.Product_ID) "&_
"FROM SH_Products AS SP "&_
"WHERE SH_Products.Product_SubSiteID = " & Session("Subsite_ID") & " Order By SH_Products.Product_ID;"
 
Set rsData = MyConn.Execute(strSQL) '<<<<< This is line xxx ------------------------------------------------------
If Not rsData.EOF Then
ArrProds = rsData.GetRows()
End If
rsData.Close

Open in new window

0
 
LVL 25

Expert Comment

by:reb73
ID: 24318327
I aliased the table SH_Products as SP, but missed out on using the alias consistently.. Try -
strSQL = "SELECT SP.Product_ID, "&_
"SH_ProdGals.NumberOfImages = (SELECT COUNT(*) FROM SH_ProdGals AS PG WHERE PG.ProdGals_ProdID = SP.Product_ID) "&_
"FROM SH_Products AS SP "&_
"WHERE SP.Product_SubSiteID = " & Session("Subsite_ID") & " Order By SP.Product_ID;"
 
Set rsData = MyConn.Execute(strSQL) '<<<<< This is line xxx ------------------------------------------------------
If Not rsData.EOF Then
ArrProds = rsData.GetRows()
End If
rsData.Close

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:GRayL
ID: 24319573
SELECT - FROM - JOIN - ON - WHERE - GROUP BY - HAVING - ORDER BY

The clauses must appear in this order.
0
 
LVL 5

Author Comment

by:Abiel de Groot
ID: 24322786
Hi all,

Sorry still getting the above error. I have reduced the statement even further. I removed the WHERE and ORDER BY so as to keep just the stuff we need for now.:

--------------------------------------------------------

strSQL = "SELECT SP.Product_ID, SH_ProdGals.NumberOfImages = (SELECT COUNT(*) FROM SH_ProdGals AS PG WHERE PG.ProdGals_ProdID = SP.Product_ID) FROM SH_Products AS SP;"

--------------------------------------------------------

Kind regards

A.
0
 
LVL 3

Expert Comment

by:alchemyst999
ID: 24325521
Perhaps try this:  (I have also removed the subselect so as to make it O(N) as opposed to O(N^2).


SELECT 	SP.Product_ID, PG.NumImages
FROM 	SH_Products AS SP
	LEFT JOIN 
	(SELECT	PG.ProdGals_ProdID, COUNT(*) As NumImages
	FROM 	SH_ProdGals AS PG 
	GROUP 	BY PG.ProdGals_ProdID) As PG
	ON 	SP.Product_ID = PG.ProdGals_ProdID

Open in new window

0
 
LVL 3

Accepted Solution

by:
alchemyst999 earned 2000 total points
ID: 24325560
Also, if you wanted to keep the subselect, I think you need to change it thus:
strSQL = "SELECT SP.Product_ID, (SELECT COUNT(*) FROM SH_ProdGals AS PG WHERE PG.ProdGals_ProdID = SP.Product_ID) As NumberOfImages FROM SH_Products AS SP;"

Open in new window

0
 
LVL 5

Author Closing Comment

by:Abiel de Groot
ID: 31578619
many thanks.

A.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question