We help IT Professionals succeed at work.
Get Started

T-SQL Select First Date, Last Date with Amounts

160 Views
Last Modified: 2017-06-16
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
Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 2 Answers and 6 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE