MS Access Query从表中获取不同的记录

时间:2015-11-04 06:51:56

标签: sql ms-access

我有一张如下表:

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

如何获得如下所示的独特记录:

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

请告诉我获取上述独特记录的逻辑,并且Supply_ID不应重复以下条件,例如Supply_LocationDemand_ID的数据。

与上面的示例中一样,Demand_Id D10D2具有相同的下一列记录集。如果其中一列与D10匹配,则D2不应重复该列。对于D2,它可以从相应列中选择任何记录,但已从D10中选择的记录除外。

1 个答案:

答案 0 :(得分:0)

您所描述的并不完全是获取不同的记录(因为原始表具有不同的记录),但获取每个Demand_Id的第一条记录。

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