简体   繁体   English

MS Access查询以从表中获得不同的记录

[英]MS Access Query to get Distinct records from a table

I have a table like below: 我有一个如下表:

ID  Demand_ID   Supply_ID   Supply_Skill    Supply_Designation  Supply_Location
1   D10         S10         Python          engineer            Mumbai
2   D10         S16         Python          engineer            Mumbai
3   D10         S2          Python          engineer            Mumbai
4   D10         S7          Python          engineer            Mumbai
5   D12         S12         Java            Manager             Bangalore
6   D1          S1          Java            Senior engineer     Bangalore
7   D1          S11         Java            Senior engineer     Bangalore
8   D1          S3          Java            Senior engineer     Bangalore
9   D2          S10         Python          engineer            Mumbai
10  D2          S16         Python          engineer            Mumbai
11  D2          S2          Python          engineer            Mumbai
12  D2          S7          Python          engineer            Mumbai
13  D3          S1          Java            Senior engineer     Bangalore
14  D3          S11         Java            Senior engineer     Bangalore
15  D3          S3          Java            Senior engineer     Bangalore

How do get a unique records like below: 如何获得唯一的记录,如下所示:

ID  Demand_ID   Supply_ID   Supply_Skill    Supply_Designation  Supply_Location
1   D10         S10         Python          engineer            Mumbai
5   D12         S12         Java            Manager             Bangalore
6   D1          S1          Java            Senior engineer     Bangalore
10  D2          S16         Python          engineer            Mumbai
14  D3          S11         Java            Senior engineer     Bangalore

Please tell me a logic to get unique records like above, and with the below condition like data from Supply_ID to Supply_Location should not be repeated for a Demand_ID . 请告诉我一个逻辑来获得独特的记录,像上面,并用以下条件就像从数据Supply_IDSupply_Location不应该被重复的Demand_ID

Like in the above example Demand_Id D10 and D2 are having same set of next column records. 像上面的示例一样, Demand_Id D10D2具有相同的下一列记录集。 If one of the column is matching for D10 then it should not be repeated for D2 . 如果该列之一与D10匹配,则不应为D2重复。 For D2 it can pick any records from the corresponding columns except the record already picked from D10 . 对于D2它可以从相应列中选取任何记录,但已从D10选取的记录除外。

What you describe is not exactly getting distinct records (because the original table has distinct records) but getting the first record for each Demand_Id . 您描述的并不是完全获得不同的记录(因为原始表具有不同的记录),而是获得每个Demand_Id的第一条记录。

SELECT *
  FROM Supplies_And_Demands
 WHERE ID IN (SELECT MIN(ID)
                FROM Supplies_And_Demands
               GROUP BY Demand_Id)

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

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