This tutorial shows in a simple Cω program how to use the DISTINCT keyword in a SQL Select expression.
To run this tutorial, you may use the following project and source files:
These files are located in the \samples\SQL\Distinct subdirectory under the path where you installed Cω, which by default is C:\Program Files\Microsoft Research\Comega.
To run this sample, you need to have a SQL configuration file in the same directory as the Select.exe you make in Visual Studio. Here are the contents of the version of this file that Cω provided you as a starting point:
<configuration>
<appSettings>
<add key="Northwind" value="Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI" />
</appSettings>
</configuration>
Verify that this is the correct SQL connection string for connecting to the Northwind database on your SQL Server 2000 installation. If necessary, you can modify the value attribute in the <add> element
When you have verified it is correct, press F5 in Visual Studio to make and run the sample.
The following is a complete Cω program that demonstrates how to use the DISTINCT keyword to filter out duplicates in the results yielded from a SQL Select expression that queries for city/country paired values.
using System;
using System.Data.SqlTypes;
using System.Query;
using Northwind;
public class Test {
static void Main() {
// use distinct to remove duplicates of the [City, Country] pairs
results = select distinct City, Country from DB.Customers order by City, Country;
Console.WriteLine("{0,-15} {1,-10}", "City", "Country");
Console.WriteLine("--------------- ----------");
foreach(row in results) {
Console.WriteLine("{0,-15} {1,-10}", row.City, row.Country);
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
}
City Country --------------- ---------- Aachen Germany Albuquerque USA Anchorage USA Århus Denmark Barcelona Spain Barquisimeto Venezuela Bergamo Italy Berlin Germany Bern Switzerland Boise USA Bräcke Sweden Brandenburg Germany Bruxelles Belgium Buenos Aires Argentina Butte USA Campinas Brazil Caracas Venezuela Charleroi Belgium Cork Ireland Cowes UK Cunewalde Germany Elgin USA Eugene USA Frankfurt a.M. Germany Genève Switzerland Graz Austria Helsinki Finland I. de Margarita Venezuela Kirkland USA Kobenhavn Denmark Köln Germany Lander USA Leipzig Germany Lille France Lisboa Portugal London UK Luleå Sweden Lyon France Madrid Spain Mannheim Germany Marseille France México D.F. Mexico Montréal Canada München Germany Münster Germany Nantes France Oulu Finland Paris France Portland USA Reggio Emilia Italy Reims France Resende Brazil Rio de Janeiro Brazil Salzburg Austria San Cristóbal Venezuela San Francisco USA Sao Paulo Brazil Seattle USA Sevilla Spain Stavern Norway Strasbourg France Stuttgart Germany Torino Italy Toulouse France Tsawassen Canada Vancouver Canada Versailles France Walla Walla USA Warszawa Poland Press ENTER to continue...