Solved

Simple SQL (why does it take me 2 queries when 1 should suffice?)

Posted on 2013-11-10
6
326 Views
Last Modified: 2013-11-10
See attached.

I want a query on the SalesInvoices table with the following characteristics;

1. Only include data from 2012
2. Use "group by" to produce the following format;

Joe     425
Tom   406


This should be fairly easy.  However, can it be done in a SINGLE query.  I find I need 2 queries.
TwoQueries.accdb
0
Comment
Question by:Patrick O'Dea
6 Comments
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 39637511
try this query

SELECT SalesInvoices.Salesman, SalesInvoices.Year, Sum(SalesInvoices.InvoiceValue) AS SumOfInvoiceValue
FROM SalesInvoices
GROUP BY SalesInvoices.Salesman, SalesInvoices.Year
HAVING (((SalesInvoices.Year)="2012"));
0
 
LVL 12

Accepted Solution

by:
duttcom earned 250 total points
ID: 39637515
Set up the query like this -
Single queryThis returns the results you are after.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39637523
if you want only two columns in a single query, use this query


Select A.Salesman,A.SumOfInvoiceValue
From
(
SELECT SalesInvoices.Salesman, SalesInvoices.Year, Sum(SalesInvoices.InvoiceValue) AS SumOfInvoiceValue
FROM SalesInvoices
GROUP BY SalesInvoices.Salesman, SalesInvoices.Year
HAVING (((SalesInvoices.Year)="2012"))
) As A
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Expert Comment

by:enfworks
ID: 39637524
SELECT SalesInvoices.Salesman, Sum(SalesInvoices.InvoiceValue) AS SumOfInvoiceValue
FROM SalesInvoices
WHERE (((SalesInvoices.year)='2012'))
GROUP BY SalesInvoices.Salesman;
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39637527
or this query


SELECT SalesInvoices.Salesman, Sum(SalesInvoices.InvoiceValue) AS SumOfInvoiceValue
FROM SalesInvoices
GROUP BY SalesInvoices.Salesman, SalesInvoices.Year
HAVING (((SalesInvoices.Year)="2012"));
0
 

Author Closing Comment

by:Patrick O'Dea
ID: 39637529
Thanks folks!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question