Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server group, filter and merge columns to show just one row
    primarykey
    data
    text
    <p>I have a table that grows vertically with properties. I am using PIVOT to convert the rows values into columns and get required data. Now, I am trying to concatenate the values stored in two separate rows based on some filter. After concatenation, I just need to show one row out of this group and second row's id column should go as new column in the resultset.</p> <p>I have a sample database table and a query that I have worked on:</p> <p><strong>Source table:</strong></p> <pre><code>id name 1 weight 2 height 3 bp-systolic 4 bp-diastolic 5 ABI 6 Notes 7 bp-systolic 8 bp-diastolic </code></pre> <p><strong>Properties table:</strong></p> <pre><code>id propertykey propertyvalue 1 value 200 1 unit lbs 1 ExtId 7 2 value 74 2 unit in 2 ExtId 8 3 value 120 3 unit mm[Hg] 3 ExtId 9 4 value 80 4 unit mm[Hg] 4 ExtId 9 7 value 126 7 unit mm[Hg] 7 ExtId 10 8 value 87 8 unit mm[Hg] 8 ExtId 10 </code></pre> <p>Now with my query, I am able to join the two tables to get the rows into column like this:</p> <pre><code>id name desc ExtId related_id 1 weight 200 lbs 7 2 height 74 in 8 3 bp-systolic 120 mm[Hg] 9 4 bp-diastolic 80 mm[Hg] 9 7 bp-systolic 126 mm[Hg] 10 8 bp-diastolic 87 mm[Hg] 10 </code></pre> <p>I used the following query:</p> <pre><code>select id, name, value + ' ' + unit [desc], '' as related_id from ( select s.ID, s.name, p.propertykey, p.propertyvalue from source S inner join properties P on s.ID = P.ID where s.name in ('height', 'weight', 'bp-systolic', 'bp-diastolic') ) as P PIVOT (MAX([PropertyValue]) FOR [PropertyKey] IN ([Value], [Unit], [ExtId])) AS PT </code></pre> <p>How can I combine/merge the data for 'bp-systolic' and 'bp-diastolic' having the same "ExtId" and show one row like this:</p> <pre><code>id name desc related_id 1 weight 200 lbs 2 height 74 in 3 bp-systolic 120/80 4 7 bp-systolic 126/87 8 </code></pre> <p>Note, that related_id gets the "id" column from the 'bp-diastolic' since they are related based on "ExtId".</p> <p>Here is my test database tables:</p> <pre><code>create table source (id int, name varchar(30)) create table properties (id int, propertykey varchar(30), propertyvalue varchar(30)) insert into source values (1, 'weight') insert into source values (2, 'height') insert into source values (3, 'bp-systolic') insert into source values (4, 'bp-diastolic') insert into source values (5, 'ABI') insert into source values (6, 'notes') insert into properties values (1, 'value', '200') insert into properties values (1, 'unit', 'lbs') insert into properties values (1, 'ExtId', '7') insert into properties values (2, 'value', '74') insert into properties values (2, 'unit', 'in') insert into properties values (2, 'ExtId', '8') insert into properties values (3, 'value', '120') insert into properties values (3, 'unit', 'mm[Hg]') insert into properties values (3, 'ExtId', '9') insert into properties values (4, 'value', '80') insert into properties values (4, 'unit', 'mm[Hg]') insert into properties values (4, 'ExtId', '9') insert into properties values (5, 'value', '123') insert into properties values (6, 'value', 'this is a sample note') </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload