0

First Database has the following:

OwnerID, FName, LName

Second Database has the following:

OwnerID,PolicyName, Owner2ID,Owner3ID,Owner4ID,Owner5ID, Owner6ID, Owner7ID

So there are multiple owners for a particular policy. My question is how can I design a query in Access that will return all the rows for each policy that have more than one owner. For example assume policy one has OwnerID,Owner2ID,Owner3ID filled with ID based on the first database. How can I return:

OwnerID, Policy Name Owner2ID,POlicy Name Owner3ID,Policy Name

So same policy name but because that one record as multiple owners, it will list all of them in a query.

4

1 回答 1

0

如果您首先对数据进行规范化,则可以运行任意数量的查询:

SELECT a.policyname,
   a.ownerid
FROM   (SELECT policyname,
           ownerid
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner2id
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner3id
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner4id
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner5id
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner6id
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner7id
    FROM   policies) AS a; 

例如,这是一个计数:

SELECT a.policyname,
   Count(a.ownerid) AS CountOfOwnerID
FROM   (SELECT policyname,
           ownerid
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner2id
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner3id
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner4id
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner5id
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner6id
    FROM   policies
    UNION ALL
    SELECT policyname,
           owner7id
    FROM   policies) AS a
WHERE (((a.OwnerID) Is Not Null))
GROUP BY a.PolicyName;

您还可以保存“平面”查询并参考保存的查询来构建其他更复杂的查询。

于 2012-09-25T17:03:43.960 回答