Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / February 2007

Tip: Looking for answers? Try searching our database.

return duplicate records

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
alex - 27 Feb 2007 13:42 GMT
Hello experts,

I'm trying the run the following query with specific intentions.

I would like the query to return 5 results; i.e., 4 distinct and one
duplicate.  I am only getting, however, 4 distinct records.  I would
like the results from the '007' id to spit out twice.

I'm not using 'distinct,' and I've tried 'all.'  I realize that I
could put my 5 employee id's in a table and do a left or right join; I
would like to avoid that, however.   Any thoughts?

Select
Employee_last_name,
Employee_first_name
>From tbl_employee
Where employee_id in (
'009',
'008',
'007',
'007',
'006'
);

alex
Steve Kass - 27 Feb 2007 15:36 GMT
Alex,

There are a few solutions. Two are (might have typos, but you should be
able to get the idea):

select Employee_last_name, Employee_first_name
from tbl_employee
join (
  select '009' as id union all
  select '008' as id union all
  select '007' as id union all
  select '007' as id union all
  select '006' as id
) as IDs
on IDs.id = tbl_employee.employee_id

or to make the specification of ids simpler:

declare @ids varchar(1000)
set @ids = '009008007007006'
declare @idlength int
set @idlength = 3

select Employee_last_name, Employee_first_name
from tbl_employee
join a_permanent_table_of_integers_from_0_to_whatever as Nums
on employee_id = substring(@ids,@idlength*n+1,@idlength)
and n < len(@ids)/@idlength
-- [n] is the column name for the permanent table and should
-- be that tables primary key

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

> Hello experts,
>
[quoted text clipped - 21 lines]
>
> alex
--CELKO-- - 28 Feb 2007 14:05 GMT
>> I would like the query to return 5 results; i.e., 4 distinct and one duplicate.  <<

The easy way is a UNION, based on a guess about the DDL you did bother
to post and the uniquness of emp_id:

SELECT last_name, first_name
 FROM Personnel
WHERE emp_id IN ('009', '008', '007', '006')
UNION
SELECT last_name, first_name
 FROM Personnel
WHERE emp_id = '007'
Erland Sommarskog - 28 Feb 2007 22:27 GMT
>>> I would like the query to return 5 results; i.e., 4 distinct and one duplicate.  <<
>
[quoted text clipped - 8 lines]
>   FROM Personnel
>  WHERE emp_id = '007'

Joe, I thought you knew SQL? This query will not return the results
that Alex was asking for.

Why is left as an exercise to the reader.

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.