Solved

Select Returns Duplicates lines

Posted on 2008-10-24
5
194 Views
Last Modified: 2012-05-05
Hi

I have in issue where a select statement is duplicating lines.  This is due to the following

Table1
Col1_1            Col1_2
1                           Bob
2                           Bill
3                           Ted

Table2
Col2_1            Col2_2
1            Red
1            Blue
2            Red
3            Green

If I do a

select Col1_1, Col1_2, Col2_2
From    Table_1 INNER JOIN Table_2 ON Table_1.Col1_1 = Table_2.Col2_1

I get 2 lines for Bob.

How can I get just one line?

Cheers

Brasso  
0
Comment
Question by:brasso_42
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22794933
well, depends on what information from Table2.Col2_2 you want to get, in the end result.
please clarify
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 22794936
brasso_42 said:
>>How can I get just one line?

The answer depends on which record from Table2 "wins" in the join.  For example, you could do this:

select t1.Col1_1, t1.Col1_2, MAX(t2.Col2_2) AS Col2_2
From    Table_1 t1 INNER JOIN Table_2 t2 ON t1.Col1_1 = t2.Col2_1
GROUP BY t1.Col1_1, t1.Col1_2

or...

select t1.Col1_1, t1.Col1_2, MIN(t2.Col2_2) AS Col2_2
From    Table_1 t1 INNER JOIN Table_2 t2 ON t1.Col1_1 = t2.Col2_1
GROUP BY t1.Col1_1, t1.Col1_2
0
 
LVL 1

Author Comment

by:brasso_42
ID: 22794944
Well at the mo I'be happy with any thing :)

but I could could join the results e.g. Red Blue   that would be by far the best.  if not a min/max approach would be fine

Cheers

Brasso
0
 
LVL 1

Author Comment

by:brasso_42
ID: 22794990
Just spoken to my boss and what he really wants is them both on 1 line eg Red Blue

Sorry to be a pain

Many thanks for your help so far

Brasso
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help on t-sql 2012 10 53
I Need a T-SQL Query (Union or Intersect, or...?) 6 39
export sql results to csv 6 34
VBScript Write Column Headers 3 34
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question