473,386 Members | 1,803 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,386 software developers and data experts.

SQL JOIN SYNTAX

MAB
Erland Sommarskog <so****@algonet.se> wrote in message
news:Xn**********************@127.0.0.1...
MAB (fk*****************@yahoo.com) writes:
What I want is the sum of the amounts of the last payments of all
customers. Now the last payment of a customer is not necessarily the one
with the highest paymentid for that customer BUT it is the one with the
highest paymentid on the MOST RECENT date. We dont keep the time part
just the date so if there are more than 1 payments of a customer on a
date ( and there are many such cases ) only then the paymentid decides
which is the last payment. Further the last payment may be the last as
of today but I may want to find the sum of all the last payments upto
say March 1, 2003 or any date. My own solution is too slow even it is
correct.


This solution is not tested, as you did not provide any sample data:

SELECT SUM(p3.amount)
FROM Payments p3
JOIN (SELECT paymentid = MAX(p2.paymentid)
FROM Payments p2
JOIN (SELECT p1.customerid, mostrecent = MAX(p1.date)
FROM Payments p1
WHERE p1.date <= '20030301'
GROUP BY p1.customerid) AS p1
ON p1.customerid = p2.customerid
AND p1.mostrecent = p2.date) AS p2
ON p3.paymentid = p2.paymentid

This solution is for SQL Server only. I don't know Access, so I can't
help with that.

As for performance, this is likely to be a case of finding the best
indexes. Clustered on (date, customerid) and nonclustered in (paymentid)
maybe.

Thanks. This looks fascinating. It looks correct too (although I havent
fully verified that). From this my next question
Is it possible to write your query in older join syntax like

FROM Payments p1, Payments p2
WHERE p1.customerid = p2.customerid etc.

Or is it that the newer syntax is superior such that you can do things with
it that you cant do with the older one?
I cant see how to write such a query the older way because you have only one
SELECT Clause there

Many thanks again

Jul 20 '05 #1
4 13497
Try:

SELECT SUM(p3.amount)
FROM Payments p3, (
SELECT paymentid = MAX(p2.paymentid)
FROM Payments p2, (SELECT p1.customerid,
mostrecent = MAX(p1.date)
FROM Payments p1
WHERE p1.date <= '20030301'
GROUP BY p1.customerid) AS p1
WHERE p1.customerid = p2.customerid
AND p1.mostrecent = p2.date) AS p2
WHERE p3.paymentid = p2.paymentid ;

--
- Anith
( Please reply to newsgroups only )
Jul 20 '05 #2
MAB
SELECT SUM(p3.amount)
FROM Payments p3
JOIN (SELECT paymentid = MAX(p2.paymentid)
FROM Payments p2
JOIN (SELECT p1.customerid, mostrecent = MAX(p1.date)
FROM Payments p1
WHERE p1.date <= '20030301'
GROUP BY p1.customerid) AS p1
ON p1.customerid = p2.customerid
AND p1.mostrecent = p2.date) AS p2
ON p3.paymentid = p2.paymentid

This solution is for SQL Server only. I don't know Access, so I can't
help with that.


Unfortunately this doesnt seem to work :(
It only brings the amount of 1 payment. So if you replace SUM(p3.amount)
with something like

p3.Paymentid, p3.customerid, p3.amount you only get 1 row which is obviously
incorrect because I need one paymentid ( the last one ) for each customer
and SUM(p3.amount) should get the total of all those records.
Can you fix this?

Jul 20 '05 #3
>> Can you fix this? <<

Not entirely, since I am not familiar with your requirements. All I have
done is simply changed the query with older syntax. If you need an exact &
accurate working solution, you'll have to provide the table DDLs (CREATE
TABLE statements ), sample data (INSERT statements) and expected results
along with a brief explanation of the required logic.

--
- Anith
( Please reply to newsgroups only )
Jul 20 '05 #4
MAB (fk*****************@yahoo.com) writes:
Unfortunately this doesnt seem to work :(
It only brings the amount of 1 payment. So if you replace SUM(p3.amount)
with something like

p3.Paymentid, p3.customerid, p3.amount you only get 1 row which is
obviously incorrect because I need one paymentid ( the last one ) for
each customer and SUM(p3.amount) should get the total of all those
records.
Can you fix this?


Maybe. As I pointed out in my original posting, the solution was
untested as you did not provide sample data and the expected result
from the sample.

So please post the following:

o CREATE TABLE statement for your table (which you already have, but
would be convenient with having all in one post.)
o INSERT statements with sample data.
o The expected result from the sample.

It would also be helpful if you repeated the problem as such, since you
did not post your reply as a followup to my post. By posting a followup,
it's easy for me to go back and review previous articles in the thread.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5

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

Similar topics

3
by: Phil Powell | last post by:
I'm not kidding, the only reason yesterday you didn't hear from me was because I wasn't coding, but today I am doing something quick, and yes, as always it failed.. right at the SQL statement: ...
2
by: Pierre Fortin | last post by:
This quest for understanding started very innocently... A simple error on my part, passing on args as "args" instead of "*args" to os.path.join() led me to wonder why an error wasn't raised... ...
2
by: Martin | last post by:
I am now working on SQL Server 2000 having had previous experience on a different database. Both of the OUTER JOIN syntaxes is different from what I am used to and I am finding it slightly...
3
by: mheydman | last post by:
I apologize if this has been asked before- I searched google but could not find a concrete answer. I recently inherited a database whose t-sql code is written in a format that I find difficult...
8
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
2
by: Good Man | last post by:
Hi there Yes, I've read about JOINs, albeit after coding for a couple of years already using queries like the following: "SELECT m.LastName, m.FirstName, o.Address FROM members m, offices o...
3
by: Ian Boyd | last post by:
i know nothing about DB2, but i'm sure this must be possible. i'm trying to get a client to create a view (which it turns out is called a "Logical" in DB2). The query needs a LEFT OUTER JOIN, but...
4
by: Jean-Claude | last post by:
Hi, which is the faster query ? (of course, in my case the real queries are more complex) 1/ select * from file1 a join file2 b on b.key=a.key where b.data=123 and b.name='TEST'
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
7
by: dunleav1 | last post by:
I have an application that uses the old join syntax instead of the SQL92 standards join syntax. I need to justify changing the code to the new standard. Is there any performance issue related to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.