?
Solved

Max Case problem

Posted on 2005-04-19
4
Medium Priority
?
215 Views
Last Modified: 2010-08-05
Hey all,

I have a question regarding the use of Max Case.....I am trying to return some data from a database using the max case syntax, but I am struggling a little.  Here is my example below.

SELECT
Max (Case WHEN
      ((Account = '1') or(Account = '2'))
      Then Sum[Expense Post Amount] Else 0 END)
      as [Total Expenses]

From dbo.mydatabase
Where ([Post Date] Between '04/01/2005' and '04/30/2005')

Sample Dataset

Account            Date             Expense Post Amount
1                     04/02/2005    1.00
1                     04/15/2005    2.00
1                     04/16/2005    3.00
2                     04/02/2005    1.00

I am looking to return 7.00 as [Total Expense]

I am screwing up the sum part of the code.  It seems to work ok until I start trying to sum up the data that is either account 1 and or account 2.

Any suggestions or ideas would be really appreciated!!!!!!!  

Thanks,
Bob
0
Comment
Question by:vmrfadmin
  • 2
4 Comments
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 13819558
SELECT SUM([Expense Post Amount])
From dbo.mydatabase
Where ([Post Date] Between '04/01/2005' and '04/30/2005')
AND Account IN ('1', '2')
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 13819570
But if you have the situation where you want a total of *all* accounts and a separate total for '1' and '2' accounts, then you can do this:


SELECT SUM([Expense Post Amount]) AS [Total Expenses],
    SUM(CASE WHEN Account IN ('1', '2') THEN [Expense Post Amount] ELSE 0 END) AS [Acct 1&2 Expenses]
From dbo.mydatabase
Where ([Post Date] Between '04/01/2005' and '04/30/2005')

0
 
LVL 28

Expert Comment

by:rafrancisco
ID: 13819581
Try this:

SELECT SUM(CASE WHEN Account IN ('1', '2') THEN [Expense Post Amount] ELSE 0 END) AS  [Total Expenses]
From dbo.mydatabase
Where ([Post Date] Between '04/01/2005' and '04/30/2005')
0
 

Author Comment

by:vmrfadmin
ID: 13819678
Wow....you guys are good!  I wish I was that quick on my feet.  It works like a charm!  

Thanks Scott and rafranciso............

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question