[英]How to Select from a pivot table in ms access
i have two tables :我有两张桌子:
and和
i need to get something like this:我需要得到这样的东西:
HeaderID | Position1 | IsSuccessful | Position2 | IsSuccessful
-------------------------------------------------------------------------
24719 | gold_box | true | propname25 | false
24720 | beyond | false | wtv | false
24721 | red_box | false | wtv22 | true
i managed this so far:到目前为止我做到了:
TRANSFORM Max(Proposals.PropName) AS MaxOfPropName
SELECT Headers.HeaderID
FROM Headers INNER JOIN Proposals ON Headers.ProposalID = Proposals.ProposalID
GROUP BY Headers.HeaderID
PIVOT Headers.Position;
HeaderID | Position1 | Position2 |
------------------------------------------
24719 | gold_box | propname25 |
24720 | beyond | wtv |
24721 | red_box | wtv22 |
now i'm thinking to select from the pivot table but it's not working, and couldn't find any documentation on that on the net, for example:现在我想从数据透视表中进行选择,但它不起作用,并且在网上找不到任何相关文档,例如:
select * from (
TRANSFORM Max(Proposals.PropName) AS MaxOfPropName
SELECT Headers.HeaderID
FROM Headers INNER JOIN Proposals ON Headers.ProposalID = Proposals.ProposalID
GROUP BY Headers.HeaderID
PIVOT Headers.Position;
)
Is there a way to use the results of the pivot table in another select query (ms access)?有没有办法在另一个选择查询(ms access)中使用数据透视表的结果? and how?如何? later on i'll have to use a single query with like 7 tables and joining them with an additional 3 pivot tables... :) I'm sorry guys for this question, but i'm stuck稍后,我将不得不使用一个包含 7 个表的单个查询,并将它们与额外的 3 个数据透视表连接起来......
Thank you谢谢
Consider adding derived table subqueries with two inner joins to the saved crosstab query for each Position.考虑将带有两个内部联接的派生表子查询添加到每个位置的已保存交叉表查询。 If more, positions exist, add more subqueries.如果存在更多职位,则添加更多子查询。
CrossTab Query (save as separate query to be used in main query below)交叉表查询(另存为单独的查询以用于下面的主查询)
TRANSFORM Max(Proposals.PropName) AS MaxOfPropName
SELECT Headers.HeaderID
FROM Headers
INNER JOIN Proposals ON Headers.ProposalID = Proposals.ProposalID
GROUP BY Headers.HeaderID
PIVOT Headers.Position
Main Query主要查询
SELECT crosstabQ.HeaderID, crosstabQ.Position1, pos1.IsSuccessful,
crosstabQ.Position2, pos2.IsSuccessful
FROM (crosstabQ
INNER JOIN
(SELECT IsSuccessful
FROM Headers
WHERE Position = 1) AS pos1
ON pos1.HeaderID = crosstabQ.HeaderID)
INNER JOIN
(SELECT IsSuccessful
FROM Headers
WHERE Position = 2) AS pos2
ON crosstabQ.HeaderID = pos2.HeaderID
Alternatively, run conditional aggregates which is the generalized version for pivot queries (here boolean values will return as integer, 0 or -1):或者,运行条件聚合,这是数据透视查询的通用版本(此处布尔值将返回为整数、0 或 -1):
SELECT Headers.HeaderID,
Max(IIF(Headers.Position=1, Proposals.PropName, NULL)) As Position1,
Max(IIF(Headers.Position=1, Headers.IsSuccessful, NULL)) As IsSuccessful1,
Max(IIF(Headers.Position=2, Proposals.PropName, NULL)) As Position2,
Max(IIF(Headers.Position=2, Headers.IsSuccessful, NULL)) As IsSuccessful2
FROM Headers
INNER JOIN Proposals ON Headers.ProposalID = Proposals.ProposalID
GROUP BY Headers.HeaderID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.