Click here to Skip to main content
15,033,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have data in below format --


CompanyId	ParamKey	        ParamValue
ABC	        MinLength	           8
ABC	        MaxLength	           0
ABC	        IsAlphanumeric	           1
ABC	        IsCaseSensitive	           0
ABC	        IsSpecialChars	           0
ABC	        PasswordExpiryDays	  80
ABC	        AlertDays	          10



Required Output Like This-
CompanyId MinLength MaxLength IsAlphanumeric IsCaseSensitive IsSpecialChars PasswordExpiryDays	AlertDays
  ABC	8	0	1	0	0	80	10


What I have tried:

I have tried using pivot but can not get required result. PLease help.
Posted
Updated 8-Mar-17 16:05pm
Comments
Animesh Datta 8-Mar-17 2:46am
   
provide your sql pivot query , so that we can find your mistake.
Tomas Takac 8-Mar-17 2:46am
   
You are supposed to put your SQL code in the "What I have tried" field. Use Improve question to do that.

I am assuming the following table structure (in particular that column paramvalue >= 0)
and providing the following query:
Table:
SQL
<pre>
create table companyvalues
(
companyid           varchar(5),
paramkey            varchar(30),
paramvalue          integer,
constraint pk_companyvalues primary key(companyid, paramkey),
constraint ck_paramvalue check(paramvalue >= 0)
);


with companynames as
(
select distinct companyid compid
from companyvalues
) ,
details as
(
select * from companyvalues
) 
select compid, max(MinLength) MinLength, max(MaxLength) MaxLength, max(IsAlphanumeric) IsAlphanumeric, 
max(IsCaseSensitive) IsCaseSensitive, max(IsSpecialChars) IsSpecialChars, max(PasswordExpiryDays) PasswordExpiryDays,
max(AlertDays) AlertDays
from
(
select compid 
		, case when  paramkey = 'MinLength'          then paramvalue else -1 end  MinLength         	
		, case when  paramkey = 'MaxLength'          then paramvalue else -1 end  MaxLength         	
		, case when  paramkey = 'IsAlphanumeric'     then paramvalue else -1 end  IsAlphanumeric    
		, case when  paramkey = 'IsCaseSensitive'    then paramvalue else -1 end  IsCaseSensitive   
		, case when  paramkey = 'IsSpecialChars'     then paramvalue else -1 end  IsSpecialChars    
		, case when  paramkey = 'PasswordExpiryDays' then paramvalue else -1 end  PasswordExpiryDays
		, case when  paramkey = 'AlertDays'          then paramvalue else -1 end  AlertDays         
from companynames, details
where compid = companyid
) finalresult
group by compid
order by 1
   
v2
Did you really try using PIVOT? Here is a working example.

SQL
DECLARE @cp2 TABLE (
Companyid	VARCHAR(50),
ParamKey	VARCHAR(50),
ParamValue	INT
)

INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'ABC', N'MinLength', 8)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'ABC', N'MaxLength', 0)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'ABC', N'IsAlphanumeric', 1)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'ABC', N'IsCaseSensitive', 0)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'ABC', N'IsSpecialChars', 0)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'ABC', N'PasswordExpiryDays', 80)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'ABC', N'AlertDays', 10)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'XYZ', N'MinLength', 7)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'XYZ', N'MaxLength', 1)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'XYZ', N'IsAlphanumeric', 3)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'XYZ', N'IsCaseSensitive', 1)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'XYZ', N'IsSpecialChars', 1)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'XYZ', N'PasswordExpiryDays', 90)
INSERT @cp2 ([CompanyId], [ParamKey], [ParamValue]) VALUES (N'XYZ', N'AlertDays', 5)

SELECT Companyid, MinLength, [MaxLength], IsAlphanumeric, IsCaseSensitive, IsSpecialChars,
PasswordExpiryDays,AlertDays
FROM (
SELECT Companyid, ParamKey, ParamValue
FROM @cp2) up
PIVOT (MAX(ParamValue) FOR ParamKey IN (MinLength, [MaxLength], IsAlphanumeric, IsCaseSensitive, IsSpecialChars,
PasswordExpiryDays,AlertDays)) AS pvt
ORDER BY Companyid
GO

Output:
Companyid	MinLength	MaxLength	IsAlphanumeric	IsCaseSensitive	IsSpecialChars	PasswordExpiryDays	AlertDays
ABC	8	0	1	0	0	80	10
XYZ	7	1	3	1	1	90	5
   
Quote:
I have tried using pivot

SQL Pivot is what you need.
Quote:
but can not get required result.

This is not informative, state in what the result is not what you want.
Show query, and actual result.

PIVOT and UNPIVOT in Sql Server | SqlHints.com[^]
Simple Way To Use Pivot In SQL Query[^]
Convert Rows to columns using 'Pivot' in SQL Server - Stack Overflow[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900