Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

complex Query (working out totals)

Posted on 2011-03-25
8
Medium Priority
?
200 Views
Last Modified: 2012-05-11
Hi,

I would like help with a quite complex SQL statement.  It involves 2 tables.

The first table (table1) look like this:

table1

identifier      Req      Parent
ID-001      5      FM
ID-002      4      FM
ID-003      12      FM
ID-004      21      PL
ID-005      40      FM
ID-006      1      PL
ID-007      10      PL
ID-008      8      FM

The second table (table2) looks like this

identifier      QTY
ID-001      2
ID-001      4
ID-001      1
ID-002      30
ID-004      40
ID-002      1
ID-007      10
ID-008      8

I’m trying to produce a recordset that could populate a list control with the data that would result from following these steps:

1.      Select table1.identifiers where table1.Parent = FM.
2.      For each row in table2 search table.identifer for a matching record, then add up the table2.QTY values for resulting records found
3.      Zero should added where no values can be found

Given the example above it should produce the following results:

identifier      Req      qty
ID-001      5      7
ID-002      4      31
ID-003      12      0
ID-005      40      0
ID-008      8      8

Could this be done?
0
Comment
Question by:andyw27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Accepted Solution

by:
davehilditch earned 1000 total points
ID: 35215129
select a.identifer, a.req, sum(b.qty) as qty
from table1 as a
left join table2 as b
on a.identifier = b.identifier
group by a.identifier, a.req
0
 
LVL 4

Expert Comment

by:davehilditch
ID: 35215138
actually, that will give nulls for id3 and id5 so modify the sum(b.qty) part to:

isnull(sum(b.qty), 0) as qty
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 1000 total points
ID: 35215246
is this in access?

SELECT tbl1.identifier, tbl1.req, nz(Sum(tbl2.qty),0) AS Totqty
FROM tbl1 LEFT JOIN tbl2 ON tbl1.identifier = tbl2.identifier
WHERE tbl1.parent="fm"
GROUP BY tbl1.identifier, tbl1.req;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:andyw27
ID: 35215398
Thanks for the suggestion, just trying to get them to work within a vba function.
0
 

Author Comment

by:andyw27
ID: 35215407
yes its access.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35215466
i suggest that you try the query first,

and post your vba codes here so we can correct it.
0
 

Author Comment

by:andyw27
ID: 35216361
Excellent got it to work, if you are both happy I'll do a 50/50 split of points for this.
0
 
LVL 4

Expert Comment

by:davehilditch
ID: 35216654
that's cool by me - it's up to you how you split the points anyway
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
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.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Suggested Courses

636 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