简体   繁体   English

MS-Access-查询以根据条件返回记录的每个子集

[英]MS-Access - Query to return each subset of records based on criteria

How would I go about writing a query that results in the following... 我将如何编写导致以下结果的查询...

Let's say my dataset looks this: 假设我的数据集看起来像这样:

| PersonId | LastName | FirstName | ProductType | ProductValue |
|----------|----------|-----------|-------------|--------------|
| P0000001 |  Bloggs  |    Joe    |      1      |      £15     |
| P0000001 |  Bloggs  |    Joe    |      1      |      £20     |
| P0000001 |  Bloggs  |    Joe    |      2      |      £10     |
| P0000001 |  Bloggs  |    Joe    |      3      |      £5      |
| P0000002 |    Doe   |   Jane    |      2      |      £25     |
| P0000002 |    Doe   |   Jane    |      3      |      £60     |
| P0000003 |   Brown  |    Jim    |      1      |      £20     |
| P0000003 |   Brown  |    Jim    |      3      |      £5      |

Where a PersonId has at least 1 occurance of ProductType = 1 in any of that PersonId 's set of records, return all records for that PersonId (even if the other records returned from the set have a ProductType that is not 1). 如果某个PersonId在该PersonId的任何记录集中至少出现1个ProductType = 1 ,则返回该PersonId所有记录(即使从该集中返回的其他记录的ProductType不为1)。

So the above dataset would be filtered like so: 因此,上面的数据集将像这样过滤:

| PersonId | LastName | FirstName | ProductType | ProductValue |
|----------|----------|-----------|-------------|--------------|
| P0000001 |  Bloggs  |    Joe    |      1      |      £15     |
| P0000001 |  Bloggs  |    Joe    |      1      |      £20     |
| P0000001 |  Bloggs  |    Joe    |      2      |      £10     |
| P0000001 |  Bloggs  |    Joe    |      3      |      £5      |
| P0000003 |   Brown  |    Jim    |      1      |      £20     |
| P0000003 |   Brown  |    Jim    |      3      |      £5      |

Jane Doe didn't have any records with ProductType = 1 so all her records were filtered-out. Jane Doe没有任何产品ProductType = 1记录,因此她的所有记录都被过滤掉了。 Both Joe Bloggs and Jim Brown have at least 1 record with ProductType = 1 so all their records are returned in the query. Joe Bloggs和Jim Brown都至少拥有1条ProductType = 1记录,因此它们的所有记录都在查询中返回。

You could use GROUP BY and HAVING clause: 您可以使用GROUP BY和HAVING子句:

SELECT PersonID, LastName, Firstname
FROM tbl
WHERE ProductType=1
GROUP BY PersonID, LastName, FirstName
HAVING COUNT(*) >=1

And then you use IN clause: 然后使用IN子句:

SELECT * FROM tbl
WHERE PersonID IN (
  SELECT PersonID
  FROM tbl
  WHERE ProductType=1
  GROUP BY PersonID
  HAVING COUNT(*) >=1
)

You could do it with an EXISTS . 您可以使用EXISTS Like this: 像这样:

SELECT 
    * 
FROM 
    yourTable as tbl
WHERE EXISTS
    (
        SELECT
            NULL
        FROM
            yourTable AS tbl1
        WHERE
            yourTable.PersonId=tbl.PersonId
            AND tbl1.ProductType=1
    )

This will get you the output: 这将为您提供输出:

P0000001    Bloggs  Joe 1   £15
P0000001    Bloggs  Joe 1   £20
P0000001    Bloggs  Joe 2   £10
P0000001    Bloggs  Joe 3   £5
P0000003    Brown   Jim 1   £20
P0000003    Brown   Jim 3   £5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM