473,652 Members | 3,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Help Needed

MAB
I have a table

Create Table Payments {
paymentid int,
customerid int,
amount int,
date datetime
}

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.
SELECT SUM( AMOUNT )
FROM PAYMENTS AS P1
WHERE PAYMENTID =
( SELECT MAX( PAYMENTID ) FROM PAYMENTS AS P2 WHERE P1.CUSTOMERID =
P2.CUSTOMERID AND DATE =
( SELECT MAX(DATE) FROM PAYMENS AS P3 WHERE P3.CUSTOMERID = P2.CUSTOMERID
AND DATE < #9/8/03# ))

What would be the most efficient solution to this.

Both in SQL Server and in Access 2000

thx in advance



Jul 20 '05 #1
3 2002
mab
Unfortunate I cannot test it

---Make view and the join the view with SELECT statement
CREATE VIEW MY_VIEW
AS
SELECT CUSTOMERID,AMOU NT AS AMOUNT
FROM PAYMENTS
INNER JOIN
(
SELECT CUSTOMERID,AMOU NT ,MAX(DATE) AS DATE FROM PAYMENTS
GROUP BY AMOUNT,CUSTOMER ID
) P1
ON P1.CUSTOMERID=P AYMENTS.CUSTOME RID
---------------------------
SELECT SUM(AMOUNT) FROM MY_VIEW

"MAB" <fk************ *****@yahoo.com > wrote in message
news:bj******** ****@ID-31123.news.uni-berlin.de...
I have a table

Create Table Payments {
paymentid int,
customerid int,
amount int,
date datetime
}

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.
SELECT SUM( AMOUNT )
FROM PAYMENTS AS P1
WHERE PAYMENTID =
( SELECT MAX( PAYMENTID ) FROM PAYMENTS AS P2 WHERE P1.CUSTOMERID =
P2.CUSTOMERID AND DATE =
( SELECT MAX(DATE) FROM PAYMENS AS P3 WHERE P3.CUSTOMERID = P2.CUSTOMERID AND DATE < #9/8/03# ))

What would be the most efficient solution to this.

Both in SQL Server and in Access 2000

thx in advance



Jul 20 '05 #2
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.paymenti d)
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.
--
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 #3
"MAB" <fk************ *****@yahoo.com > wrote in message
news:bj******** ****@ID-31123.news.uni-berlin.de...
I have a table

Create Table Payments {
paymentid int,
customerid int,
amount int,
date datetime
}

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.
SELECT SUM( AMOUNT )
FROM PAYMENTS AS P1
WHERE PAYMENTID =
( SELECT MAX( PAYMENTID ) FROM PAYMENTS AS P2 WHERE P1.CUSTOMERID =
P2.CUSTOMERID AND DATE =
( SELECT MAX(DATE) FROM PAYMENS AS P3 WHERE P3.CUSTOMERID = P2.CUSTOMERID
AND DATE < #9/8/03# ))

What would be the most efficient solution to this.

Both in SQL Server and in Access 2000

thx in advance


With SQL Server 2000, one can use a UDF.

CREATE VIEW Now (date_time)
AS
SELECT CURRENT_TIMESTA MP

CREATE FUNCTION LatestPaymentsA llCustomers
(@d DATETIME = NULL)
RETURNS TABLE
AS
RETURN(
SELECT SUM(P2.amount) AS total
FROM (SELECT customerid, MAX(date) AS latest
FROM Payments
WHERE date <= COALESCE(@d, (SELECT date_time FROM Now))
GROUP BY customerid) AS P1
INNER JOIN
Payments AS P2
ON P1.customerid = P2.customerid AND
P1.latest = P2.date AND
NOT EXISTS (SELECT * FROM Payments
WHERE customerid = P1.customerid AND
date = P1.latest AND
paymentid > P2.paymentid)
)

-- For latest payments as of now
SELECT total
FROM LatestPaymentsA llCustomers(DEF AULT)

-- For latest payments as of a particular date, e.g., 20030301
SELECT total
FROM LatestPaymentsA llCustomers('20 030301')

Regards,
jag
Jul 20 '05 #4

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

Similar topics

0
2765
by: System | last post by:
Hello All, Redhat 9.0 Mysql 3.23.56 ==> Running I want to upgarde to 4.0.13 but this is the error it says: # rpm -Uvh MySQL-server-4.0.13-0.i386.rpm warning: MySQL-server-4.0.13-0.i386.rpm: V3 DSA signature: NOKEY, key ID 5072e1f5 error: Failed dependencies: libmysqlclient.so.10 is needed by (installed) mod_auth_mysql-1.11-12
8
1605
by: Stephen | last post by:
I am trying to add some code to below to include a datatable and fill the datatable. The reason for doing this is so as I can check to see whether there are any rows returned by the stored procedure. If there are no records returned then this would give me an indicator and I can re-direct the page somewhere more appropriate. Well this is the theory. I have never used datatables before and am not sure how to implemenet what I want so I was...
13
1736
by: Joe Feldman | last post by:
This position is located in the South Bay Area in Northern California. If you are interested please send me your resume in a word .doc so that I can review it. If this does not look like a match, we have over 100 other open positions. Senior Member of Technical Staff/ Principal Engineer- Protocols Infrastructure
0
1794
by: Cindy B | last post by:
Please send your resume and position to Cindy@AtlanticResource.com! I CAN NOT accept candidates that ARE OUTSIDE OF THE US! NO PHONE CALLS PLEASE! Email your resume to me! Position:SQL SERVER/PEOPLESOFT DBA Date: 03/07/05 Location: Richmond, VA Duration: 5 month conract to hire Salary: 65,000-80,000K Contract to Hire 5 months
3
2259
by: Wade | last post by:
I would like to install the .Net 1.1 framework on a Web Server running W2K to be able to run ASP.NET files, but I'm not sure where to find the files I need for the .Net framework. I have ".NET Framework 1.1 Service Pack 1" (NDP1.1sp1-KB867460-X86.exe) and "ASP.NET Security Update for .NET Framework 1.1 SP1" (NDP1.1sp1-KB886903-X86.exe). The SP1 will not install unless 1.1 is already installed (I thought that SP1 was the whole 1.1...
17
2337
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object)." Does that mean that in the following code, *p does not have to be evaluated since its side...
5
1707
by: Steve | last post by:
Hi, I am sitting down to design our next set of internal apps. I would like to approach this in a way that would allow me to break logical parts of the application that handle specific tasks into modules. For example, assuming we have 4 main tasks to accomplish: - Printing - Editing - Viewing - Inventory Management
0
1476
by: ultradiv | last post by:
I have a VB.NET application partly built that produces an xml output (just a file at present) I have a .NET webserver and SQLserver 2000 I need to be able to send the xml to the webserver/database (some crunching is needed before the data is stored) The database (stored proc.) will reply with several small pieces of data which need to be sent back to the client app. I am thinking that all of this could be accomplished in one call to a...
28
1929
by: Ian Davies | last post by:
Hello I would appreciate some help from someone who has knowledge of working with css, php javascript and how they interact. Ive been working on a task for the last few days and have started to hit a brick wall. I need general advice on whether I m tackling the problem the correct way and some solutions for my current problems Ive posted my project below where ive detailed the current problems im having...
37
2391
by: C_guy | last post by:
Does anyone know of a (hopefully free) tool that can traverse a project and determine which "#include"s are not needed or needed in every .C file? This would be helpful in removing header inclusions that are redundant and/or unnecessary. Thanks!
0
8367
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8279
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7302
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5619
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.