从包含TRANSFORM ... PIVOT表的子查询表中选择

时间:2013-10-24 15:29:25

标签: sql ms-access ms-access-2007

我有以下功能查询在Access

中创建交叉表/数据透视表
TRANSFORM Sum(y.TNAV) AS TNAV
SELECT y.RecDate
FROM BNYDaily AS y
WHERE (((y.AccName) In ("A","B")) AND y.RecDate >= DateValue("1/1/2013"))
GROUP BY y.RecDate
PIVOT y.AccName; ) 

问题是查询返回带有NULL字段的结果会影响我的计算。我想省略此交叉表中在任一列中具有NULL值的行:

RecDate     A                   B
....
1/25/2013   1,469,004,032.00    968.63
1/26/2013   1,466,082,304.00    
1/28/2013                       973.91
1/29/2013   1,471,277,440.00    971.66
...

我尝试了以下查询,将上述查询用作子查询而没有任何运气:

SELECT * FROM
(
TRANSFORM Sum(y.TNAV) AS TNAV
SELECT y.RecDate
FROM BNYDaily AS y
WHERE (((y.AccName) In ("A","B")) AND y.RecDate >= DateValue("1/1/2013"))
GROUP BY y.RecDate
PIVOT y.AccName; 
) AS t
WHERE t.A IS NOT NULL AND t.B is NOT NULL

奇怪地不在Access中运行并返回错误。如果我从交叉表查询中查询为已保存的查询表,则可以正常工作。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

而不是从交叉表的结果中“挤出”包含Null的行,如何从交叉表的中消除产生Null的行?我刚刚尝试了以下内容,似乎有效:

TRANSFORM Sum(y.TNAV) AS TNAV
SELECT y.RecDate
FROM 
    (
        SELECT RecDate, AccName, TNAV
        FROM BNYDaily 
        WHERE RecDate IN (SELECT RecDate FROM BNYDaily WHERE AccName = "A")
            AND RecDate IN (SELECT RecDate FROM BNYDaily WHERE AccName = "B")
    ) AS y
WHERE (((y.AccName) In ("A","B")) AND y.RecDate >= DateValue("1/1/2013"))
GROUP BY y.RecDate
PIVOT y.AccName;