Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Another variation on row concatenation

$
0
0

Hi,

Suppose I have a table like this:

IF OBJECT_ID('dbo.Soils', 'U') IS NOT NULL
DROP TABLE dbo.Soils;
CREATE TABLE Soils(
 CHKEY varchar (5) NULL,
 LAYER1 int NULL,
 A1 decimal (4, 1) NULL,
 B1 decimal (4, 1) NULL
);

INSERT INTO dbo.Soils(CHKEY, LAYER1, A1, B1)
VALUES ('a', 1, 6.1, 2.3), ('a', 2, 5.1, 2.3), ('a', 3, 8.1, 3.2), ('a', 4, 6.4, 2.0),
    ('b', 1, 2, 1), ('b', 2, 2.1, 5.1),
    ('c', 1, 3.2, 12.0), ('c', 2, 8.1, 7.1), ('c', 3., 10.5, 6.0);

SELECT * FROM Soils;

The above creates a table like this:

CHKEYLAYER1    A1B1
a16.12.3
a25.12.3
a38.13.2
a46.42
b121
b22.15.1
c13.212
c28.17.1
c310.56

How do I create a T-SQL query to get the following output:

CHKEY LAYER1   A1   B1     LAYER2  A2     B2       LAYER3   A3    B3       LAYER4 A4      B4
a            1         6.1  2.3    2            5.1   2.3      3             8.1   3.2      4         6.4       2
b            1         2     1       2            2.1   5.1      
c            1         3.2   12     2            8.1   7.1      3            10.5   6   

Appreciate any help.


Marilyn Gambone


Viewing all articles
Browse latest Browse all 23857

Trending Articles