SQL Pivot Query中的PHP数组显示不同的数据

时间:2018-05-18 11:46:55

标签: php sql-server

我有一个SQL Server查询,它在SQL Server管理工具中显示以下结果:

   loc    L    M    S   XL
1  001    1    2    0    1
2  006    4    3    0    2
3  012    1    1    1    0
4  022    0    1    0    0

问题1:要在网页上显示此结果,我已在PHP中创建了一个数组,但它显示了我不需要的更多数据(在每个尺寸之后(L,M,S,XL) )有一个额外的数字,再次显示数量。)

问题2:是否可以按照我可以定义的方式对尺寸进行排序? 大小应该从小到大排序,如:S - M - L - XL,而不是按字母顺序排列。 (有时一个项目的大小以数字显示,从低到高分类很好,所以没问题。)

Array
(
    [0] => Array
        (
            [Location Code] => 001
            [0] => 001
            [L] => 1
            [1] => 1
            [M] => 2
            [2] => 2
            [S] => 0
            [3] => 0
            [XL] => 1
            [4] => 1
        )

    [1] => Array
        (
            [Location Code] => 006
            [0] => 006
            [L] => 4
            [1] => 4
            [M] => 3
            [2] => 3
            [S] => 0
            [3] => 0
            [XL] => 2
            [4] => 2
        )

    [2] => Array
        (
            [Location Code] => 012
            [0] => 012
            [L] => 1
            [1] => 1
            [M] => 1
            [2] => 1
            [S] => 1
            [3] => 1
            [XL] => 0
            [4] => 0
        )

    [3] => Array
        (
            [Location Code] => 022
            [0] => 022
            [L] => 0
            [1] => 0
            [M] => 1
            [2] => 1
            [S] => 0
            [3] => 0
            [XL] => 0
            [4] => 0
        )

)

SQL查询:

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
DECLARE @item varchar(max);
DECLARE @open varchar(max);
SET @item = 291557
SET @open = 1

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME([size])
FROM (SELECT DISTINCT [size] FROM [db].[dbo].[table] WHERE [Item] = @item AND [Open] = @open) AS data

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
  'SELECT [Loc], ' + @ColumnName + '
    FROM 
    (SELECT ROUND(CAST([Quantity] AS decimal (2,0)), 1) AS [QuantityRound],  [Loc], [size] FROM [db].[dbo].[table] 
    WHERE [Item] = ''' + @item + ''' AND [Open] = ''' + @open + ''') x
    PIVOT(SUM([QuantityRound])
          FOR [size] IN (' + @ColumnName + '))  AS PVTTable ORDER BY [Loc] + 0 ASC'
--Execute the Dynamic Pivot Query

EXEC sp_executesql @DynamicPivotQuery

PHP文件:

$ItemLedgerPivotQuery = "
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX);
DECLARE @ColumnName AS NVARCHAR(MAX);
DECLARE @SelectColumnNames AS NVARCHAR(MAX);

DECLARE @item varchar(max);
DECLARE @open varchar(max);
SET @item = '$ItemNo';
SET @open = 1;

SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME([size])
FROM (SELECT DISTINCT [size] FROM [db].[dbo].[table] WHERE [Item No_] = @item AND [Open] = @open) AS data

SELECT @SelectColumnNames
    = ISNULL(@SelectColumnNames + ',','')
    + 'ISNULL(' + QUOTENAME([size]) + ', 0) AS '
    + QUOTENAME([size])
    FROM (SELECT DISTINCT [size] FROM [db].[dbo].[table] WHERE [Item No_] = @item AND [Open] = @open) AS data

SET @DynamicPivotQuery = 
  'SELECT [loc], ' + @SelectColumnNames + '
    FROM 
    (SELECT ROUND(CAST([Remaining Quantity] AS decimal (2,0)), 1) AS [quantity],  [loc], [size] FROM [db].[dbo].[table] 
    WHERE [Item No_] = ''' + @item + ''' AND [Open] = ''' + @open + ''') x
    PIVOT(SUM([quantity])
          FOR [size] IN (' + @ColumnName + '))  AS PVTTable ORDER BY [loc] + 0 ASC'

EXEC sp_executesql @DynamicPivotQuery";

$sth = $dbh->prepare($ItemLedgerPivotQuery);
$sth->execute();
$result = $sth->fetchAll();

echo '<table border="1">';
echo '<tr>';
foreach( $result as $key => $value )
{
    if( is_array($value) ) 
    { 
        foreach($value as $key => $column) {
            echo '  <th colspan="1" style="width: 30px">'.$key.'</th>';
        }
    }
    else 
    {
        echo '<th colspan="1">Nothing</th>';
    }
    break;
}
echo '</tr>';
//Data
foreach( $result as $key => $value )
{
echo '<tr>';
    if( is_array($value) ) 
    { 
        foreach($value as $key => $column) {
            echo '<td>'.$column.'</td>';
        }
    }
    else 
    {
        echo '<td>'.$value.'</td>';
    }
echo '</tr>';
}

1 个答案:

答案 0 :(得分:0)

您应该使用FETCH_ASSOC指定您希望PDO返回关联数组

$sth->fetchAll(PDO::FETCH_ASSOC);

您应该能够使用usort以自定义方式进行排序 https://secure.php.net/manual/en/function.usort.php