2010-01-15 8 views
1

J'ai une table tblUser, avec des colonnesComment convertir des lignes de données en colonnes?

userId,firstName,LastName,Mobile,.......,QuestionID

je une autre table son nom tblResults, avec ces colonnes

questionID,Question,choiceID,choice,.........

tblUser 
----------------------------------------------------------------------------- 
userID - FirstName- LstName -Mobile ...  ...  ... - QuestionID - ChiceID 
----------------------------------------------------------------------------- 
001 - xx  -  yy - 03212   -     01  01 
001 - xx  -  yy - 03212   -     02  02 
002 - xxx  - yyy - 03425   -     01  02 
002 - xxx  - yyy - 03425  -      02  01 
003 - xxxx - yyyy - 03429   -     03  02 
003 - xxxx -  yyyy - 03429   -     03  01 
------------------------------------------------------------------------------ 

tblResults 
--------------------------------------------------------------------- 
QuestionID Question  ChoiceID  Chice ....  ....    
--------------------------------------------------------------------- 
01  - **Are you** - 01 - Male 
01  - **Areyou** - 02 - Female 
02  - **From**  - 01 - xxxxx 
02  - **FROM**  - 02 - yyyyy 
--------------------------------------------------------------------- 

Je veux obtenir le résultat indiqué dans la suite table

------------------------------------------------------------------------------- 
UserID FirstName LastName Mobile **Areyou** **From** 
------------------------------------------------------------------------------- 
001 - xx  - yy  - 03212 - Male - yyyyy 
002 - xxx - yyy - 03425 - Female - xxxxx 
003 - xxxx - yyyy - 03429 - Female - xxxxx   

Répondre

0

Vous obtenez les utilisateurs uniques avec leurs choises respectifs pour chaque question, et se joindre à la table tblResults une fois pour chaque question:

select 
    u.userID, u.FirstName, u.LastName, u.Mobile, 
    q1.Choice as Areyou, 
    q2.Choice as [From] 
from (
    select userID, FirstName, LastName, Mobile, 
    sum(case QuestionId when '01' then ChoiseId else 0 end) as ChoiseId1, 
    sum(case QuestionId when '02' then ChoiseId else 0 end) as ChoiseId2 
    from tblUser 
) as u 
inner join tblResults as q1 on q1.QuestionID = '01' and q1.ChoiseID = u.ChoiseId1 
inner join tblResults as q2 on q2.QuestionID = '02' and q2.ChoiseID = u.ChoiseId2 
0

baically, nous pouvons créer une table t (UserID, ChoiceID) pour chaque question, puis interne rejoindre toutes ces tables. alors nous avons obtenu (UserID, ChoiceID_for_Question_1, ChoiceID_for_Question_2, ...). ajouter l'infoinfomation, nous avons obtenu le résultat souhaité.


select t1.UserID, t1.FirstName, t1.LastName, t1.Mobile, t1.Choice, t2.Choice 
from 
    (select u.UserID, u.FirstName, u.LastName, u.Mobile, r.Choice 
    from tblUser u, tblResults r 
    where u.questionID = '01' and u.QuestionID=r.QuestionID and u.ChoiceID = r.ChoiceID) t1 
inner join 
    (select u.UserID UserID, r.Choice 
    from tblUser u, tblResults r 
    where u.questionID = '02' and u.QuestionID=r.QuestionID and u.ChoiceID = r.ChoiceID) t2 
/* 
inner join 
    (...) t3 
*/ 
on t1.UserID = t2.UserID /* and t2.UserID = t3.UserID */ 

0

Vous pouvez utiliser un PIVOT pour produire les résultats que vous voulez. Il existe deux méthodes, soit un Pivot statique que vous codez en dur les colonnes, soit un Pivot dynamique qui obtient la liste des colonnes au moment de l'exécution.

statique Pivot: (Voir SQL Fiddle With Demo)

create table tblUser 
(
    userid int, 
    fname varchar(10), 
    lname varchar(10), 
    mobile int, 
    questionid int, 
    choiceid int 
) 
insert into tblUser values (001, 'xx', 'yy', 03212, 01, 01) 
insert into tblUser values (001, 'xx', 'yy', 03212, 02, 02) 
insert into tblUser values (002, 'xxx', 'yyy', 03425, 01, 02) 
insert into tblUser values (002, 'xxx', 'yyy', 03425, 02, 01) 
insert into tblUser values (003, 'xxxx', 'yyyy', 03429, 03, 02) 
insert into tblUser values (003, 'xxxx', 'yyyy', 03429, 03, 01) 

create table tblResults 
(
    questionid int, 
    question varchar(10), 
    choiceid int, 
    choice varchar(10) 
) 
insert into tblresults values (01, 'Are you', 01, 'Male') 
insert into tblresults values (01, 'Are you', 02, 'Female') 
insert into tblresults values (02, 'From', 01, 'xxxxx') 
insert into tblresults values (02, 'From', 02, 'yyyyy') 

select * 
from 
(
    select u.userid, 
     u.fname, 
     u.lname, 
     u.mobile, 
     r.question, 
     r.choice 
    from tbluser u 
    left join tblresults r 
     on u.questionid = r.questionid 
     and u.choiceid = r.choiceid 
) x 
pivot 
(
    min(choice) 
    for question in([are you], [from]) 
) p 

Dynamic Pivot: (Voir SQL Fiddle with Demo)

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

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.question) 
      FROM tblresults c 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 

set @query = 'SELECT userid, fname, lname, mobile, ' + @cols + ' from 
      (
       select u.userid, 
        u.fname, 
        u.lname, 
        u.mobile, 
        r.question, 
        r.choice 
       from tbluser u 
       left join tblresults r 
        on u.questionid = r.questionid 
        and u.choiceid = r.choiceid 
      ) x 
      pivot 
      (
       min(choice) 
       for question in (' + @cols + ') 
      ) p ' 


execute(@query) 

deux produira les mêmes résultats.