简体   繁体   English

MS访问SQL帮助

[英]MS access SQL help

I have this table:我有这张桌子:

beneficiary     service   marks   term
1               eng        50       1
1               eng                 2
1               math       30       1
1               math       20       2
1               com                 1
1               com        70       2
2               com                 1
2               com        30       2
2               eng        20       1
2               eng        30       2

How can I extract only the rows for beneficiary / service pairs that have marks in both terms in a service ?如何仅提取服务中两个术语都有标记受益人/服务对的行?

So from this table it should return only:所以从这个表中它应该只返回:

beneficiary     service   mark     term
1               math       30       1
1               math       20       2
2               eng        20       1
2               eng        30       2

This works when I import your data into my local database and run it:当我将您的数据导入本地数据库并运行它时,这会起作用:

SELECT s.*
FROM scores AS s
JOIN (
    SELECT beneficiary,service
    FROM scores
    WHERE marks IS NOT NULL
    GROUP BY beneficiary,service HAVING COUNT(*) = 2
) AS x ON (x.beneficiary = s.beneficiary AND x.service = s.service);

Output: Output:

 beneficiary | service | marks | term
-------------+---------+-------+------
           1 | math    |    30 |    1
           1 | math    |    20 |    2
           2 | eng     |    20 |    1
           2 | eng     |    30 |    2
(4 rows)

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

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