Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Selecting from a table where my unique Key is not their un..

 
   Database Help (Home) -> Programming RSS
Next:  Procedure to insert into multiple tables  
Author Message
Mufasa

External


Since: Jan 24, 2008
Posts: 17



(Msg. 1) Posted: Tue Jul 22, 2008 12:56 pm
Post subject: Selecting from a table where my unique Key is not their unique key.
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I've got a list of customers, customer names, ... from another db (I didn't
design it).

I want to get a list of all of the customers plus the first customer name.
So if customer 1 appears twice (customer name = 'Fred' and 'John') I want
the first one it finds.

Is there an easy way to do this without spinning through all of the records.
I'm using the results of the query as a select statement as input to an
insert statement.

TIA - Jeff.

 >> Stay informed about: Selecting from a table where my unique Key is not their un.. 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jan 10, 2008
Posts: 2166



(Msg. 2) Posted: Tue Jul 22, 2008 1:24 pm
Post subject: Re: Selecting from a table where my unique Key is not their unique key. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

SQL Server doesn't understand the concept of "first" since a table is an
unordered set of rows. Do you have some other column that can be used to
break this tie? e.g. is there a created_date column or something? If so,
you could say:


SELECT
customer_id,
customer_name
FROM
customers c
INNER JOIN
(
SELECT customer_id,
created_date = MIN(created_date) -- or MAX?
FROM customers
GROUP BY customer_id
) s
ON c.customer_id = s.customer_id
AND c.created_date = s.created_date;


I suppose this could still yield ties of you have multiple rows for the same
customer with the same created_date. Hard to suggest anything further
without more details.




"Mufasa" wrote in message

> I've got a list of customers, customer names, ... from another db (I
> didn't design it).
>
> I want to get a list of all of the customers plus the first customer name.
> So if customer 1 appears twice (customer name = 'Fred' and 'John') I want
> the first one it finds.
>
> Is there an easy way to do this without spinning through all of the
> records. I'm using the results of the query as a select statement as input
> to an insert statement.
>
> TIA - Jeff.
>
>

 >> Stay informed about: Selecting from a table where my unique Key is not their un.. 
Back to top
Login to vote
Steve Kass

External


Since: Aug 28, 2003
Posts: 153



(Msg. 3) Posted: Tue Jul 22, 2008 2:11 pm
Post subject: Re: Selecting from a table where my unique Key is not their unique [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Mufasa,

In SQL Server 2005, it's not hard:

with T as (
select
*,
row_number() over (
partition by your_unique_key
order by their_unique_key
) as occurence
from theTable
)
select <columns you want>
from T
where occurence = 1

This selects the first occurrence in their-key order of
each customer (as defined by your-key).

Steve Kass
Drew University
http://www.stevekass.com

Mufasa wrote:

>I've got a list of customers, customer names, ... from another db (I didn't
>design it).
>
>I want to get a list of all of the customers plus the first customer name.
>So if customer 1 appears twice (customer name = 'Fred' and 'John') I want
>the first one it finds.
>
>Is there an easy way to do this without spinning through all of the records.
>I'm using the results of the query as a select statement as input to an
>insert statement.
>
>TIA - Jeff.
>
>
>
>
 >> Stay informed about: Selecting from a table where my unique Key is not their un.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Selecting everything in 2 different tables based on a valu.. - I have 5 tables. [Tablen1, Tablen2, Tablex1, Tablex2 and Table3] I need to be able to select all in either tablen1 and tablen2 or tablex1 and tablex2 based on a value from Columnx in table 3. How do you do something like this? That wouldn't be the end...

ODBC error messages when selecting from a linked table - Hi all, I am trying to link to an OMNUS database and create a SQL 2005 database for SRS reporting. I linked to the ODBC driver and all seemed well. I did a simple "Select * from OPENQUERY (Aware,'SELECT * FROM f_work') " First error: Conve...

SELECTing Rows to Columns - How to Get Column Alias From T.. - Considering the following query which transposes rows into columns as desired; please notice that the column name aliases are "hard coded" (e.g., AS ABO, AS RHFactor, AS A1, AS A2, etc). Each row in the table Lookup_TransplantParticipantFact...

Unique identifier in temporary table - Hi, I'm using a temporary table in my T-SQL code and I would like to include a unique identifier, i.e. automatically increase for each row in the table. Is there a simple way to achieve this? Adam

How to tag 500 rows of a table without a unique key - i want to flag 500 rows in a staging table as the rows i'm going to process right now. Once they've been processed they will be deleted, and another 500 will get marked. i don't really care which 500 rows, just 500 of them. If the rows has a unique..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]