how to query access to select entire records given a distinct criteria

半世苍凉 提交于 2020-01-16 04:38:06

问题


I want to select the entire first row of each record where a promo code is unique. I am trying to create a samples table, in this table will be one record (the first record) from each distinct promo code. I have asked all of my co-workers and they usually go though the data by hand and select one from each. the problem is that the number of promo codes grows each time and the codes change. so I want to write a query that will select the first record found to have each distinct code. so for I have something like this:

SELECT DISTINCT Customer.promo1 FROM Customer AS promo;

SELECT * FROM Customer, promo
WHERE Customer.promo1 = promo.promo1;

But this obviously give the original table. I do have a ID field called AutoID in Customer.

Thanks in advance.


回答1:


I'm assuming you want the first Customer.AutoId associated with each Customer.Promo

SELECT
    c.*
FROM
    Customer  c 
    INNER JOIN 
    (

    SELECT 
        c.promo1,
        MIN(c.AutoID) AutoID
    FROM 
        Customer  c
    GROUP BY
        c.promo1) FirstCusomterWithPromo
    ON c.AutoID = FirstCusomterWithPromo.AutoID



回答2:


Something like that:

SELECT * FROM Customer
GROUP BY Customer.promo1 


来源:https://stackoverflow.com/questions/5953592/how-to-query-access-to-select-entire-records-given-a-distinct-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!