在给定不同条件的情况下,如何查询访问权限以选择整个记录 [英] how to query access to select entire records given a distinct criteria

查看:53
本文介绍了在给定不同条件的情况下,如何查询访问权限以选择整个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择每个记录的整个第一行,其中促销代码是唯一的.我正在尝试创建一个示例表,该表中将是来自每个不同促销代码的一条记录(第一条记录).我已经问过我所有的同事,他们通常会手工查看数据并从每个数据中选择一个.问题在于,促销代码的数量每次都会增加,并且代码会更改.因此,我想编写一个查询,该查询将选择找到的第一个记录以包含每个不同的代码.所以我有这样的东西:

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;

但这显然可以提供原始表.我的客户中确实有一个名为AutoID的ID字段.

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

谢谢.

推荐答案

我假设您要与每个Customer.Promo关联的第一个Customer.AutoId

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

这篇关于在给定不同条件的情况下,如何查询访问权限以选择整个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆