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.
Author |
Topic |
shinelawrence
Starting Member
32 Posts |
Posted - 2013-07-02 : 06:56:41
|
Hi Everyone, I have 5 rows in my table I want to get Multiple column for each row. How to do... My Current O/P: AppNo AppName Type100 Lawrence Individual100 Smith Partner100 Raja Private100 Harsha Public100 Fedal IndividualI need like this O/P: AppNo AppName1 AppName2 AppName3 AppName4 AppName5 Type1 Type2100 Lawrence Smith Raja Harsha Fedal Individual PartnType3 Type4 Type5Private Public IndividualHow to get... Just tell the solution... Thanks In AdvancedLawce |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-02 : 07:07:39
|
How to get... Just tell the solution... What a way to ask for spoonfed answers! I'm not intending to give you spoonfed just for this one statementLook for cross tabing in google and you'll get lots of solutionsALso look for syntax of PIVOT in books online------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
shinelawrence
Starting Member
32 Posts |
Posted - 2013-07-02 : 07:10:43
|
no...not like that... I don't know how to use two pivot in one query...Please tell the solution....Lawce |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
shinelawrence
Starting Member
32 Posts |
Posted - 2013-07-02 : 07:21:26
|
I'll get partial but the type column shown empty.... Please tell the solution....Lawce |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-02 : 07:22:19
|
quote: Originally posted by shinelawrence I'll get partial but the type column shown empty.... Please tell the solution....Lawce
show your used code------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
shinelawrence
Starting Member
32 Posts |
Posted - 2013-07-02 : 07:24:06
|
SELECT ApplicationNo,ApplicantName,ISNULL([1],'') AS AppName1,ISNULL([2],'') AS AppName2,ISNULL([3],'') AS AppName3,ISNULL([4],'') AS AppName4,Pk_id,ISNULL([6],'') AS Guar1,ISNULL([7],'') AS Guar2,ISNULL(,'') AS Guar3,ISNULL([9],'') AS Guar4,ISNULL([10],'') AS Guar5,GuarConstitution FROM(SELECT ROW_NUMBER() OVER (PARTITION BY A.ApplicationNo ORDER BY A.ApplicationNo) AS Seq,A.ApplicationNo,G.GuarantorName,A.ApplicantName,A.Unitshrtdesr,A.Pk_id,G.GuarConstitution FROM Loln_caApplicantInfo A LEFT JOIN Loln_caGuarantor G ON G.ApplicationNo=A.ApplicationNo WHERE A.Unitshrtdesr='ABR' AND A.ApplicationNo NOT IN (SELECT ApplicationNo FROM Loln_caDoclist) AND A.ApplicationNo='ABR0000002')t PIVOT(MAX(GuarantorName) FOR Seq IN ([1],[2],[3],[4],[5],[6],[7],,[9],[10])) AS pLawce |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-02 : 07:31:03
|
[code];With CTEAS(SELECT ROW_NUMBER() OVER (PARTITION BY A.ApplicationNo ORDER BY A.ApplicationNo) AS Seq,A.ApplicationNo,G.GuarantorName,A.ApplicantName,A.Unitshrtdesr,A.Pk_id,G.GuarConstitution FROM Loln_caApplicantInfo A LEFT JOIN Loln_caGuarantor G ON G.ApplicationNo=A.ApplicationNo WHERE A.Unitshrtdesr='ABR' AND A.ApplicationNo NOT IN (SELECT ApplicationNo FROM Loln_caDoclist) AND A.ApplicationNo='ABR0000002')SELECT m.ApplicationNo,m.[1] AS AppName1,m.[2] AS AppName2,m.[3] AS AppName3,m.[4] AS AppName4,m.[5] AS AppName5,n.[1] AS Guar1,n.[2] AS Guar2,n.[3] AS Guar3,n.[4] AS Guar4,n.[5] AS Guar5FROM(SELECT *FROM (SELECT ApplicationNo,Seq,ApplicantNameCTE )c1PIVOT(MAX(ApplicantName) FOR Seq IN ([1],[2],[3],[4],[5]))p)mINNER JOIN (SELECT *FROM (SELECT ApplicationNo,Seq,GuarantorNameCTE )c1PIVOT(MAX(GuarantorName) FOR Seq IN ([1],[2],[3],[4],[5]))p)nON n.ApplicationNo = m.ApplicationNoAND n.Seq = m.Seq[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
shinelawrence
Starting Member
32 Posts |
Posted - 2013-07-02 : 07:40:22
|
i got these errors:Msg 207, Level 16, State 1, Line 26Invalid column name 'ApplicationNo'.Msg 207, Level 16, State 1, Line 26Invalid column name 'Seq'.Msg 207, Level 16, State 1, Line 26Invalid column name 'ApplicantName'.Msg 207, Level 16, State 1, Line 28Invalid column name 'ApplicantName'.Msg 207, Level 16, State 1, Line 33Invalid column name 'ApplicationNo'.Msg 207, Level 16, State 1, Line 33Invalid column name 'Seq'.Msg 207, Level 16, State 1, Line 33Invalid column name 'GuarantorName'.Msg 207, Level 16, State 1, Line 35Invalid column name 'GuarantorName'.Msg 207, Level 16, State 1, Line 38Invalid column name 'Seq'.Msg 207, Level 16, State 1, Line 38Invalid column name 'Seq'.Lawce |
|
|
shinelawrence
Starting Member
32 Posts |
Posted - 2013-07-02 : 07:42:29
|
Now got 2 errors:Msg 207, Level 16, State 1, Line 36Invalid column name 'Seq'.Msg 207, Level 16, State 1, Line 36Invalid column name 'Seq'.Lawce |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-02 : 07:46:58
|
quote: Originally posted by shinelawrence Now got 2 errors:Msg 207, Level 16, State 1, Line 36Invalid column name 'Seq'.Msg 207, Level 16, State 1, Line 36Invalid column name 'Seq'.Lawce
Show your used query if different from mine------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
shinelawrence
Starting Member
32 Posts |
Posted - 2013-07-02 : 07:51:30
|
thanks....i solved that issue.... thank u very much....Lawce |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-02 : 07:55:48
|
quote: Originally posted by shinelawrence thanks....i solved that issue.... thank u very much....Lawce
Ok..Good that you got it sorted outNow try to understand the query by reading PIVOT syntax and usage in MSDN or books online------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
shinelawrence
Starting Member
32 Posts |
Posted - 2013-07-02 : 23:53:26
|
ok...sure....thanksLawce |
|
|
|
|
|
|
|