cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can DISTINCT be added as a keyword for queries?

Can DISTINCT be added as a keyword for queries?

I recently got into a situation where I wanted to know how many distinct values were in a column of a table. It would be nice to be able to use the following:

SELECT COUNT (DISTINCT column-name)  FROM table-name
6 Comments
mischa_spelt
Advisor

If you just want to know how many distinct values there are in a column, you could do something like

query("SELECT column-name FROM table-name GROUP BY column-name");
int distinctValues = getquerymatchcount();

Of course that only works in some pretty specific cases but it may be enough to help you go forward for now.

MBJEBZSRG
Advocate

The DISTINCT keyword does have its uses, but many problems where you need it can be solved by using the Group by statement: This will give you a list where each new distinction of column name has its own row. Now you can iterate through the distinctions.

SELECT COUNT(*), [column-name] FROM table GROUP BY [column-name]

cameron_pluim
Not applicable

Thanks @Mischa Spelt, I didn't understand exactly how GROUP BY works, but that should help a lot

ABajpaiWMKNX
Enthusiast

How can I use DISTINCT to list out distinct values from the column into another table?

Already tried the following code and got the error "time: 0.000000 exception: FlexScript exception: Could not parse query SELECT DISTINCT ModelName FROM ProductionOrder at MODEL:/Tools/UserCommands/WriteStationMaps/code"

string sql4="SELECT DISTINCT ModelName \
FROM ProductionOrder";

Table result4=Table.query(sql4);
result4.cloneTo(Table("UniqueModelNames"));
DISTINCT isn's a keyword you can use - as it mentions on this page and in the documentation. Use:


SELCT ModelName From ProductionOrder GROUP BY ModelName

In future please post a new question.

ABajpaiWMKNX
Enthusiast

Thanks Jason. Appreciate it as always. Will keep that in mind.

Can't find what you're looking for? Ask the community or share your knowledge.

Submit Idea