1

我有数据库dbadmin,表 - tbl_empreimbursefields-emp_id,rem_amount,rem_date

我想检索从给定日期到一周前的数据。我试过这个查询,

SELECT SUM(rem_amount),DATEADD(dd, -7, "2012-01-10")  
FROM tbl_empreimburse 
GROUP BY emp_id 
HAVING emp_id='5' AND rem_date BETWEEN DATEADD(dd, -7, "2012-01-10") AND "2012-01-10"

它给了我错误“ FUNCTION dbadmin.DATEADD 不存在”。我需要将“2012-01-10”转换为日期格式吗?请问有什么帮助吗?

4

2 回答 2

1

试试这个:

此查询给出您为员工 ID 5 和 7 天的日期期间指定的结果。

SELECT emp_id, SUM(rem_amount) 
FROM tbl_empreimburse 
WHERE emp_id='5' AND DATEDIFF('2012-12-31', rem_date) BETWEEN 0 AND 7;

或者

下面的查询为您提供所有员工数据。

SELECT emp_id, SUM(rem_amount) 
FROM tbl_empreimburse 
GROUP BY emp_id 
HAVING DATEDIFF('2012-12-31', rem_date) BETWEEN 0 AND 7;
于 2012-12-15T09:13:41.493 回答
0

检查此*SQLFIDDLE 参考。:)

我不确定您为什么在group by这里使用子句...

样品日期:

ID  AMOUNT  RDATE
1   3400    January, 01 2012 00:00:00+0000
2   5000    January, 10 2012 00:00:00+0000
3   3000    January, 02 2012 00:00:00+0000
5   1000    January, 05 2012 00:00:00+0000
5   2000    January, 04 2012 00:00:00+0000
2   2000    February, 10 2012 00:00:00+0000

询问:

select * from emp
where id = 5;

这是获取总和的查询

select id, sum(amount)
from emp
where rdate between '2012-01-10' - interval 7 day
and '2012-01-10'
and id = 5
;

结果: 员工 ID 的所有记录 = 5

ID  AMOUNT  RDATE
5   1000    January, 05 2012 00:00:00+0000
5   2000    January, 04 2012 00:00:00+0000

按员工 ID 计算的金额总和 = 5

ID  SUM(AMOUNT)
5   3000
于 2012-12-15T08:28:56.843 回答