Skip to content
Advertisement

Why Select Distinct stored ‘As T’ and how to alias?

From this helpful response on another question on stackoverflow.com (How do I count unique items in field in Access query?), the answer contained:

SELECT Count(*) AS N
FROM
(SELECT DISTINCT Name FROM table1) AS T;

Why is the distinct selection aliased “As T” (i.e. not why the letter T, but why do this?)

If you had the case of:

SELECT Count(Long_Table_Name_Here.[Field1]) AS N
FROM
(SELECT DISTINCT Field1 FROM Long_Table_Name_Here) AS T;

How could you also alias the long table name?

Advertisement

Answer

Because MS Access requires that every “object” referred to in the FROM clause has a name. Tables have a default name — which is their name. (Actually, you can use either multi-part name or just the table name.)

As for your second query:

SELECT Count(Long_Table_Name_Here.[Field1]) AS N
FROM (SELECT DISTINCT Field1 FROM Long_Table_Name_Here) AS T;

This will result in a syntax error because Long_Table_Name_Here is not defined. You need to use the table alias defined in the FROM clause:

SELECT Count(T.[Field1]) AS N
FROM (SELECT DISTINCT Field1 FROM Long_Table_Name_Here) AS T;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement