我有 fields-emp_id,rem_amount,rem_date 的数据库 dbadmin ,table - tbl_empreimburse 。
我想检索从给定日期到一周的数据。 我试过这个查询,
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"
它给出了错误“功能dbadmin.DATEADD不存在”。我需要将“2012-01-10”转换为日期格式吗?请帮忙吗?
答案 0 :(得分: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;
答案 1 :(得分:0)
检查*SQLFIDDLE reference out。 :)
我不确定你为什么在这里使用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