(SQL查询)将同一列中没有数据的设置值与另一行进行比较

时间:2014-07-04 02:49:49

标签: sql ms-access

请帮助,

我尝试使用PHP页面的odbc连接查询Ms.Access。

我有这个表格( table1

class   quantity    date 
1   30  01/04/2014 
2   23  01/04/2014 
3   23  01/04/2014 
4   14  01/04/2014 
5   5   01/04/2014 
1   41  01/05/2014 
2   38  01/05/2014 
3   36  01/05/2014 
4   28  01/05/2014 
5   25  01/05/2014 
6   1   01/05/2014 

请为查询提供帮助以获得此输出:

class   quantity    date 
1   30  01/04/2014 
2   23  01/04/2014 
3   23  01/04/2014 
4   14  01/04/2014 
5   5   01/04/2014 
6   0   0 
1   41  01/05/2014 
2   38  01/05/2014 
3   36  01/05/2014 
4   28  01/05/2014 
5   25  01/05/2014 
6   1   01/05/2014 

在输出中,0将显示0作为第6类的数量,实际上在2014年4月1日没有第6类的记录。

1 个答案:

答案 0 :(得分:0)

您可以使用union all执行此操作。但更普遍的解决方案是:

select c.class, d.date, nz(t1.quantity, 0)
from ((select distinct class from table1) as c cross join
      (select distinct date from table1) as d
     ) left join
     table1 as t1
     on t1.class = c.class and t1.date = d.date