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

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

LVL 5
Abiel de GrootDeveloperAsked:
Who is Participating?
 
alchemyst999Connect With a Mentor Commented:
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
 
reb73Commented:
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
 
Abiel de GrootDeveloperAuthor Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
reb73Commented:
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
 
GRayLCommented:
SELECT - FROM - JOIN - ON - WHERE - GROUP BY - HAVING - ORDER BY

The clauses must appear in this order.
0
 
Abiel de GrootDeveloperAuthor Commented:
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
 
alchemyst999Commented:
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
 
Abiel de GrootDeveloperAuthor Commented:
many thanks.

A.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.