Domanda

I have the two tables which need to be linked and present the data in human friendly way. Could you experts point me in right direction, I am bit stuck here.

Both table 1 and table2 are received through ftp and loaded to SQL table in SQL 2008 R2. These two tables are linked by nid and cid together.

Apologies i couldn't copy paste table here, please consider "-" are column separators

Table 1

ID  nid  cid  pid  form_key-name
 1    4    1   33  Import_Gen_Title-Title
 2    4    2   33  Import_Gen_Firstname-Firstname
 3    4    3   33  Import_Gen_Surname-Surname
 4    4    4   33  Import_Gen_AddressLine1-AddressLine1
 5    4    5   33  Import_Gen_AddressLine2-AddressLine2
 6    4    6   33  Import_Gen_City-Town/City
 7    4    7   33  Import_Gen_Zip-Post code

Table 2

ID  nid sid cid data
 1    4  14   1  Mr
 2    4  14   2  John
 3    4  14   3  Smith
 4    4  14   4  A Company
 5    4  14   5  Nice Street
 6    4  14   6  London
 7    4  14   7  SE11 0TS

Now how can get this a Result Table like this one below ?

NiD  SID Title  Firstname Surname  AddressLine1  AddressLine2  Town/City-Post code
  4   14    Mr       John   Smith  A Company     Nice Street   London-SE11 0TS
È stato utile?

Soluzione

Check my answer for this same question here: Pivot in sql server

I made a stored procedure that uses the PIVOT statement shown by salvoo - but it's more generic, you don't have to know all the columns in advance.

Take a look.

Here is the example using your data - note that the default output of the pivot has the columns in alphabetical order which you don't want - so I used the option to output the pivot to a temp table then queried directly from the temp table to put the columns in the order you wanted.

drop table Fields
go
drop table Data
go
create table Fields
   (
   ID                   Integer,
   nid                  Integer,
   cid                  Integer,
   pid                  Integer,
    form_key_name       varchar(50)
   );
go
create table Data
   (
   ID                   Integer,
   nid                  Integer,
   sid                  Integer,
   cid                  Integer,
   data                 varchar(50)
   );
go
insert into Fields values (1,4,1,33,'Import_Gen_Title-Title')
go
insert into Fields values (2,4,2,33,'Import_Gen_Firstname-Firstname')
go
insert into Fields values (3,4,3,33,'Import_Gen_Surname-Surname')
go
insert into Fields values (4,4,4,33,'Import_Gen_AddressLine1-AddressLine1')
go
insert into Fields values (5,4,5,33,'Import_Gen_AddressLine2-AddressLine2')
go
insert into Fields values (6,4,6,33,'Import_Gen_City-Town/City')
go
insert into Fields values (7,4,7,33,'Import_Gen_Zip-Post code')
go
insert into Data values (1,4,14,1,'Mr')
go
insert into Data values (2,4,14,2,'John')
go
insert into Data values (3,4,14,3,'Smith')
go
insert into Data values (4,4,14,4,'A Company')
go
insert into Data values (5,4,14,5,'Nice Street')
go
insert into Data values (6,4,14,6,'London')
go
insert into Data values (7,4,14,7,'SE11 0TS')
go
declare @mySQL varchar(MAX);

set @mySQL = '
select
   f.nid,
   d.sid,
   right(f.form_key_name, len(f.form_key_name) - charindex(''-'',f.form_key_name)) form_key_name,
   d.data
from
   Fields f

   JOIN Data d
      on ( d.nid = f.nid
           and d.cid = f.cid )
   ';

exec pivot_query @mySQL, 'nid, sid', 'form_key_name','max(data)', '##tmppivot';

select
   nid,
   sid,
   Title,
   Firstname,
   Surname,
   AddressLine1,
   AddressLine2,
   [Town/City],
   [Post code]
from
   ##tmppivot;
go

Altri suggerimenti

With Pivot: Fiddle demo

SELECT sid, nid,
       [Import_Gen_Title-Title] as Title,
       [Import_Gen_Firstname-Firstname] as Name,
       [Import_Gen_Surname-Surname] as SurName,
       [Import_Gen_AddressLine1-AddressLine1] as Address1,
       [Import_Gen_AddressLine2-AddressLine2] as Address2,
       [Import_Gen_City-Town/City] as Town,
       [Import_Gen_Zip-Post code] as PostCode
FROM
(
  SELECT t2.sid, t2.nid, t2.dat, t1.form_key
  FROM tab1 t1 INNER JOIN tab2 t2 ON t1.nid = t2.nid AND t1.cid = t2.cid
) x
PIVOT
(  
  min(x.dat)
  for x.form_key in ([Import_Gen_Title-Title],
                     [Import_Gen_Firstname-Firstname],
                     [Import_Gen_Surname-Surname],
                     [Import_Gen_AddressLine1-AddressLine1],
                     [Import_Gen_AddressLine2-AddressLine2],
                     [Import_Gen_City-Town/City],
                     [Import_Gen_Zip-Post code]
                    )
) pvt

Edit (Added generic version): Fiddle demo

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT ',[' + t.form_key + ']'
            FROM tab1 t
            group by t.form_key, cid
            order by t.cid
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)'),1,1,'');

SET @query = 'SELECT * FROM
(
  SELECT t2.sid, t2.nid, t2.dat, t1.form_key
  FROM tab1 t1 INNER JOIN tab2 t2 ON t1.nid = t2.nid AND t1.cid = t2.cid
) x
PIVOT
(
  min(x.dat)
  FOR x.form_key IN (' + @cols + ')
) pvt;';

execute(@query);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top