Bryian Tan
Вы действительно пробовали использовать PIVOT? Вот рабочий пример.
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
Выход:
Companyid MinLength MaxLength IsAlphanumeric IsCaseSensitive IsSpecialChars PasswordExpiryDays AlertDays
ABC 8 0 1 0 0 80 10
XYZ 7 1 3 1 1 90 5