我正在做这样的动态调整,我想在旋转后用0替换字段中的Null值。不知道我怎么能实现这一点。这是我的询问。
DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);
DECLARE @users TABLE (
userId UNIQUEIDENTIFIER
,NAME VARCHAR(500)
);
INSERT INTO @users
SELECT u.id
,u.firstname + space(1) + u.lastname NAME
FROM users u
INNER JOIN userparametervalues upv ON u.id = upv.userid
INNER JOIN UserPermissions up ON u.id = up.userid
AND up.PermissionId IN (
'34FDF821-E3FF-43B2-AA90-0126BF5FAD56'
,'CFC3076F-0BED-4A41-BD1D-0240C4535132'
)
AND upv.parameterid = '137696EA-6B68-42E8-A020-2FA5A833F34E'
INNER JOIN users AS u1 ON upv.value = u1.id
AND u1.UserName IN (
'IN\libin.jose'
,'IN\Pallabi.P'
)
UNION
SELECT users.id id
,users.firstname + space(1) + users.lastname NAME
FROM users
WHERE UserName IN (
'IN\libin.jose'
,'IN\Pallabi.P'
)
SELECT @cols = STUFF((
SELECT DISTINCT ',' + QUOTENAME(NAME)
FROM @users
FOR XML PATH('')
,TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, '');
SELECT @query = '
SELECT *
FROM
(
select LoanPrograms.Name, (case when UserParameterValues.value is null then ''X'' else ''√'' end) as K ,(users.FirstName + space(1)+users.LastName ) L from LoanPrograms
left join UserParameterValues on LoanPrograms.id = UserParameterValues.ValueId and ParameterId = ''CD1DB446-75EF-4887-86ED-4313CCE6D2F1''
left join users on UserParameterValues.UserId = Users.Id
) AS t
PIVOT
(
MAX(k)
FOR L IN( ' + @cols + ' )' + ' ) AS p ; ';
EXECUTE (@query);
我得到的结果就是这些。
Name Akansha a Lia Jose Libin Pallabi a
----------------- ---------- --------- -------- ------
CONF 10 YEAR FIXED NULL NULL NULL Y
CONF 10 YR 105 NULL NULL NULL NULL
CONF 10 YR 125 NULL Y NULL NULL
CONF 10 YR LPMI Y NULL NULL NULL
CONF 10/1 LIBOR ARM NULL NULL NULL NULL
我想将所有空值替换为0。
答案 0 :(得分:1)
您需要在ISNULL
的最终SELECT
中使用PIVOT
功能。例如,您可以像这样构建列列表:
SELECT @SELECTEDcols = STUFF((
SELECT DISTINCT ',ISNULL([' + NAME + '], 0) AS [' + NAME + ']'
FROM @users
FOR XML PATH('')
,TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, '');
并用最终*
语句替换SELECT
。
SELECT @query = '
SELECT' + @SELECTEDcols + '
FROM
(
select LoanPrograms.Name, (case when UserParameterValues.value is null then ''X'' else ''√'' end) as K ,(users.FirstName + space(1)+users.LastName ) L from LoanPrograms
left join UserParameterValues on LoanPrograms.id = UserParameterValues.ValueId and ParameterId = ''CD1DB446-75EF-4887-86ED-4313CCE6D2F1''
left join users on UserParameterValues.UserId = Users.Id
) AS t
PIVOT
(
MAX(k)
FOR L IN( ' + @cols + ' )' + ' ) AS p ; ';
答案 1 :(得分:0)
SELECT @SELECTEDcols = STUFF((
SELECT DISTINCT ',ISNULL([' + NAME + '], 0) AS [' +NAME + ']'
FROM @users
FOR XML PATH('')
,TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, '');
SELECT @query = '
SELECT' + @SELECTEDcols + '
FROM
(
select LoanPrograms.Name, (case when UserParameterValues.value is null then ''X'' else ''√'' end) as K ,(users.FirstName + space(1)+users.LastName ) L from LoanPrograms
left join UserParameterValues on LoanPrograms.id = UserParameterValues.ValueId and ParameterId = ''CD1DB446-75EF-4887-86ED-4313CCE6D2F1''
left join users on UserParameterValues.UserId = Users.Id
) AS t
PIVOT
(
MAX(k)
FOR L IN( ' + @cols + ' )' + ' ) AS p ; ';