#错误应出现在多个LEFT JOIN语句中访问查询,当值应为NULL时
技术问答
185 人阅读
|
0 人回复
|
2023-09-12
|
我试着回去ID最近四年的数据(如果存在)。表(称为A_TABLE)如下所示:ID,Year,Val
6 O" x4 R2 t* }2 S, a9 N( P8 ]查询背后的想法是:表中的每一个ID /年份,使用LEFT6 {$ n7 Y p2 O6 a
JOIN以及Year-1,Year-2和Year-3(获取4年数据),然后返回每年Val。这是SQL:7 [9 U! h3 v9 L+ p6 e/ ^7 H0 G$ p
SELECT a.ID,a.year AS [Year],a.Val AS VAL, a1.year AS [Year-1],a1.Val AS [VAL-1], a2.year AS [Year-2],a2.Val AS [VAL-2], a3.year AS [Year-3],a3.Val AS [VAL-3]FROM ( ([A_TABLE] AS a LEFT JOIN [A_TABLE] AS a1 ON (a.ID = a1.ID) AND (a.year = a1.year 1) LEFT JOIN [A_TABLE] AS a2 ON (a.ID = a2.ID) AND (a.year = a2.year 2) LEFT JOIN [A_TABLE] AS a3 ON (a.ID = a3.ID) AND (a.year = a3.year 3)问题是,对于过去几年没有数据的年份(例如Year-1),我是对的VAL列(例如[VAL-1)看到1])
- F) \ e0 g T! `8 N8 T+ ?1 \$ p1 MError奇怪的是,我在年份列中看到了预期的空(比如[YEAR-1])。样本数据:
* D9 }& F) y7 j6 K% wID YEAR VALDave 2004 1Dave 2006 2Dave 2007 3Dave 2008 5Dave 2009 0这样的输出:/ a: s8 m9 R' e \) S. T
ID YEAR VAL YEAR-1 VAL-1 YEAR-2 VAL-2 YEAR-3 VAL-3Dave #Error #Error #ErrorDave #Error #ErrorDave 2007 3 2006 2006 #Error 2004 1Dave 2008 5 2007 33 #ErrorDave 2009 2008 55 2006 2006 2006 20008 有意义吗?为什么我不存在?YEAR获得适当的NULL值,而不存在VAL获得#错误?' L! Q- z4 t3 I
(这是Access2000。像“ IIf(a1.val为null,-999,a1.val)这样的条件句似乎没有效果。
5 I; R% M; f& u* d- ]; V- `" ]编辑:事实证明,错误是由A_TABLE事实上,这是由查询事实引起的。当我将所有数据放入实际表并运行相同的查询时,一切都将按原始显示。谢谢你的帮助。9 d( M' t3 y2 |7 _$ r2 o2 U
( C6 f& D; D% d/ N5 n8 a' u 解决方案: |
|
|
|
|
|