473,411 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

SQL Query

I have 4 tables

People, address, Payments and invoices the latter 3 link back to people.

I need to pull out the total of all payments and invoices for each person
but only if the sum of paymenst is less than the sum of invoices. and
everything from addresses.

I can do most of it ie get all the bits I need but not just for those that
owe money it returns all rows.
Jul 23 '05 #1
8 2228
Post the sql you have already ...

Jul 23 '05 #2
On Tue, 7 Dec 2004 16:08:34 -0000, Paul Owen wrote:
I have 4 tables

People, address, Payments and invoices the latter 3 link back to people.

I need to pull out the total of all payments and invoices for each person
but only if the sum of paymenst is less than the sum of invoices. and
everything from addresses.

I can do most of it ie get all the bits I need but not just for those that
owe money it returns all rows.


Something like this, I'll wager:

select people.Name, address.city,
sum(payments.amount) [PayTotal],
sum(invoices.amount) [InvTotal],
sum(invoices.amount) - sum(payments.amount) [StillOwed]
from people
inner join address on people.ID=address.ID
left join payments on people.ID=payments.ID
left join invoices on people.ID=invoices.ID
having sum(payments.amount) < sum(invoices.amount)
Jul 23 '05 #3

current sql is

select people.Name, address.city,
sum(payments.amount) [PayTotal],
sum(invoices.amount) [InvTotal],
sum(invoices.amount) - sum(payments.amount) [StillOwed]
from people
left join payments on people.ID=payments.ID
left join invoices on people.ID=invoices.ID
group by people.id

returns the right info but for all people, cant quite get the last bit
Still new to this, but getting their slowly.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4
On Tue, 07 Dec 2004 19:53:58 GMT, Paul Owen wrote:
current sql is (snip)returns the right info but for all people


Hi Paul,

Are you sure? You should get errors because people.Name and address.city
are in the select (not in an aggregate) but not in the group by. And
because the address table is not in the from clause at all.

Assuming these errors were made when you copied the query, you should
still get the wrong results. Have you tested it with a combination of a
person with two payments and two invoices? I think you'll the values will
be double what you expect. And it gets much worse if you have five
payments and ten invoices....

Try this one:

SELECT ID, PayTotal, InvTotal,
InvTotal - PayTotal AS StillOwed
FROM (SELECT people.ID,
COALESCE((SELECT SUM(amount)
FROM payments
WHERE payments.ID = people.ID),0) AS PayTotal,
COALESCE((SELECT SUM(amount)
FROM invoices
WHERE invoices.ID = people.ID),0) AS InvTotal
FROM people
GROUP BY people.ID) AS x
WHERE InvTotal > PayTotal

or this one:

SELECT people.ID,
COALESCE (paytot.Total,0) AS PayTotal,
COALESCE (invtot.Total,0) AS InvTotal,
COALECSE (invtot.Total,0)
- COALESCE (paytot.Total,0) AS StillOwed
FROM people
LEFT JOIN (SELECT ID, SUM(amount) AS Total
FROM payments
GROUP BY ID) AS paytot
ON paytot.ID = people.ID
LEFT JOIN (SELECT ID, SUM(amount) AS Total
FROM invoices
GROUP BY ID) AS invtot
ON invtot.ID = people.ID
WHERE COALECSE (invtot.Total,0) > COALESCE (paytot.Total,0)

(both queries untested!)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #5
I had this

select people.pid, sum(payments.amount), sum(invoice.totalexvat),
sum(payments.amount) - sum(invoice.totalexvat) as obal
from people
left join payments on people.pid = payments.pid
left join invoice on people.pid = invoice.pid
group by people.pid

and have now added having obal > 1 to the end and it seems to work but have
not cross checked any individuals yet.

Paul

"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:t5********************************@4ax.com...
On Tue, 07 Dec 2004 19:53:58 GMT, Paul Owen wrote:
current sql is

(snip)
returns the right info but for all people


Hi Paul,

Are you sure? You should get errors because people.Name and address.city
are in the select (not in an aggregate) but not in the group by. And
because the address table is not in the from clause at all.

Assuming these errors were made when you copied the query, you should
still get the wrong results. Have you tested it with a combination of a
person with two payments and two invoices? I think you'll the values will
be double what you expect. And it gets much worse if you have five
payments and ten invoices....

Try this one:

SELECT ID, PayTotal, InvTotal,
InvTotal - PayTotal AS StillOwed
FROM (SELECT people.ID,
COALESCE((SELECT SUM(amount)
FROM payments
WHERE payments.ID = people.ID),0) AS PayTotal,
COALESCE((SELECT SUM(amount)
FROM invoices
WHERE invoices.ID = people.ID),0) AS InvTotal
FROM people
GROUP BY people.ID) AS x
WHERE InvTotal > PayTotal

or this one:

SELECT people.ID,
COALESCE (paytot.Total,0) AS PayTotal,
COALESCE (invtot.Total,0) AS InvTotal,
COALECSE (invtot.Total,0)
- COALESCE (paytot.Total,0) AS StillOwed
FROM people
LEFT JOIN (SELECT ID, SUM(amount) AS Total
FROM payments
GROUP BY ID) AS paytot
ON paytot.ID = people.ID
LEFT JOIN (SELECT ID, SUM(amount) AS Total
FROM invoices
GROUP BY ID) AS invtot
ON invtot.ID = people.ID
WHERE COALECSE (invtot.Total,0) > COALESCE (paytot.Total,0)

(both queries untested!)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Jul 23 '05 #6
On Wed, 8 Dec 2004 12:19:36 -0000, Paul Owen wrote:
I had this

select people.pid, sum(payments.amount), sum(invoice.totalexvat),
sum(payments.amount) - sum(invoice.totalexvat) as obal
from people
left join payments on people.pid = payments.pid
left join invoice on people.pid = invoice.pid
group by people.pid

and have now added having obal > 1 to the end and it seems to work but have
not cross checked any individuals yet.


Hi Paul,

Test it with this data:

PEOPLE PAYMENTS INVOICES
====== =========== ===============
pid pid amount pid totalexvat
1 1 25 1 100
1 75
2 2 100 2 25
2 75
3 3 40 3 40
3 60 3 60
4 4 20 4 75
4 20 4 25
4 20
4 20
4 20

After testing, get back to my previous post to find some queries that will
work.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #7


Hi

I can now see why this does not work, but I have tried your examples and
cant get either of these to work.

I tried starting with a basic query using coalesce but that gives an
error too

SELECT people.pID, COALESCE (payments.amount,0)
FROM people,payments
left join payments on people.pid = payments.pid
group by people.pid
Paul

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #8
On 15 Dec 2004 08:35:32 -0600, Paul Owen wrote:
I tried starting with a basic query using coalesce but that gives an
error too

SELECT people.pID, COALESCE (payments.amount,0)
FROM people,payments
left join payments on people.pid = payments.pid
group by people.pid


Hi Paul,

Next time, it helps to post the CREATE TABLE and INSERT statements to
recreate your table and fill it with some sample data, so that others can
run your query and test what happens. If you get error messages, it also
helps to include them in the message.

In this case, your syntax is wrong. You join the payments table twice,
once with the "old-style" join syntax (from tablename, tablename, ...) and
once with the "new-style" join syntax (from tablename [jointype] join
tablename on conditions ...). For outer joins, only the "new-style" is
safe. Change your query to this:

SELECT people.pID, COALESCE (payments.amount,0)
FROM people -- <-- Change is here!!
left join payments on people.pid = payments.pid
group by people.pid
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
4
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets...
14
by: Dave Thomas | last post by:
If I have a table set up like this: Name | VARCHAR Email | VARCHAR Age | TINYINT | NULL (Default: NULL) And I want the user to enter his or her name, email, and age - but AGE is optional. ...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.