how to use distinct in ms access

后端 未结 3 1368
傲寒
傲寒 2020-12-19 05:22

I have two tables. Task and Categories.

\"Task

相关标签:
3条回答
  • 2020-12-19 05:40

    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]));
    
    0 讨论(0)
  • 2020-12-19 05:49

    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.

    0 讨论(0)
  • 2020-12-19 05:59

    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.

    0 讨论(0)
提交回复
热议问题