Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 数据库使用不同的列多次选择同一记录_Sql_Database_Database Design_Ms Access - Fatal编程技术网

Sql 数据库使用不同的列多次选择同一记录

Sql 数据库使用不同的列多次选择同一记录,sql,database,database-design,ms-access,Sql,Database,Database Design,Ms Access,我有一个带有多个日期列(部门完成日期)的订单表。我想查询该表,并为指定日期的每个匹配返回一个唯一的行,并给出一个为什么选择该行的引用 因此,从这个表中: OrderID OrderName Date1 Date2 Date3 456 feh 5/1/2011 6/1/2011 3/1/2011 487 meh 12/1/2010 2/1/2011 8/1/2011 如果查询任何大于2011年4月1

我有一个带有多个日期列(部门完成日期)的订单表。我想查询该表,并为指定日期的每个匹配返回一个唯一的行,并给出一个为什么选择该行的引用

因此,从这个表中:

OrderID   OrderName   Date1      Date2      Date3
456       feh         5/1/2011   6/1/2011   3/1/2011
487       meh         12/1/2010  2/1/2011   8/1/2011
如果查询任何大于2011年4月1日的日期,我想返回:

456       feh         5/1/2011     Date1
456       feh         6/1/2011     Date2
487       meh         8/1/2011     Date3
数据在MS Access中,我不确定这在查询级别是否可行,或者是否需要生成子报表


谢谢你的帮助

这可以通过查询实现:

Select OrderID, OrderName, Date1 as Date 
from table where date1>'4/21/2011'

UNION ALL

Select OrderID, OrderName, Date2 as Date 
from table where Date2>'4/21/2011'

UNION ALL

Select OrderID, OrderName, Date3 as Date 
from table where Date3>'4/21/2011'

请尝试以下操作以获取您的最后一列:

Select OrderID, OrderName, Date1 as SelDate, 'Date1' as Reason
  from table where date1>'4/21/2011'
UNION ALL  
Select OrderID, OrderName, Date2 as SelDate, 'Date2' as Reason
  from table where Date2>'4/21/2011'
UNION ALL  
Select OrderID, OrderName, Date3 as SelDate, 'Date3' as Reason
  from table where Date3>'4/21/2011' 

还要注意,我更改了
SelDate
列的别名——即使它是别名时(在Access中)并不重要,但您确实应该避免使用保留字命名列

这和上面的一个都有效,唯一的调整是我希望最后一列不包含日期,而是包含匹配的标题或原因。你需要为此添加另一列。在错误区域中发布评论。在这里发布:两个union语句都有效,但是,我希望最后一列不是字段中的日期,而是列名或匹配原因。多谢!!太棒了,谢谢!很明显,这是整个记录集的3次点击,对吗?嗯。。。。很可能是的,尽管它都是由查询引擎(即“服务器”处理的,尽管它实际上是本地访问的),然后在返回之前由联合进行组合。如果你真的想知道细节,谷歌“喷气机展示计划”。
Select OrderID, OrderName, Date1 as SelDate, 'Date1' as Reason
  from table where date1>'4/21/2011'
UNION ALL  
Select OrderID, OrderName, Date2 as SelDate, 'Date2' as Reason
  from table where Date2>'4/21/2011'
UNION ALL  
Select OrderID, OrderName, Date3 as SelDate, 'Date3' as Reason
  from table where Date3>'4/21/2011'