如何在ms access中使用distinct

发布于 2024-11-03 12:20:07 字数 709 浏览 3 评论 0原文

我有两张桌子。任务和类别。

任务表

类别表

TaskID 不是主键,因为存在重复值。当为特定任务选择多个联系人时,taskid 和其他详细信息将重复。我编写了查询:

SELECT Priority, Subject, Status, DueDate, Completed, Category
FROM Task, Categories
WHERE Categories.CategoryID=Task.CategoryID;

查询结果

现在,由于该任务选择了多个联系人,因此对于taskid=T4,有两条记录(以灰色突出显示)。我曾尝试在 ms access 2003 中使用 unique,但它不起作用。我想显示不同的记录。 (这里不需要显示taskid)如果我写:

select priority, distinct(subject), .......

并保持与上面查询中提到的相同,那么它会给我一个错误。我也尝试过distinctrow,但没有成功。如何在ms access中获取不同的值?

I have two tables. Task and Categories.

Task Table

Categories Table

TaskID is not a primary key as there are duplicate values.When there are multiple contacts are selected for a specific task,taskid and other details will be duplicated.I wrote the query:

SELECT Priority, Subject, Status, DueDate, Completed, Category
FROM Task, Categories
WHERE Categories.CategoryID=Task.CategoryID;

Query Result

Now as multiple contacts are selected for that task,for the taskid=T4, there are two records(highlighted with gray). I have tried using distinct in ms access 2003 but its not working. I want to display distinct records. (Here there's no requirement to show taskid) If I write :

select priority, distinct(subject), .......

and remaining same as mentioned in above query then its giving me an error. I have tried distinctrow also.But didnt get success. How to get distinct values in ms access?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那请放手 2024-11-10 12:20:07

好的。它是这样工作的。

SELECT DISTINCT Task.Priority, Task.Subject, Task.Status, Task.DueDate, 
Task.Completed, Categories.Category
FROM Task, Categories
WHERE (((Categories.CategoryID)=[Task].[CategoryID]));

Okay.Its working this way.

SELECT DISTINCT Task.Priority, Task.Subject, Task.Status, Task.DueDate, 
Task.Completed, Categories.Category
FROM Task, Categories
WHERE (((Categories.CategoryID)=[Task].[CategoryID]));
ペ泪落弦音 2024-11-10 12:20:07

我不喜欢使用 SELECT DISTINCT,我发现它使我的代码需要更长的时间来编译。我这样做的另一种方法是使用 GROUP BY。

    SELECT Priority, Subject, Status, DueDate, Completed, Category
    FROM Task, Categories
    WHERE Categories.CategoryID=Task.CategoryID
    GROUP BY Subject;

我目前没有 VBA,但这应该也可以。

I don't like using SELECT DISTINCT, I have found that it makes my code take longer to compile. The other way I do it is by using GROUP BY.

    SELECT Priority, Subject, Status, DueDate, Completed, Category
    FROM Task, Categories
    WHERE Categories.CategoryID=Task.CategoryID
    GROUP BY Subject;

I do not have VBA up at the moment but this should work as well.

Oo萌小芽oO 2024-11-10 12:20:07

使用 SELECT DISTINCT 将适合您,但更好的解决方案是更改您的数据库设计。

重复记录可能会导致数据不一致。例如,假设具有相同 TaskID 的不同记录中有两种不同的状态。哪一个是正确的?

更好的设计将包括诸如任务表、联系人表和分配表之类的内容,如下所示(括号中的字段是 PK):

任务:[TaskID]、TaskPriority、Subject、Status、DueDate、Completed、StartDate、Owner , CategoryID, ContactID, ...

联系人:[ID], Name, Surname, Address, PhoneNumber, ...

分配:[TaskID, ContactID]

然后,您可以通过简单的 SELECT 从任务中检索任务表。
每当您需要知道分配给任务的联系人时,您都可以使用 JOIN 子句来实现,就像这样

SELECT T.*, C.*
FROM TaskID as T 
  INNER JOIN Assignment as A
    ON T.TaskID = A.TaskID
  INNER JOIN Contac as C
    ON A.ContactID = C.ID

或类似的。您可以使用 SQL 的所有查询功能对结果进行过滤、排序或分组。

Using SELECT DISTINCT will work for you, but a better solution here would be to change your database design.

Duplicate records may lead to inconsistent data. For example, imagine having two different status in different records with the same TaskID. Which one would be right?

A better design would include something like a Task table, a Contact table and an Assignment table, as follows (the fields in brackets are the PK):

Tasks: [TaskID], TaskPriority, Subject, Status, DueDate, Completed, StartDate, Owner, CategoryID, ContactID, ...

Contact: [ID], Name, Surname, Address, PhoneNumber, ...

Assignment: [TaskID, ContactID]

Then, you can retrieve the Tasks with a simple SELECT from the Tasks tables.
And whenever you need to know the contacts assigned to a Tasks, you would do so using the JOIN clause, like this

SELECT T.*, C.*
FROM TaskID as T 
  INNER JOIN Assignment as A
    ON T.TaskID = A.TaskID
  INNER JOIN Contac as C
    ON A.ContactID = C.ID

Or similar. You can filter, sort or group the results using all of SQL's query power.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文