Firuz says:
For example: out result:
Id
|
Person
|
Product
|
Date
|
Product
|
Date
|
1
|
Ivan
|
|
|
oil
|
22.07.2017
|
2
|
Andrey
|
tomato
|
22.07.2016
|
|
|
3
|
Sasha
|
|
|
milk
|
22.07.2017
|
4
|
Kolya
|
meat
|
22.07.2015
|
You requirement doesn't make any sense as there is no uniqueness to define them in different columns and values in different rows as your requirement is to display data in zig zag manner which is not possible but you can get the person name by below query.
Refer below SQL Query.
SQL
DECLARE @MainTable AS TABLE (ID INT, Name VARCHAR(50))
INSERT INTO @MainTable VALUES(1,'Ivan')
INSERT INTO @MainTable VALUES(2,'Andrey')
INSERT INTO @MainTable VALUES(3,'Sasha')
INSERT INTO @MainTable VALUES(4,'Kolya')
DECLARE @RealtedTable AS TABLE (ID INT,IdPerson INT, Product VARCHAR(50), [Date] VARCHAR(50))
INSERT INTO @RealtedTable VALUES (1,1,'oil','22.07.2017')
INSERT INTO @RealtedTable VALUES (2,2,'tomato','22.07.2016')
INSERT INTO @RealtedTable VALUES (3,3,'melon','22.07.2017')
INSERT INTO @RealtedTable VALUES (4,4,'meat','22.07.2015')
SELECT * FROM @MainTable
SELECT IdPerson
,(SELECT Name FROM @MainTable main WHERE main.ID = rbl.IdPerson ) Person
,Product
,[Date]
FROM @RealtedTable rbl
OutPut
IdPerson |
Person |
Product |
Date |
1 |
Ivan |
oil |
22.07.2017 |
2 |
Andrey |
tomato |
22.07.2016 |
3 |
Sasha |
melon |
22.07.2017 |
4 |
Kolya |
meat |
22.07.2015 |