前の質問からの回答に続いて、いくつかの場合に番号がないときにいくつかのNULL値を取り除きたい
私は次のクエリを持っています
CREATE TABLE [MYTABLE]
(
[A] int
, [B] int
, [VAL] float
);
INSERT INTO [MYTABLE] ([A], [B], [VAL]) VALUES
(1, 1, -218.46),
(1, 2, -2.1846000000000e+02),
(2, 1, 6.4000000000000e+01),
(2, 2, -218.46),
(2, 3, 6.4000000000000e+01),
(3, 2, -2.1846000000000e+02),
(3, 3, -218.46),
(4, 3, 6.4000000000000e+01),
(4, 4, -218.46),
(4, 5, 6.4000000000000e+01),
(5, 4, 6.4000000000000e+01),
(5, 5, -218.46),
(5, 6, 6.4000000000000e+01),
(6, 5, 6.4000000000000e+01),
(6, 6, -218.46),
(6, 7, 6.4000000000000e+01),
(7, 6, 6.4000000000000e+01),
(7, 7, -218.46),
(7, 8, 6.4000000000000e+01),
(8, 7, 6.4000000000000e+01),
(8, 8, -218.46),
(8, 9, 6.4000000000000e+01),
(9, 8, 6.4000000000000e+01),
(9, 9, -218.46),
(9, 10, 6.4000000000000e+01),
(10, 9, 6.4000000000000e+01),
(10, 10, -218.46);
SELECT [1],[2],[3],[4],[5],[6],[7],[8],[9],[10]
FROM
( SELECT x = a
, y = b
, v = (CASE WHEN IsNumeric(val)=0 THEN 0 ELSE val END)
FROM MYTABLE
) AS T
PIVOT
(
min( v)
FOR y IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])
) AS pvt
order by x
次の結果が得られます。
1 2 3 4 5 6 7 8 9 10
------- ------- ------- ------- ------- ------- ------- ------- ------- -------
-218.46 -218.46 NULL NULL NULL NULL NULL NULL NULL NULL
64 -218.46 64 NULL NULL NULL NULL NULL NULL NULL
NULL -218.46 -218.46 NULL NULL NULL NULL NULL NULL NULL
NULL NULL 64 -218.46 64 NULL NULL NULL NULL NULL
NULL NULL NULL 64 -218.46 64 NULL NULL NULL NULL
NULL NULL NULL NULL 64 -218.46 64 NULL NULL NULL
NULL NULL NULL NULL NULL 64 -218.46 64 NULL NULL
NULL NULL NULL NULL NULL NULL 64 -218.46 64 NULL
NULL NULL NULL NULL NULL NULL NULL 64 -218.46 64
NULL NULL NULL NULL NULL NULL NULL NULL 64 -218.46
しかし、私は次の結果が必要です
1 2 3 4 5 6 7 8 9 10
------- ------- ------- ------- ------- ------- ------- ------- ------- -------
-218.46 -218.46 NULL NULL 0 0 0 0 0 0
64 -218.46 64 0 0 0 0 0 0 0
0 -218.46 -218.46 0 0 0 0 0 0 0
0 0 64 -218.46 64 0 0 0 0 0
0 0 0 64 -218.46 64 0 0 0 0
0 0 0 0 64 -218.46 64 0 0 0
0 0 0 0 0 64 -218.46 64 0 0
0 0 0 0 0 0 64 -218.46 64 0
0 0 0 0 0 0 0 64 -218.46 64
0 0 0 0 0 0 0 0 64 -218.46
すべてのNULL値を0に置き換えるにはどうすればよいですか?
編集
@Sivaの回答をフォローする
CREATE TABLE [dbo].[Matrix]
(
[A] INT
, [B] INT
, [VAL] FLOAT
);
INSERT INTO [dbo].[Matrix] ([A], [B], [VAL])
VALUES
(1, 1, -218.46)
, (1, 2, -2.1846000000000e+02)
, (2, 1, 6.4000000000000e+01)
, (2, 2, -218.46)
, (2, 3, 6.4000000000000e+01)
, (3, 2, -2.1846000000000e+02)
, (3, 3, -218.46)
, (4, 3, 6.4000000000000e+01)
, (4, 4, -218.46)
, (4, 5, 6.4000000000000e+01)
, (5, 4, 6.4000000000000e+01)
, (5, 5, -218.46)
, (5, 6, 6.4000000000000e+01)
, (6, 5, 6.4000000000000e+01)
, (6, 6, -218.46)
, (6, 7, 6.4000000000000e+01)
, (7, 6, 6.4000000000000e+01)
, (7, 7, -218.46)
, (7, 8, 6.4000000000000e+01)
, (8, 7, 6.4000000000000e+01)
, (8, 8, -218.46)
, (8, 9, 6.4000000000000e+01)
, (9, 8, 6.4000000000000e+01)
, (9, 9, -218.46)
, (9, 10, 6.4000000000000e+01)
, (10, 9, 6.4000000000000e+01)
, (10, 10, -218.46);
SELECT COALESCE([1], 0) [1]
, COALESCE([2], 0) [2]
, COALESCE([3], 0) [3]
, COALESCE([4], 0) [4]
, COALESCE([5], 0) [5]
, COALESCE([6], 0) [6]
, COALESCE([7], 0) [7]
, COALESCE([8], 0) [8]
, COALESCE([9], 0) [9]
, COALESCE([10], 0) [10]
FROM
(
SELECT x = a
, y = b
, v = val
FROM [dbo].[Matrix]
) AS dataToPivot
PIVOT
(
MIN(v)
FOR y IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10])
) AS pivoted
ORDER BY x;
ダイナミックでこれを行うための最良の方法は何でしょうか、私は最初の部分のためにやろうと思っていました...:
DECLARE @sql nvarchar(max);
DECLARE @Flag INT;
DECLARE @Size INT;
SET @Flag = 1
SET @Size = 10 ;
SET @sql = 'SELECT '+ CHAR(13);
WHILE (@Flag <= @Size) BEGIN
SET @sql = @sql + ' COALESCE([' + cast( @Flag as varchar(10)) + '], 0) ['+ cast( @Flag as varchar(10))+'],'+ CHAR(13) ;
SET @Flag = @Flag + 1;
END
SET @sql = LEFT(@sql, LEN(@sql) - 2)
SET @sql = @sql + '
FROM
(
SELECT x = a
, y = b
, v = val
FROM [dbo].[Matrix]
) AS dataToPivot
PIVOT
(
MIN(v)
FOR y IN (';
SET @Flag = 1
WHILE (@Flag <= @Size) BEGIN
SET @sql = @sql + ' [' + cast( @Flag as varchar(10)) + '],';
SET @Flag = @Flag + 1;
END
SET @sql = LEFT(@sql, LEN(@sql) - 1);
SET @sql = @sql +
')
) AS pivoted
ORDER BY x;'
print @sql
exec(@sql);