SQL。如何在同一个表中的不同行中工作/比较/查找差异

时间:2010-02-04 02:41:52

标签: mysql sql comparison

我有一张看起来像这样的表:

ID   Date      Size   Marked
1    2010-02-02    2        X
2    2002-02-02    1
1    2010-02-03    2        X
2    2010-02-03    3        
3    2010-02-03    4        X

我有一个代码(PHP)执行以下操作: a)计算每天的大小总和 b)找出当天和最后一天的总数之间的差异。 c)找出今天标记的行的大小总和(昨天没有标记具有相同id的行)。

例如,我将得到以下结果:

Date        Total  DiffWithYesterday  MarkedThisDay
2010-02-02    3      0                  0
2010-02-03    9      6                  4

我觉得有一种方法可以在SQL中编写它。但是我在SQL方面很弱,所以我在玩了一天内部连接,分组和嵌入选择后放弃了。

如果你能给我一些线索,我将不胜感激。

哦..我正在使用MySQL。

此致 维克多

2 个答案:

答案 0 :(得分:2)

这样的东西在SQL Server中有效。我没有MySQL进行测试,但是一旦看到逻辑就可以转换。

create table so (sodate datetime, sosize int, somarked varchar(1))

insert into so (sodate,sosize,somarked) values ('1-jan-2010',3,'X')
insert into so (sodate,sosize,somarked) values ('2-jan-2010',1,'X')
insert into so (sodate,sosize,somarked) values ('3-jan-2010',2,'X')
insert into so (sodate,sosize,somarked) values ('4-jan-2010',0,null)
insert into so (sodate,sosize,somarked) values ('5-jan-2010',2,null)
insert into so (sodate,sosize,somarked) values ('6-jan-2010',1,null)
insert into so (sodate,sosize,somarked) values ('6-jan-2010',4,null)
insert into so (sodate,sosize,somarked) values ('6-jan-2010',1,null)
insert into so (sodate,sosize,somarked) values ('7-jan-2010',3,'X')
insert into so (sodate,sosize,somarked) values ('8-jan-2010',3,'X')
insert into so (sodate,sosize,somarked) values ('9-jan-2010',2,null)
insert into so (sodate,sosize,somarked) values ('10-jan-2010',2,'X')
insert into so (sodate,sosize,somarked) values ('11-jan-2010',1,'X')
insert into so (sodate,sosize,somarked) values ('12-jan-2010',2,null)
insert into so (sodate,sosize,somarked) values ('13-jan-2010',3,'X')

select so.sodate
    ,sum(so.sosize) as Total
    ,isnull(sum(so.sosize),0) - isnull(min(so2.sosize),0) as DiffFromYesterday
    ,sum(case when so.somarked = 'X' then so.sosize end) as MarkedThisDay
from so
    left join (select so.sodate,sum(so.sosize) sosize from so group by sodate) so2 on dateadd(dd,1,so2.sodate) = so.sodate
group by so.sodate  

..安装mysql后,这似乎在那里工作......

select so.sodate
    ,sum(so.sosize) as Total
    ,ifnull(sum(so.sosize),0) - ifnull(min(so2.sosize),0) as DiffFromYesterday
    ,sum(case when so.somarked = 'X' then so.sosize end) as MarkedThisDay
from so
    left join (select so.sodate,sum(so.sosize) sosize from so group by sodate) so2 on (so2.sodate + INTERVAL 1 day )= so.sodate
group by so.sodate  ;

答案 1 :(得分:2)

玩得很开心。

SELECT 
  today.date as Date, 
  today.total as Total, 
  (today.total - yesterday.total) as DiffWithYesterday  , 
  marked.total as MarkedThisDay
FROM
  (SELECT date, sum(size) as total
   FROM table_name
   GROUP BY date) today
LEFT JOIN 
  (SELECT date, sum(size) as total
   FROM table_name
   WHERE marked = 'X'
   GROUP BY date) marked ON today.date = marked.date
LEFT JOIN
  (SELECT (date + INTERVAL 1 day) as date, sum(size) as total
   FROM table_name
   GROUP BY date) yesterday ON today.date=yesterday.date

显然,您需要将“table_name”替换为表格的名称