Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How not to show duplicate rows on a join query 1

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
US
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


 
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.

 
Thank you,

that's what I thought... Just wanted to check
 
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
 
Ahhhhh, This may work...

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

Thanks
 
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.
 
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"

[pre]
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
[/pre]
Use PRE tag

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top