简体   繁体   中英

Access 2010 - Query to produce a total count/sum of specific occurences in multiple Tables using Union All and Sum Iif

I have a database with four tables: Table 1, 2, 3, & 4. Each table holds the following fields: ID (AutoNumber), Date (Date/Time), Name (Text), and Item (Text)

I created an SQL Query that uses Union All and a Sum Iif expression to retrieve the number of "Apples", "Oranges", and "Pears" that occur under the Item column for each individual. For example, when the Query runs, I want the query to provide me the date, the name of the individual, and the grand total of "Apples", "Oranges", and/or "Pears" for the date and individual. (12/15/2015 - Dee Wolf - 15 Apples, 10 Oranges, 20 Pears)

My query successfully counts all of the data from all 4 tables, however, it is not producing a total. 我的查询成功计数了所有4个表中的所有数据,但是,它没有产生总数。 It is, instead, giving my a 1-to-1 count for each table. For example: Table 1 will show that John has 1 apple and 1 orange, where Table 2 shows that John has 2 apples and 1 orange. When I run my query, it will show that John has 2 apples, 1 apple, 1 orange, and 1 orange (a result for each table), but I want it to just say John has 3 total apples.

I have written this code in another database and it seems to produce the results I want. But I'm not sure why I'm unable to reproduce the results. Am I overlooking something?

Here is the SQL:

SELECT Table1.Date, Table1.Name, Sum(IIf([Table1]![Item]="Apples",1,Null)) AS CountApples, Sum(IIf([Table1]![Item]="Oranges",1,Null)) AS CountOranges, Sum(IIf([Table1]![Item]="Pears",1,Null)) AS CountPears
FROM Table1
GROUP BY Table1.Date, Table1.Name
HAVING (((Table1.Date) Between [SS/SS/SS] And [EE/EE/EE]) AND ((Table1.Name)))
UNION ALL
SELECT Table2.Date, Table2.Name, Sum(IIf([Table2]![Item]="Apples",1,Null)) AS CountApples, Sum(IIf([Table2]![Item]="Oranges",1,Null)) AS CountOranges, Sum(IIf([Table2]![Item]="Pears",1,Null)) AS CountPears
FROM Table2
GROUP BY Table2.Date, Table2.Name
HAVING (((Table2.Date) Between [SS/SS/SS] And [EE/EE/EE]) AND ((Table2.Name)))
UNION ALL
SELECT Table3.Date, Table3.Name, Sum(IIf([Table3]![Item]="Apples",1,Null)) AS CountApples, Sum(IIf([Table3]![Item]="Oranges",1,Null)) AS CountOranges, Sum(IIf([Table3]![Item]="Pears",1,Null)) AS CountPears
FROM Table3
GROUP BY Table3.Date, Table3.Name
HAVING (((Table3.Date) Between [SS/SS/SS] And [EE/EE/EE]) AND ((Table3.Name)))
UNION ALL
SELECT Table4.Date, Table4.Name, Sum(IIf([Table4]![Item]="Apples",1,Null)) AS CountApples, Sum(IIf([Table4]![Item]="Oranges",1,Null)) AS CountOranges, Sum(IIf([Table4]![Item]="Pears",1,Null)) AS CountPears
FROM Table4
GROUP BY Table4.Date, Table4.Name
HAVING (((Table4.Date) Between [SS/SS/SS] And [EE/EE/EE]) AND ((Table4.Name)));
HAVING ((...) AND ((Table1.Name)))

still doesn't make much sense...

To get the totals, wrap your whole query into a Sum() / GROUP BY query:

SELECT [Date], [Name], Sum(CountApples) AS TotalApples, Sum(CountOranges) AS TotalOranges, 
                       Sum(CountPears) AS TotalPears
FROM (
    -- your UNION query goes here
)
GROUP BY [Date], [Name]

(Shamelessly lifted from https://stackoverflow.com/a/32274957/3820271 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM