SQL Query Multiple Columns Using Distinct on One Column Only

后端 未结 6 738
余生分开走
余生分开走 2020-12-05 18:04

I am trying to write a SQL query that selects multiple columns from a table with the distinct operator on one column only.

The table is simple. The columns are:

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 18:52

    I needed to do the same and had to query a query to get the result

    I set my first query up to bring in all IDs from the table and all other information needed to filter:

    SELECT tMAIN.tLOTS.NoContract, tMAIN.ID
    FROM tMAIN INNER JOIN tLOTS ON tMAIN.ID = tLOTS.id
    WHERE (((tLOTS.NoContract)=False));
    

    Save this as Q04_1 -0 this returned 1229 results (there are 63 unique records to query - soime with multiple LOTs)

    SELECT DISTINCT ID
    FROM q04_1;
    

    Saved that as q04_2

    I then wrote another query which brought in the required information linked to the ID

    SELECT q04_2.ID, tMAIN.Customer, tMAIN.Category
    FROM q04_2 INNER JOIN tMAIN ON q04_2.ID = tMAIN.ID;
    

    Worked a treat and got me exactly what I needed - 63 unique records returned with customer and category details.

    This is how I worked around it as I couldn't get the Group By working at all - although I am rather "wet behind the ears" weith SQL (so please be gentle and constructive with feedback)

提交回复
热议问题