INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How not to show duplicate rows on a join query

How not to show duplicate rows on a join query

(OP)
I have two tables

Table 1 and Table 2


Now, in Table 1 the data could look like this all on 1 row

Number Result 1 Result 2 Result 3
100 7.1 7.8 6.4


Table 2 has multiple rows

Number Test Percent
100 abc 1.3
100 cde 12.9
100 zzz 24.3


Now, if I join the tables the results will be


Number Result 1 Result 2 Result 3 Test Percent
100 7.1 7.8 6.4 abc 1.3
100 7.1 7.8 6.4 cde 12.9
100 7.1 7.8 6.4 zzz 24.3


How can I get the results to look like this


Number Result 1 Result 2 Result 3 Test Percent
100 7.1 7.8 6.4 abc 1.3
cde 12.9
zzz 24.3

Is this even possible ?

any help would be appreciated

Thanks


RE: How not to show duplicate rows on a join query

SQL joins work that way and not the way you want.

Some report engines would allow to group by eg Number and only output line 1 once. Or suppress repeated values. You surely have multiple options on whatever end point use of the data, it's not the job of SQL to provide it 1:1 the way you intend to display it. One thing you can do, when querying into a PHP array or .NET Datatable or whatever is to blank the surplus columns there as post processing.

Bye, Olaf.

RE: How not to show duplicate rows on a join query

(OP)
Thank you,

that's what I thought... Just wanted to check

RE: How not to show duplicate rows on a join query

Olaf is correct in that this is better accomplished on the front end. If it must be done in the database, you could try something like this:

CODE

Declare @T1 Table(Number Int, Result1 decimal(10,2), Result2 decimal(10,2), Result3 decimal(10,2))
Insert Into @T1 Values(100,7.1,7.8,6.4)

Declare @T2 Table(Number Int, Test VarChar(10), [Percent] Decimal(10,2))
Insert Into @T2 Values(100, 'abc', 1.3)
Insert Into @T2 Values(100, 'cde', 12.9)
Insert Into @T2 Values(100, 'zzz', 24.3)

; With Data As
(
  Select *, 
         Row_Number() Over (Partition By Number Order By Test) As RowId
  From   @T2 
)
Select  T2.Number, T1.Result1, T1.Result2, T1.Result3, T2.Test, T2.[Percent]
From    Data As T2
        Left Join @T1 As T1
          On T1.Number = T2.Number
          And T2.RowId = 1 

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

RE: How not to show duplicate rows on a join query

(OP)
Ahhhhh, This may work...

Appreciate it. I will still also look into grouping with the grid I'm using.

Thanks

RE: How not to show duplicate rows on a join query

Yes, the only way is to turn around the join but also do the tedious preprocessing of numbering rows to have that extra join condition RowId=1, I rather dislike this approach. As the join is on Number that's the thing to group data by. I'd perhaps fetch the data unjoined and do the joining clientside. If all else fails in two grids. One just showing the main line, the details only showing for the selected row.

Bye, Olaf.

RE: How not to show duplicate rows on a join query

It would be also nice if you would align the data to show it better:

"How can I get the results to look like this"

 Number Result_1 Result_2 Result_3 Test Percent
 100     7.1       7.8       6.4    abc   1.3
                                    cde  12.9
                                    zzz  24.3
 
Use PRE tag

Have fun.

---- Andy

There is a great need for a sarcasm font.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close