SQL02: Sql sélect Declaració diferent

La instrucció SELECT DISTINCT s'utilitza per retornar només valors diferents (diferents).
Dins d'una taula, una columna sovint conté molts valors duplicats;


Exemple SELECT sense DISTINCT

La següent instrucció SQL selecciona tots els valors (inclosos els duplicats) de la columna "Country" de la taula "Customers":

SQL: SELECT Country FROM Customers;
GenQL:
List<String> allcountriesrepeat = this.getCustomerManager().executeQuery(CustomersFields.COUNTRY); print(allcountriesrepeat, "Countries Without DISTINCT");





SELECT DISTINCT Exemples - Amb DISTINCT

La següent instrucció SQL només selecciona els valors DISTINCT de la columna "Country" de la taula "Customers":

SQL: SELECT DISTINCT Country FROM Customers;
GenQL:
SelectDistinct<String> sd = new SelectDistinct<String>(CustomersFields.COUNTRY); List<String> distinctcountries = this.getCustomerManager().executeQuery(sd); print(distinctcountries, "Countries With DISTINCT");





SELECT DISTINCT Exemples - Comptar països

La següent instrucció SQL llista el nombre de països clients:

SQL: SELECT COUNT( Country) FROM Customers;
GenQL:
SelectCount sc = new SelectCount(CustomersFields.COUNTRY); Long countdisctintcountries = this.getCustomerManager().executeQueryOne(sc, null); print(countdisctintcountries, "Count Countries ");





SELECT DISTINCT Exemples - Comptar països diferents

La següent instrucció SQL llista el nombre de països clients (diferents) diferents:

SQL: SELECT COUNT(DISTINCT Country) FROM Customers;
GenQL:
SelectDistinct<String> sd = new SelectDistinct<String>(CustomersFields.COUNTRY); SelectCount sc = new SelectCount(sd); Long countdisctintcountries = this.getCustomerManager().executeQueryOne(sc, null); print(countdisctintcountries, "Count DISTINCT Countries ");