SELECT DISTINCT?

Posts   
 
    
mshe
User
Posts: 167
Joined: 02-Feb-2006
# Posted on: 02-Oct-2007 21:16:52   

Is there a way to fetch a list of entities based on a distinct field?

i.e

Customers
    CustomerID
    CustomerName
    Phone
    OtherField

i.e. SELECT DISTINCT(CustomerName), CustomerID from Customers

I'm using LLBLGen Pro 2.0 w/ Self-Servicing.

Thanks!

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 02-Oct-2007 23:10:41   

use a typedlist, dynamiclist or project the results onto an entity collection/custom collection.

your query will not work. distinct applies to the entire select portion of the query. it would look like this

select distinct CustomerName, CustomerId from Customers
mshe
User
Posts: 167
Joined: 02-Feb-2006
# Posted on: 02-Oct-2007 23:36:06   

jmeckley wrote:

use a typedlist, dynamiclist or project the results onto an entity collection/custom collection.

your query will not work. distinct applies to the entire select portion of the query. it would look like this

select distinct CustomerName, CustomerId from Customers

Weird, I tried it in query analyzer and it seemed to work ok.

peschkaj
User
Posts: 30
Joined: 21-Sep-2006
# Posted on: 03-Oct-2007 04:06:46   

If you have a unique constraint on the field, you can use FetchUsingUCXYZ. SELECT DISTINCT will return only the distinct combination of all columns on the query. It may have seemed to work because you only have unique values in your table.

If you actually need a field to be unique/distinct, you need to use a constraint.

mshe
User
Posts: 167
Joined: 02-Feb-2006
# Posted on: 03-Oct-2007 05:19:13   

True, I could use the Fetch UsingUCXYZ but it will only return 1 value, not ALL the unique values in the list?

Basically I want to return all unique entities based on a field.

But I guess the TypedList will do for now.

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 03-Oct-2007 14:22:04   

by defination an entity is unique (see help docs). you can use projection to populate an entitycollection with a distinct list of customers.

this way you get the required result contained within the model you need.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Oct-2007 15:29:26   

Basically I want to return all unique entities based on a field.

DISTINCT is applied on all the fields in the select list, not on one field. I don't understand what you are trying to do. Please post an example. post a table design schema, and a sample of rows data, and last but not least the query that you want to execute.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 04-Oct-2007 14:49:16   

sqlserver doesn't support this, only oracle does. LLBLGen Pro only supports DISTINCT per row, not per field.

Frans Bouma | Lead developer LLBLGen Pro
mshe
User
Posts: 167
Joined: 02-Feb-2006
# Posted on: 04-Oct-2007 16:10:26   

Thanks for the information Otis!