Adding Count to a select query (1 Viewer)

deletedT

Guest
Local time
Today, 11:55
Joined
Feb 2, 2019
Messages
1,218
I have a Select query that collects data from several tables.

Here's the simplified version of its sql :
Code:
SELECT tblReceiption.Rec_ID, tblReceiption.SetName, tblMaster.DrawingNo, tblUsedMaterials.ProgramNo, 
tblUsedMaterials.LaNc, tblUsedMaterials.Tools FROM tblReceiption 
INNER JOIN (tblUsedMaterials INNER JOIN tblMaster ON tblUsedMaterials.DrawingID_FK = tblMaster.DrawingID_PK) 
ON tblReceiption.DrawingID_FK = tblMaster.DrawingID_PK
WHERE (((tblReceiption.SetName)="NF17979BK101"));

I'm trying to add a column to count the number of "DrawingNo". As the following image shows, 2,3 & 4 rows have the same DrawingNo. So I need a column that shows 3 for these rows and 1 for the others.

Here's what the query looks like:



I tried to add a sub query as a column, but I wasn't able to make it work.

Any kind of advice is appreciated.
 

Attachments

  • 2019-09-12_8-38-48.jpg
    2019-09-12_8-38-48.jpg
    67.3 KB · Views: 215
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 03:55
Joined
Oct 29, 2018
Messages
21,447
Hi. Have you tried using DCount()? However, it might run a bit slow.
 

deletedT

Guest
Local time
Today, 11:55
Joined
Feb 2, 2019
Messages
1,218
Hi. Have you tried using DCount()? However, it might run a bit slow.

Sorry for not mentioning it.
Yes, I've already tried DCount. But as you explained it's too slow. If the query returns some 500 records, it takes about 10 sec to show the result, and as I scroll the result I feel a freeze time every now and then somewhat like Access is trying to refresh the result.

I was thinking of a sub-query if it's possible.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:55
Joined
Oct 29, 2018
Messages
21,447
Sorry for not mentioning it.
Yes, I've already tried DCount. But as you explained it's too slow. If the query returns some 500 records, it takes about 10 sec to show the result, and as I scroll the result I feel a freeze time every now and then somewhat like Access is trying to refresh the result.

I was thinking of a sub-query if it's possible.
Right. A subquery solution might look something like:
Code:
SELECT T1.FieldName, (SELECT Count(*) FROM TableName WHERE SomeField=T1.FieldName) AS RowNum
FROM TableName T1
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:55
Joined
May 7, 2009
Messages
19,226
you can also make a Total Query first (qryNumDrawings):

select DrawingID_FK, Count("1") As NumOfDrawing
from tblUsedMaterials
group by DrawinID_FK;

add the query (field, NumOfDrawing) to your posted query and join by DrawingID_FK.
 

deletedT

Guest
Local time
Today, 11:55
Joined
Feb 2, 2019
Messages
1,218
Right. A subquery solution might look something like:
Code:
SELECT T1.FieldName, (SELECT Count(*) FROM TableName WHERE SomeField=T1.FieldName) AS RowNum
FROM TableName T1

I followed you. But unfortunately I couldn't feel any speed progress comparing to DCount.
I'll try to add some more criteria to bring down the number of result.

Million thanks for your help.
 

deletedT

Guest
Local time
Today, 11:55
Joined
Feb 2, 2019
Messages
1,218
you can also make a Total Query first (qryNumDrawings):

select DrawingID_FK, Count("1") As NumOfDrawing
from tblUsedMaterials
group by DrawinID_FK;

add the query (field, NumOfDrawing) to your posted query and join by DrawingID_FK.

I appreciate your help. It seems faster than DCount.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:55
Joined
Oct 29, 2018
Messages
21,447
I followed you. But unfortunately I couldn't feel any speed progress comparing to DCount.
I'll try to add some more criteria to bring down the number of result.

Million thanks for your help.

Hi. Glad to hear Arnel’s suggestion worked better. Good luck with your project.
 

Users who are viewing this thread

Top Bottom