troubleshooting Question

T-SQL Select First Date, Last Date with Amounts

Avatar of Sonia Bowditch
Sonia BowditchFlag for Guernsey asked on
* T-SQLMicrosoft SQL Server
6 Comments2 Solutions161 ViewsLast Modified:
Hello Experts,

I am trying to write a SQL statement which will give me the First and Last dates with the amounts.

I am using the following SQL to get the total first date for the ref and the dates for first and last entry but I need to get the first entry for that ref and the last entry with the first amount and last amount.  I have tried using FIRST and LAST functions but I am not getting the results I want.

SELECT COUNT(DISTINCT ref) AS REF,
              MIN (FIRST_DATE) AS FIRST_DATE,
              MAX(LAST_DATE) AS LAST_DATE,
             SUM(FIRST_AMOUNT) AS FIRST_AMOUNT,
             SUM(LAST_AMOUNT) AS LAST_AMOUNT,
FROM TABLE
GROUP BY REF
Select-First-and-Last-Amount.xlsx
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros