We help IT Professionals succeed at work.

Seperate SUM() values for SQL

nvenable asked
Last Modified: 2010-04-17
Here is a sample of my database structure:

CustID   Amount    TimeCompleted
-------   ----------    -----------------
1234    10             2006-02-15 10:00:00
1234    15             2006-02-15 10:23:00
1234    3               2006-02-15 09:12:00
2345    50             2006-02-15 10:00:00
2345    20             2006-02-15 12:00:00
3456    10             2006-02-15 07:00:00
3456    5               2006-02-15 13:00:00

I have the following SQL query:

      SUM(Amount) AS TotalAmount
      TimeCompleted BETWEEN '2006-02-15' AND '2006-02-15 12:00:00'

Which returns something like this:

CustID   Amount
-------   ----------
1234     28
2345     70
3456     10

Is there any way to write a query that will allow me to seperate multiple totals/sums for different time periods?  I'd like to grab the sum of Amount between 00:00:00 to 10:00:00 AND 10:00:00 to 12:00:00 seperately...  for example:

CustID   Amount
-------   ----------
1234     3
1234     25
2345     70
3456     10
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)


Perfect!  That seems to be just what I'm looking for.  

One question for you: what's the "A" just before the GROUP BY clause doing?  I'm not familliar with that, and my SQL processing program displays it as incorrect syntax (even though it's apparently needed to work properly).  Thanks!

A as alias for the sub-query
    CASE  WHEN TimeCompleted BETWEEN '2006-02-15' AND '2006-02-15 9:59:59' THEN 1 ELSE
          CASE WHEN TimeCompleted BETWEEN '2006-02-15 10:00:00' AND '2006-02-15 12:00:00' THEN 2 END END AS DateGroup                            
WHERE TimeCompleted BETWEEN '2006-02-15' AND '2006-02-15 12:00:00'

In this sub-quey, we will "mark" for each row as which period of date it belongs to.
The A subquery will return records like

CustID   Amount   DateGroup
-------   ----------
1234    10            2
1234    15            2
1234    3              1
2345    50            2
2345    20            2
3456    10            1

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
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
Empower Your Career
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

Ask ANY Question

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

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.