• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

SQL query to return a new generated column based on another column

I have an table in an Access database with three columns:

File
Date
Amount

If I would like to retrieve the sum of the amounts for each file, from 1st of October to 1st of November 2006, I would write an SQL query like this:

SELECT File, SUM(Amount) AS Total FROM Table1 WHERE ([Date] >= #01/10/2006#) AND ([Date] < #01/11/2007) GROUP BY File

Now, what if I wanted to return three columns instead of two. The first two columns would be lthe same as above (File and Total), and the third column would be another total - the sum of all Amounts from 1st of October 2005 to 1st November 2005.
0
SETP
Asked:
SETP
1 Solution
 
Daniel WilsonCommented:
I think the SWITCH statement will get you there. I'm also using the Between ... And operator as it looks a little cleaner.

SELECT File,
SUM( Switch([Date] Between #01/10/2006# AND #01/11/2007#,  Amount, [Date] NOT Between #01/10/2006# AND #01/11/2006#, 0)) AS TotalOct200,
SUM( Switch([Date] Between #01/10/2005# AND #01/11/2005#,  Amount, [Date] NOT Between #01/10/2006# AND #01/11/2007#, 0)) AS TotalOct2005
 FROM Table1
 GROUP BY File
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now