Author |
Topic |
Muhammad Nasir
Starting Member
10 Posts |
Posted - 2011-02-04 : 00:42:24
|
Hello FriendsI need you expert help on the issue i am currently faceingI have two tables with large amount of data. Below describes the structure of the tablesClients--------ClientID, ClinetName, Address, ContactPerson--------------------------------------1 Mark StephenClient_Survey-------------Client_ID, Question_Number Question_Name, Answer1 1 Mobile User Yes1 2 Status Employee1 3 Nationality USANow i want to write a query which could display the result as belowClient_ID,Name,Address,Contact_person,Nationality,Status,Mobile_UserMy question is how would i do that ?My approach is that i should write a scalar function to whome i pass two parameters (clinet_ID and QuestionNumber) and that function will return a scalar value which will eventually become a coloum of my report for exampleSelect *,dbo.fn_GetAnserFromSurver(1,2) as Status,dbo.fn_GetAnserFromSurver(1,3) as NationalityNow the proble is that i have a large amount of data and scalar functions are reducing my speed of query. Please help me with some solutionThanxs and accept my appologies for the long post |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-02-04 : 01:21:21
|
Well first of all I do not see some of the required output columns (as highlighted below) in the original tables .. Client_ID,Name,Address,Contact_person,Nationality,Status,Mobile_Userwhere are the coming from ? |
 |
|
Ranjit.ileni
Posting Yak Master
183 Posts |
Posted - 2011-02-04 : 01:57:36
|
Hi, check it once ...(I wrote this query for display your the expected result) this may usefulcreate table #Clients(ClientID int, ClinetName varchar(20), Address varchar(20), ContactPerson varchar(20))goinsert into #Clients(ClientID,ClinetName,Address,ContactPerson)values(1,'Mark','usa',' Stephen')insert into #Clients(ClientID,ClinetName,Address,ContactPerson)values(2,'Mark','usa',' Stephen')gocreate table #Client_Survey(Client_ID int, Question_Number int,Question_Name varchar(20), Answer varchar(20))goinsert #Client_Surveyselect 1,1,'Mobile User ','Yes' union allselect 1,2,'Status',' Employee' union allselect 1,3,'Nationality','USA'union allselect 2,1,'Mobile User ','Yes' union allselect 2,2,'Status',' Employee' goselect * from #Clientsselect * from #Client_Surveygoselect c.ClientID ,ClinetName ,Address ,ContactPerson ,min(Nationality) as Nationality ,min(Status)Status ,min([Mobile User ])[Mobile User ]from #Clients c join( SELECT Client_ID,[Mobile User ],[Status],[Nationality] FROM ( SELECT Client_ID,Question_Number,Question_Name,Answer FROM #Client_Survey ) AS source PIVOT ( min(Answer) FOR Question_Name IN ([Mobile User ] ,[Status],[Nationality]) ) as pvt )Der on c.ClientID=Der.Client_IDgroup by c.ClientID,ClinetName,Address,ContactPersonresult:ClientID ClinetName Address ContactPerson Nationality Status Mobile User 1 Mark usa Stephen USA Employee Yes2 Mark usa Stephen NULL Employee Yes--Ranjit |
 |
|
Muhammad Nasir
Starting Member
10 Posts |
Posted - 2011-02-06 : 19:45:56
|
Hi RanjitThank you so much- You are wonderful |
 |
|
Ranjit.ileni
Posting Yak Master
183 Posts |
Posted - 2011-02-06 : 22:46:47
|
wel come--Ranjit |
 |
|
|
|
|