我想编写查询以按当前日期和每月计算金额。
我的费用表中有 TransactionID,Date,Amount 列。
我想要这种格式:
currentDayAmount TotalMonthlyAmount
$12305 $236589 --suppose on 31 Aug 2011
$62598 $365489 --suppose on 30 Sep 2011
此处CurrentDayAmount是我输入以执行查询的日期,TotalMonthlyAmount是我输入日期的月份。这意味着我想要执行三个日期。首先是我要运行查询的那一天,最后两个是日期范围,需要每月总金额。 我执行以下查询但显示正确的数据但显示在行中。
select sum(c.amount)
from charge c
where c.eDate between '09/16/2011' and '09/17/2011'
union all
select sum(c1.amount)
from charge c1
where c1.eDate between '09/01/2011' and '09/20/2011'
结果
$33256.25
$705826.36
我希望这些数据在列中
current Monthly
$33256.25 $705826.36
我希望这些信息足以让我了解我想做的事情 请提出任何建议或疑问以解决我的问题。
提前致谢
答案 0 :(得分:1)
略有不同的方法:
SELECT SUM(CASE
WHEN c.edate BETWEEN '09/16/2011' AND '09/17/2011' THEN c.amount
ELSE 0
END),
SUM(CASE
WHEN c.edate BETWEEN '09/01/2011' AND '09/20/2011' THEN c.amount
ELSE 0
END),
FROM charge c
答案 1 :(得分:0)
试试这个:
SELECT
currentDayAmount = (
SELECT
SUM(c.amount)
FROM
charge c
WHERE
c.eDate BETWEEN '09/16/2011' AND '09/17/2011'
)
, TotalMonthlyAmount = (
SELECT
SUM(c1.amount)
FROM
charge c1
WHERE
c1.eDate BETWEEN '09/01/2011' AND '09/20/2011'
)