Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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_Sql Server_Full Outer Join - Fatal编程技术网

Sql 在完全外部联接中跨两个独立列按日期排序

Sql 在完全外部联接中跨两个独立列按日期排序,sql,sql-server,full-outer-join,Sql,Sql Server,Full Outer Join,我有两列数据,我正在使用一个完整的外部联接排列,但它包括两个独立的日期列,这使得排序很困难 表1显示了产品的销售排名数据。 表2给出了同一产品的实际销售数据 每个表都可能有其他表没有的日期条目 因此,设想一下,在完全连接之后,我们最终会得到如下简化示例: ProdID L.Date P.Date Rank Units 101 null 2011-10-01 null 740 101 2011-10-02 201

我有两列数据,我正在使用一个完整的外部联接排列,但它包括两个独立的日期列,这使得排序很困难

表1显示了产品的销售排名数据。 表2给出了同一产品的实际销售数据

每个表都可能有其他表没有的日期条目

因此,设想一下,在完全连接之后,我们最终会得到如下简化示例:

ProdID  L.Date         P.Date       Rank   Units
101     null           2011-10-01   null   740
101     2011-10-02     2011-10-02   23     652
101     2011-10-03     null         32     null
以下是我用于提取此数据的查询:

select L.ListID, L.ASIN, L.date, L.ranking, P.ASIN, P.POSdate, P.units from ListItem L
full outer join POSdata P on 
    L.ASIN = P.ASIN and 
    L.date = P.POSdate and 
    (L.ListID = 1 OR L.ASIN is null)
where (L.ASIN = 'xxxxxxxxxx' and L.ListID = 1) or  
      (P.ASIN = 'xxxxxxxxxx' and L.BookID is null) 
order by POSdate, date
它有点复杂,因为产品可能出现在多个列表中,所以我也必须考虑到这一点,但它会返回我需要的数据。我愿意接受关于改进的建议,当然,如果有人有建议的话

问题是,当两个日期列中都可能至少有一些空值时,如何正确排序。当两个列都有一个空值时,我现在的排序方式将不起作用


谢谢。

按ISNULL订购(p.POSdate,L.date)
我想你应该做你需要做的事吗?

马丁,你就像一个网络服务。即时、正确的答案。谢谢我想再提一件我做过的事。我将ISNULL(P.POSdate,L.date)作为date1放入我的选择列表中,并按date1排序。这使我能够在结果集中显示单个日期列。