0

I have to match two tables, table1 and table2, for one row of table1 I have two or more rows in table 2 and I want to display them in one row.

Now I have the following:

Select
    a.[ID], b.[Description]
From 
    table1 a, table2 b 
Where 
    a.[ID] = b.[ID];

Output:

[ID] | [Description]
-----+--------------
1    | Fee
1    | Domestic Fee
2    | Fee
2    | International Fee 

I want to get the following result

[ID] | [Description1] | [Description2]
-----+----------------+---------------
1    | Fee            | Domestic Fee
2    | Fee            | International Fee

Thank you in advance :)

3
  • 2
    Search for PIVOT. What if there will be more than 2 descriptions? Commented Aug 8, 2017 at 14:08
  • 4
    Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style was replaced with the proper ANSI JOIN syntax in the ANSI-92 SQL Standard (25 years ago) and its use is discouraged Commented Aug 8, 2017 at 14:09
  • I searched for PIVOT but it needs some aggregate functions, which I don't need. There will be fixed amount of descriptions per on row for table1 Commented Aug 8, 2017 at 14:14

4 Answers 4

1

If you have multiple descriptions then you can use Pivot as below:

Select * from (
    Select *, RowN = Concat('Description', Row_Number() over (partition by Id order by Id)) from #description ) a
    pivot (max([Description]) for RowN in ([Description1],[Description2])) p

Output as below:

+----+--------------+-------------------+
| Id | Description1 |   Description2    |
+----+--------------+-------------------+
|  1 | Fee          | Domestic Fee      |
|  2 | Fee          | International Fee |
+----+--------------+-------------------+
Sign up to request clarification or add additional context in comments.

1 Comment

You have to make that dynamic sql. What will happen if there's a third Description -- it's not specified in your 'for' clause.
1

This is my proposal with cross apply operator

data prepartaion:

create table tst_1 (id int, dsc nvarchar(30))

insert into tst_1 (id, dsc)
values (1,'Fee'),(1,'Domestic Fee'),(2,'Fee'),(2,'International Fee')

Next simple select with jon show data that you looking for:

select t1.id, t1.dsc, x.dsc
from tst_1 t1
cross apply ( select row_number() over (order by id) as lp
                                        ,id
                                        ,dsc
                            from tst_1 )x
where x.id = t1.id and x.dsc <> t1.dsc
and lp%2 = 0

tst_1 can be a view based on select ... from you question.

Comments

0
Select
    a.[ID],a.[Description], b.[Description]
From 
    table1 a left outer join table2 b 
on
    a.[ID] = b.[ID];``

3 Comments

why LEFT OUTER JOIN and not INNER JOIN ?
Is there a [description] field in Table 1? I think you're making assumptions.
@greespark just an assumption.
0

try this:

select ID, DESCRIPTION1, DESCRIPTION2
from
(
    Select a.[ID]
      ,b.[Description]
      ,'DESCRIPTION' + CAST(ROW_NUMBER() OVER(PARTITION BY a.ID ORDER BY a.ID) AS VARCHAR(255))AS RN
  from table1 a 
  JOIN  table2 b ON  a.[ID] = b.[ID] 
) d
pivot
(
  max([Description])
  for RN in (DESCRIPTION1,DESCRIPTION2)
) piv;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.