我有一个SQL语句,我在其中执行此操作
... group by date having date between '2010-07-01' and '2010-07-10';
结果如下:
sum(test) day
--------------------
20 2010-07-03
120 2010-07-07
33 2010-07-09
42 2010-07-10
所以我有这些结果,但是有可能,我可以写一个语句,让我每天在“介于”条件下返回这种结果行:
sum(test) day
--------------------
0 2010-07-01
0 2010-07-02
20 2010-07-03
0 2010-07-04
0 2010-07-05
0 2010-07-06
120 2010-07-07
... ...
42 2010-07-10
否则,如果无法做到这一点,我必须在程序逻辑中执行此操作。
提前多多感谢&最诚挚的问候。
更新:如果我向您展示完整的SQL语句,也许会更好:
select COALESCE(sum(DUR), 0) AS "r", 0 AS "opt", DATE_FORMAT(date, '%d.%m.%Y') AS "day" from (
select a.id as ID, a.dur as DUR, DATE(FROM_UNIXTIME(REVTSTMP / 1000)) as date,
a_au.re as RE, a_au.stat as STAT from b_c
join c on b_c.c_id = c.id
join a on c.id = a.c_id
join a_au on a.id = a_au.id
join revi on a_au.re = revi.re
join (
select a.id as ID, DATE(FROM_UNIXTIME(REVTSTMP / 1000)) as date,
max(a_au.re) as MAX_RE from b_c
join c on b_c.c_id = c.id
join a on c.id = a.c_id
join a_au on a.id = a_au.id
join revi on a_au.re = revi.re
where b_c.b_id = 30 group by ID, date) x on
x.id = a.id and x.date = date and x.MAX_RE = a_au.rev
where a_au.stat != 7
group by ID, x.date)
AS SubSelTable where date between '2010-07-01' and '2010-07-15' group by date;
更新: 我的新SQL语句( - > Dave Rix):
select coalesce(`theData`.`real`, 0) as 'real', 0 as 'opt', DATE_FORMAT(`DT`.`ddDate`, '%d.%m.%Y') as 'date'
from `dimdates` as DT
left join (
select coalesce(sum(DUR), 0) AS 'real', 0 AS 'opt', date
from (
select a.id as ID, a.dur as DUR, DATE(FROM_UNIXTIME(REVTSTMP / 1000)) as date, a_au.RE as RE, a_au.stat as STAT
from b_c
join c on b_c.c_id = c.id
join a on c.id = a.c_id
join a_au on a.id = a_au.id
join revi on a_au.RE = revi.RE
join (
select a.id as ID, DATE(FROM_UNIXTIME(REVTSTMP / 1000)) as date, max(a_au.RE) as MAX_RE
from b_c
join c on b_c.c_id = c.id
join a on c.id = a.c_id
join a_au on a.id = a_au.id
join revi on a_au.RE = revi.RE
where b_c.b_id = 30 GROUP BY ID, date
) x
on x.id = a.id and x.date = date and x.MAX_RE = a_au.RE
where a_au.stat != 20
group by ID, x.date
) AS SubTable
where date between '2010-07-01' and '2010-07-10' group by date) AS theData
ON `DT`.`ddDate` = `theData`.`date` where `DT`.`ddDate` between '2010-07-01' and '2010-07-15';
答案 0 :(得分:1)
将Between逻辑放在Where子句中
Select Sum(day), day
From Table
Where day Between date1 and date2
Group By day
编辑: 只应用于过滤聚合中的数据......即
Having Sum(day) > 10
答案 1 :(得分:1)
查看我对以下问题的回答;
Select all months within given date span, including the ones with 0 values
这可能正是您所寻找的:)
您可以按照以下方式修改上面的查询(您可以将其集成,但这种方式更简单!);
SELECT COALESCE(`theData`.`opt`, 0), `DT`.`myDate`
FROM `dateTable` AS DT
LEFT JOIN (
... INSERT YOUR QUERY HERE ...
) AS theData
ON `DT`.`myDate` = `theData`.`date`
您还需要将查询中的DATE_FORMAT(date, '%d.%m.%Y') AS "day"
更改为date
E.g。
select COALESCE(sum(DUR), 0) AS "r", 0 AS "opt", `date` from
对于@OMG Ponies的回答,您需要预先填充dateTable
的大量数据行!
有谁知道如何将此表的SQL转储发布为可以附加的文件?它很大,但可能很有用......
答案 2 :(得分:1)
假设您的日期列是DATETIME列,您需要使用某些内容来更改时间值,以便进行正确的分组。 IE:
SELECT SUM(t.test),
DATE_FORMAT(t.date, '%Y-%m-%d') AS day
FROM TABLE t
WHERE t.date BETWEEN @start AND @end
GROUP BY DATE_FORMAT(t.date, '%Y-%m-%d')
但是如果给定日期没有记录,则日期不会出现在结果集中。换句话说,输出中不会出现零日期。
要解决这个问题,你需要LEFT JOIN到一个日期表,MySQL没有生成的能力。它甚至无法生成数字列表,因此您必须创建一个包含单列的表:
DROP TABLE IF EXISTS `example`.`numbers`;
CREATE TABLE `example`.`numbers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
...并填充它:
INSERT INTO numbers (id) VALUES (NULL)
...在您使用数字值生成DATE_ADD function之前的日期列表:
之前 SELECT COALESCE(SUM(t.test), 0),
x.the_date AS day
FROM (SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL n.id-1 DAY), '%Y-%m-%d') AS the_date
FROM NUMBERS n) x
LEFT JOIN your_table yt ON DATE_FORMAT(yt.date, '%Y-%m-%d') = x.the_date
WHERE x.the_date BETWEEN @start AND @end
GROUP BY x.the_date