Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 Pivot on strings

Author  Topic 

Johno3535
Starting Member

1 Post

Posted - 2010-09-05 : 02:14:17

Hi,
I have 2 tables that I would like to join in a view. But the second table has a varying number of multiple records for each record in the first table.

Here is a simplified version of the two tables:
Table 1:
ID    Type  Priority
------- ----- -----
myid1 major 1
myid2 minor 2
myid3 other 8

Table 2:
ID    Keyvalue datavalue
------- -------- ---------
myid1 url      Id1value
myid1 user    Timmy
myid2 valid    True
myid3 security authorized

I would like a view that produces the following result:
ID    Type  Priority url user valid security
----- ----- -------- --- ----- ----- --------
myid1 major 1 id1value Timmy null null
myid2 minor 2 null null True null
myid2 other 8 null null null authorized

I was hoping to do this using a pivot to make the keyvalue field become columns in the view with the data for those columns coming from the datavalue field. But it seems pivots need aggregates.

Can anyone tell me the best way to accomplish what I am trying to do?
Thanks!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-09-05 : 09:24:30
MAX() and MIN() aggregate functions work on character and datetime data types.
Go to Top of Page
   

- Advertisement -