Link to home
Start Free TrialLog in
Avatar of thandel
thandel

asked on

Counting simliar items in a query result

I have a table (tMain) of sales... one entry for each sale.  In this table is the model (FrameModel) and an office (Office, currently just testing for all offices in the table.)  I would like to run a query that will pull the max similar model sold and display their count and also find their percentage (based on the total number of FrameModels sold) .  I am using the following query to try to pull this and a date range based on a form for the user,  (Basically trying to find models that have sold over 1 to find out "top model" sellers.

Its returning results but  I know for sure that there are other fame models not even showing up that have many sales (over 10)

SELECT tMain.DOS, tMain.FrameModel, Count(tMain.FrameModel) AS CountOfFrameModel, tMain.FrameLine, tMain.Office
FROM tMain
GROUP BY tMain.DOS, tMain.FrameModel, tMain.FrameLine, tMain.Office
HAVING (((tMain.DOS) Between [Forms]![FReportSelect]![DateStart] And [Forms]![FReportSelect]![DateEnd]) AND ((Count(tMain.FrameModel))>1))
ORDER BY Count(tMain.FrameModel) DESC;

Thank you.
SOLUTION
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is irrelevant whether you place the condition to WHERE or HAVING in this case. You should focus on the date range entered which is the most likely reason for missing data on output.

The difference can be in the date interpretation - American vs. Canadian date may cause the difference etc.

In other words: Are you sure there is over 10 sales for given DOS and FrameModel?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thandel
thandel

ASKER

Hi

Pcelba, yes I see all the frame models in that date range for the given office. (123 of them found)

Aikimark - query works but is showing a count of 2 for all models when I know its not accurate as some have more than 2
Avatar of thandel

ASKER

Crystal getting an error with your query: cannot have aggregarte function where clause
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I can't see the data nor the form.  The problem is most likely one of those two places.
Avatar of thandel

ASKER

HI Pcelba... its a date range:

Between [Forms]![FReportSelect]![DateStart] And [Forms]![FReportSelect]![DateEnd]
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thandel

ASKER

Thanks let me implement this....
Avatar of thandel

ASKER

thank and sorry for the delay on this.... so I ran it but some of the models are not being reporting correctly.  I've attached sample data. for example if you run with a date range of 1/1/18 - 5/20/2018 the Model "Utrecht" is showing as qty 4 but when I count manually its 7.  Other models have the same miscount error.

Thoughts?
Avatar of thandel

ASKER

Sorry forgot to upload the file after selecting.. here it is.
Sample.xls
Manual count shows 8 rows in above file so you should first make sure what file/table are you querying.

Where are your data stored?  In Access database or in XLS file?
Avatar of thandel

ASKER

in access database
Avatar of thandel

ASKER

Hi any thoughts about why the query is not pulling up data fully?
You did not post the final version of your query. It would also help to post the accdb database with sample data (tMain table should be enough in this db).

The XLS file is not sufficient to tell what's wrong because your count (7) is different from my count (8) so we are even not sure whether are you looking at the same file.
Avatar of thandel

ASKER

Sorry I didn't see you needed my query:

SELECT Count(tMain.FrameModel) AS CountOfFrameModel, tMain.FrameModel, tMain.FrameLine, tMain.JobType
FROM tMain
WHERE (((tMain.DOS) Between [Forms]![FReportSelect]![DateStart] And [Forms]![FReportSelect]![DateEnd]) AND ((tMain.FrameOWF)=False) AND ((tMain.Office)=[Forms]![FReportSelect]![LocSelect]) AND ((tMain.Status)<>"Cancelled")) OR (((tMain.DOS) Between [Forms]![FReportSelect]![DateStart] And [Forms]![FReportSelect]![DateEnd]) AND ((tMain.FrameOWF)=False) AND ((tMain.Status)<>"Cancelled") AND (([Forms]![FReportSelect]![LocSelect])="ALL"))
GROUP BY tMain.FrameModel, tMain.FrameLine, tMain.JobType
HAVING (((Count(tMain.FrameModel))>1) AND ((tMain.FrameLine)<>"MA Health") AND ((tMain.JobType)="Rx")) OR (((Count(tMain.FrameModel))>1) AND ((tMain.FrameLine)<>"MA Health") AND ((tMain.JobType)="Rx"));
The query has some parts duplicated in WHERE and HEAVING clause. Why? OTOH, this should not make problem but I would recommend to fix it.

As you are not providing sample database you should do two things:
1) Display [DateStart] and [DateEnd] inside the application to ensure you are working with expected values. Optionally you may add the two form fields into the column list in the SELECT command
2) Execute the query in MS Access directly - it will allow to review the result set
Avatar of thandel

ASKER

I have the having as I have a count going to ensure that only a model of more than (1) is on the report... I didn't want single count models to be included as I consider anything with a count of more than (1) a "top" model.
This is OK and I understand it as it is working fine for you.

 But remember you have  

HAVING  condition  OR  condition

so why to have two exactly same conditions in HAVING?

If you would need more specific help then we would need data sample from you. Simply create a sample accdb database containing just the data you would like to query and the expected result.
Avatar of thandel

ASKER

Thank you.
Many good hints were provided in this question.