Ask Your Question

Base Query not counting/summing correctly

asked 2020-03-03 09:06:35 +0100

Jon170579 gravatar image

updated 2020-07-27 20:10:49 +0100

Alex Kemp gravatar image

OS 10.15.2 Libre Office

See File: C:\fakepath\J.D. Wetherspoons.odb

in "Z_Test_Single_Qry" I have selected 4 fields 3 different tables, there is a 4th table but I am not using any fields from it. I want base to count the unique visit dates for each Pub_Name, then count total drinks purchased from that visit date as there might be more than 1 receipt per visit, then finally I want to sum the value per visit.

The problem is when I add all the tables the count on visit date is counting the total line per visit, ie if 2 drinks were bought in the same visit it is returning 2 as the answer when it should be 1.

I have managed to do 2 seperate queries "z_Test_Multiple_1" & "z_Test_Multiple_2" which does the count for visit date correct and drink count and sum correct respectively. but when I try and link these together in a third query using Pub name as a link I get a syntax error and I cannot even save the 3rd query

I need all on the fields counting/summing correctly to use in a report

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2020-03-04 00:23:33 +0100

cpb gravatar image

updated 2020-03-04 02:46:46 +0100

i think that i agree with @Ratslinger who said that the table "tbl_Visit_Time" appears to be superfluous to requirements.

the field "Time" should probably reside in the table "tbl_Visit_Date".

anyway i'm posting a solution to your issue.

from the 'Base' window hit 'Queries' then hit 'Create Query in SQL View' (query design window opens).

paste the code in the query design window.

activate the 'SQL' icon on the toolbar and hit F5 (execute).

select p."Pub_Name", count(distinct d."Visit_ID") "CountVisits",
    count(*) "Transactions", sum(r."Value") "Receipt"
    (select "Visit_ID", "Pub_ID" from "tbl_Visit_Date") d
    (select "Visit_Time_ID", "Visit_ID" from "tbl_Visit_Time") t
    on t."Visit_ID" = d."Visit_ID"
    (select "Visit_Time_ID", "Value" from "tbl_Receipt") r
    on r."Visit_Time_ID" = t."Visit_Time_ID"
    (select "Pub_Info_ID", "Pub_Name" from "ztbl_Pub_Info") p
    on p."Pub_Info_ID" = d."Pub_ID"
group by "Pub_Name"
order by "Pub_Name"
edit flag offensive delete link more



Think you missed the objective. Each visit must have separate totals. Try and pick one like The Friar Penketh - It will make your head spin just trying to connect the dots. Visit date needs to play a part in this and be a part of the result set. And then there is the question of what to do if you visit two or more different times on the same date?

Ratslinger gravatar imageRatslinger ( 2020-03-04 00:48:46 +0100 )edit

@Ratslinger the OP stated that each of the 2 separate queries "z_Test_Multiple_1" & "z_Test_Multiple_2" returned accurate figures but the the problem was in merging those queries. am i missing something?

cpb gravatar imagecpb ( 2020-03-04 01:13:34 +0100 )edit


The question is a bit hard to read but here is one of the items:

"...then finally I want to sum the value per visit."
Ratslinger gravatar imageRatslinger ( 2020-03-04 01:29:23 +0100 )edit

answered 2020-03-03 19:33:37 +0100

Ratslinger gravatar image


The answer to the question is that you have not included the Visit_Date in the grouping so it is simply totaling all for a particular Pub. You would also need to include this in the select otherwise you do not know which of the visits was which.

It also may be you have overly complicated your tables and the query. Not sure why there are different table for Visit_Time and Visit_Date. With the query I would select each particle of information and then use a left join to create the single record being output.

Just trying to figure what goes to what and exactly what you expect from the report was too confusing to go any further.

I would simplify all before proceeding.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-03-03 09:06:35 +0100

Seen: 186 times

Last updated: Mar 04 '20