# how to get 2 different group totals in one access query?

I have 2 tables, t1 (col CustomerName, InvoiceNo) and t2 (col InvoiceNo, Model, SN)

Data is like:
Table1                                                     Table2
CustomerName   InvoiceNo                  InvoiceNo   Model  SN
Name1                 Inv1                            Inv1            A          A-1
Name1                 Inv2                            Inv1            B          B-1
Name1                 Inv3                            Inv1            A          A-2
Name2                 Inv4                            Inv2            B          B-2
Name2                 Inv5                            Inv2            C          C-1

I want to get a total (count(SN)) for InvoiceNo and also total for CustomerName.

SELECT t1.CustomerName, t1.InvoiceNo, COUNT(t2.SN) as Qty FROM t1 INNER JOIN t2 on t1.InvoiceNo=t2.InvoiceNo
GROUP BY t1.CustomerName, t1.InvoiceNo

This only shows the total for InvoiceNo.  How can I get total for CustomerName and InvoiceNo in one query?  Or possible?

Thank you very much in advance.  HB
Asked:
###### Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You can use the following query to do what you are looking for...

``````SELECT t1.CustomerName, COUNT(t2.SN) as Qty
FROM t1 INNER JOIN t2 on t1.InvoiceNo=t2.InvoiceNo
GROUP BY t1.CustomerName

Union all

SELECT t1.InvoiceNo, COUNT(t2.SN) as Qty
FROM t1 INNER JOIN t2 on t1.InvoiceNo=t2.InvoiceNo
GROUP BY t1.InvoiceNo;
``````

Saurabh...
0
Author Commented:
Thank you for your suggestions.  They query returns the result but it's kind of hard to summarize.  Is it possible to show the result like
Name1  Inv1   2
Name1  Inv2   1
Name1  Inv3   1
Name1          4
Name2  Inv4   3
Name2  Inv5   2
Name2  Inv6   1
Name2          6

I appreciate your inputs.  Thank you very much.
0
Commented:
Use the below mentioned query...

``````select t10.CustomerName,t10.invoice,t10.QTY
from
(SELECT t1.CustomerName,t1.InvoiceNo as invoice,COUNT(t2.SN) as Qty
FROM t1 INNER JOIN t2 on t1.InvoiceNo=t2.InvoiceNo
GROUP BY t1.CustomerName,t1.InvoiceNo

Union all

SELECT t1.CustomerName,'Total' as Invoice , COUNT(t2.SN) as Qty
FROM t1 INNER JOIN t2 on t1.InvoiceNo=t2.InvoiceNo
GROUP BY t1.CustomerName)t10
order by t10.customername;
``````

Saurabh...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Great.  That was what I wanted.  Very appreciated.  Thank you.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.