0

前の質問からの回答に続いて、いくつかの場合に番号がないときにいくつかの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);   
4

3 に答える 3

2

COALESCE結果を達成するために使用できます。

SQL フィドル

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;
于 2012-04-26T00:17:28.367 に答える
2

IsNull はどうですか?

SELECT 
isnull([1], 0) as [1],
isnull([2], 0) as [2],
isnull([3], 0) as [3],
isnull([4], 0) as [4],
isnull([5], 0) as [5],
isnull([6], 0) as [6],
isnull([7], 0) as [7],
isnull([8], 0) as [8],
isnull([9], 0) as [9],
isnull([10], 0) as [10] 
FROM 
(SELECT x=a, y=b, v= CASE WHEN IsNumeric(val)=0 THEN 0 ELSE IsNull(val, 0) 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
于 2012-04-26T00:13:49.017 に答える
1

このSOの質問で動的ピボット手法を使用しましたSqlで列と行を転置する簡単な方法は?

以下のように、ピボット レポートに 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);

DECLARE @Sql nvarchar(max), @PvtCols nvarchar(max), @PvtSelect nvarchar(max)

SELECT @PvtSelect = Stuff(
                (
                    SELECT ',IsNull(' + Quotename(B) + ',0) as ' + Quotename(B)
                    FROM MYTABLE mt
                    GROUP BY B
                    ORDER BY B
                    FOR XML PATH(''), TYPE
                    ).value('.', 'NVARCHAR(MAX)') 
                ,1,1,'')

SELECT @PvtCols = Stuff(
                    (
                    SELECT DISTINCT  ',' + Quotename(B) 
                    FROM MYTABLE mt
                    FOR XML PATH(''), TYPE
                    ).value('.', 'NVARCHAR(MAX)') 
                ,1,1,'')

SET @Sql =
'SELECT ' + @PvtSelect + ' 
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 (' + @PvtCols + ') 
) AS pvt
order by x'

EXEC(@SQL)
于 2013-11-22T16:47:14.470 に答える