0

我无法将 SQL Select 语句输出到我的 XML 以用于我的图表

我有这些结果

December 2011 470 FRESENIUS

January 2012 434 FRESENIUS

February 2012 278 FRESENIUS

February 2012 2 STORESID

我需要像这样输出它们,这样我就可以循环我的代码并生成 XML

Month Year FRESNIUS STORESID

December 2011 470 0

January 2012 434 0

February 2012 278 2

请注意我没有包括标题,这些都是列结果。而且,STORESID 和 FRESNIUS 不是静态值。存在多个存储器,因此我还需要动态扩展列数。

我有点需要输出第二个以通过我已经拥有的 PHP 正确生成 XML。或者有没有其他合适的方法。

谢谢。

在下面查询我正在谈论的输出

选择  
        DATENAME(month, orderdate) + ' ' + CAST(Year(orderdate) AS VARCHAR(4)) AS 'Month Year' ,
        count(*) '订单数',
        存储密钥
来自订单
通过...分组
        存储密钥,
        DATENAME(month, orderdate) + ' ' + CAST(Year(orderdate) AS VARCHAR(4)),
        CAST(Year(orderdate) AS VARCHAR(4)) + RIGHT('0' + CAST(Month(orderdate) AS VARCHAR(2)),2)
订购方式    
        存储密钥,
        CAST(Year(orderdate) AS VARCHAR(4)) + RIGHT('0' + CAST(Month(orderdate) AS VARCHAR(2)),2)

        
4

3 回答 3

1

您可以将表连接到自身。如果我对列名做出假设,假设 FRESNIUS 或 STORESID 可能不存在,并使用公用表表达式(未经测试!):

WITH date_fresnius AS (
    SELECT [Month Year], FRESNIUS
    FROM original_table
)
, date_storesid AS (
    SELECT [Month Year], STORESID
    FROM original_table
)
SELECT
      ISNULL( f.[Month Year], s.[Month Year]) AS [Month Year]
    , ISNULL( f.FRESNIUS, 0 ) AS FRESNIUS
    , ISNULL( s.STORESID, 0 ) AS STORESID
FROM
    date_fresnius f
    FULL OUTER JOIN date_storesid s ON (f.[Month Year] = s.[Month Year])

如果您始终拥有 FRESNIUS 记录,则可以改用 LEFT JOIN。

于 2012-04-04T09:04:06.290 回答
1

我不是 100% 确定您需要的 XML 输出,但您可以利用SQL-Servers FOR XML 子句和 PIVOT 函数。这应该让你开始:

CREATE TABLE #Test (GraphDate DATETIME, Value INT, Type VARCHAR(50))
INSERT #Test VALUES 
    ('01/12/2011', 470, 'FRESENIUS'),
    ('01/01/2012', 434, 'FRESENIUS'),
    ('01/12/2012', 278, 'FRESENIUS'),
    ('01/02/2012', 2, 'STORESID')

SELECT  DATEPART(YEAR, GraphDate) [Year], 
        DATENAME(MONTH, GraphDate) [Month], 
        ISNULL(FRESENIUS, 0) [FRESENIUS],
        ISNULL(STORESID, 0) [STORESID]
FROM    #Test
        PIVOT
        (   SUM(Value)
            FOR Type IN ([FRESENIUS], [STORESID])
        ) PivotTable
FOR XML PATH('row'), ROOT

DROP TABLE #Test

编辑

以下是您根据您给出的查询要求的基本查询。

SELECT  DATENAME(MONTH, OrderDate)  + ' ' + DATENAME(YEAR, OrderDate) [MonthYear], 
        ISNULL(FRESENIUS, 0) [FRESENIUS],
        ISNULL(STORESID, 0) [STORESID]
FROM    Orders
        PIVOT
        (   COUNT(StorerKey)
            FOR StorerKey IN ([FRESENIUS], [STORESID])
        ) PivotTable
ORDER BY  DATEPART(YEAR, OrderDate), DATEPART(MONTH, OrderDate)

编辑 2

我认为PIVOTSQL-Server 2000 不支持。您需要使用聚合函数:

SELECT  DATENAME(MONTH, OrderDate)  + ' ' + DATENAME(YEAR, OrderDate) [MonthYear], 
        COUNT(CASE WHEN StorerKey = 'FRESENIUS' THEN 1 END) [FRESENIUS],
        COUNT(CASE WHEN StorerKey = 'STORESID' THEN 1 END) [STORESID]
FROM    Orders
GROUP BY DATEPART(YEAR, OrderDate), DATEPART(MONTH, OrderDate), DATENAME(YEAR, OrderDate), DATENAME(MONTH, OrderDate)
ORDER BY  DATEPART(YEAR, OrderDate), DATEPART(MONTH, OrderDate)

我认为某些FOR XML功能仍然可用,因此您仍然可以直接从 SQL 输出 XML。


编辑 3

DECLARE @SQL NVARCHAR(2000)
SELECT  @SQL = ISNULL(@SQL, '') + ', COUNT(CASE WHEN StorerKey = ''' + StorerKey + ''' THEN 1 END) [' + StorerKey + ']'
FROM    (   SELECT  DISTINCT StorerKey
            FROM    Orders
        ) Keys


SET @SQL = 'SELECT  DATENAME(MONTH, OrderDate) + '' '' + DATENAME(YEAR, OrderDate) [MonthYear]' + @SQL + 
            ' FROM    Orders
            GROUP BY DATEPART(YEAR, OrderDate), DATEPART(MONTH, OrderDate), DATENAME(YEAR, OrderDate), DATENAME(MONTH, OrderDate)
            ORDER BY  DATEPART(YEAR, OrderDate), DATEPART(MONTH, OrderDate)'

EXECUTE SP_EXECUTESQL @SQL
于 2012-04-04T09:08:35.663 回答
1

根据您的查询:

SELECT
    DATENAME(MONTH, orderdate) + ' ' + CAST(YEAR(orderdate) AS VARCHAR(4)) AS [Month Year]
    COUNT(CASE storerkey WHEN 'FRESNIUS' THEN 1 END) AS FRESNIUS,
    COUNT(CASE storerkey WHEN 'STORESID' THEN 1 END) AS STORESID
FROM orders
GROUP BY YEAR(orderdate), MONTH(orderdate), DATENAME(MONTH, orderdate)
ORDER BY YEAR(orderdate), MONTH(orderdate)

输出:

Month Year      FRESNIUS   STORESID
December 2011        470          0
January 2012         434          0
February 2012        278          2
于 2012-04-04T09:28:27.863 回答