[英]MS ACCESS 2010 SQL Trickery
I have a dates table and a customer fact table like below. 我有一个日期表和一个客户事实表,如下所示。 I want to try to get the customer code on the dates left over that do not match the customer fact table. 我想尝试在剩下的日期上获取与客户事实表不匹配的客户代码。 I'm not sure what to do in SQL to get this. 我不确定该如何在SQL中执行此操作。
Dates: 日期:
Day of year| TY Date | LY Date
-----------+----------+---------
200 | 2/1/2015 | 2/2/2014
201 | 2/2/2015 | 2/3/2014
202 | 2/3/2015 | 2/4/2014
203 | 2/4/2015 | 2/5/2014
204 | 2/5/2015 | 2/6/2014
205 | 2/6/2015 | 2/7/2014
Cust: 客户:
Cust # | Day of Year | TY Date | LY Date
-------+-------------+----------+--------
300 | 203 | 2/4/2015 | 2/5/2014
900 | 205 | 2/6/2015 | 2/7/2014
RESULT: 结果:
Cust # | Day of Year | TY Date | LY Date
-------+-------------+----------+--------
300 | 200 | 2/1/2015 | 2/2/2014
300 | 201 | 2/2/2015 | 2/3/2014
300 | 202 | 2/3/2015 | 2/4/2014
300 | 204 | 2/5/2015 | 2/6/2014
300 | 205 | 2/6/2015 | 2/7/2014
900 | 200 | 2/1/2015 | 2/2/2014
900 | 201 | 2/2/2015 | 2/3/2014
900 | 202 | 2/3/2015 | 2/4/2014
900 | 203 | 2/4/2015 | 2/5/2014
900 | 204 | 2/5/2015 | 2/6/2014
One possible solution: 一种可能的解决方案:
You can find the missing rows by first generating the complete set of possible pairs of Cust #
and Day of Year
, and then use that set as a derived table and do a left join with the Cust table and filter out the rows that are null, which will be the missing ones. 您可以找到丢失的行,方法是先生成完整的Cust #
和Day of Year
的可能对,然后将其用作派生表,然后对Cust表进行左连接并过滤出空的行,这将是失踪的。
This query: 该查询:
select a.*
from (
select [Cust#], d.[Day of year] from cust, dates as d
) a
left join Cust as c on c.[Cust#] = a.[Cust#] and c.[Day of Year] = a.[Day of year]
where c.[Cust#] is null
order by a.[Cust#], a.[Day of Year]
would give you the following result: 将为您带来以下结果:
Cust# Day of year
300 200
300 201
300 202
300 204
300 205
900 200
900 201
900 202
900 203
900 204
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.