Re: SELECT'ing one row from duplicates

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Steve Kass (skass_at_drew.edu)
Date: 09/01/04


Date: Wed, 01 Sep 2004 16:44:02 -0400

Tom,

  Since multiple rows contain the same value of KEY, KEY is not a
primary key for the table. If you have a primary key (and the
collations for whichever of KEY and primary_key_column are strings match
on both servers), you can do this:

select KEY, ADDRESS_LINE_1, ADDRESS_LINE_2
from Tom T1
where primary_key_column = (
  select min(primary_key_column)
  from Tom T2
  where T2.KEY = T1.KEY
)

The solution you suggest will return NULL if there are no non-null
values of the column, and it can be made to give the same results on
both servers if you specify the same collation (i.e., write
MIN(ADDRESS_LINE_1 COLLATE Latin1_General_CS_AS), or whatever collation
you want). But it may not give you ADDRESS_LINE_1 and ADDRESS_LINE_2
values from the same row of the table. Additionally, if there is a
reason for having different collations for ADDRESS_LINE_1/2 on the two
different servers, you really don't have the same table structure on
each server, and it's not even clear what it means to want the same
answer from each server. If you get answer X from one server and answer
Y from the other, it could be that X=Y is true under the first server's
collation and X=Y is false under the second server's collation. The
notion of "same" for strings depends on the collation.

Steve Kass
Drew University

Tom Richards wrote:

>I have a table with a key and a number of fields which constitute an address
>
>eg:
>
>KEY ADDRESS_LINE_1 ADDRESS_LINE_2
>-------------------------------------------------
>FRED 1, HOG STREET HOGLAND
>FRED 2, HOG STREET HOGLANDIO
>FRED NULL NULL
>
>I need to select the key (ie FRED) and one of these addresses. However, the
>select will reside on 2 different sql server 2000 servers and needs to behave
>the same on both.
>
>Can I do something like
>
>SELECT KEY,
> MIN(ADDRESS1),
> MIN(ADDRESS2)
>FROM TABLE
>GROUP BY KEY
>
>Will the fact that MIN sorts in character order sequence ensure that I get
>the same row back on both servers? Also, what happens to NULLs? I'm hoping I
>get a NULL back rather than the row being ignored...
>
>Thanks for any help,
>Tom
>
>
>
>



Relevant Pages

  • Re: Pros and Cons of CASE SENSITIVE COLLATION SETTINGS
    ... is there any good reason to use a database Case Sensitive collation? ... if you are creating code to be deployed on servers and/or databases where you have no control over the collation. ...
    (microsoft.public.sqlserver.clients)
  • Re: insert into linked server problem
    ... Distributed transactions across servers are substantially slower than ... both SQL Servers with the same collation settings, ... But if i execute this one ...
    (comp.databases.ms-sqlserver)
  • Re: SELECTing one row from duplicates
    ... The server collations are the same. ... If you have a primary key (and the ... > from Tom T1 ... > collation and X=Y is false under the second server's collation. ...
    (microsoft.public.sqlserver.programming)
  • Re: Indexed View Replication with wrong snapshot result
    ... The servers' setting are the same, collation as ... "Paul Ibison" wrote: ... > HTH, ...
    (microsoft.public.sqlserver.replication)
  • Using "Table" variable
    ... INT IDENTITYNOT NULL PRIMARY KEY, ... My current database and its objects are in the collation named "COLLATE ...
    (microsoft.public.sqlserver.programming)