TSQL组错误

时间:2014-06-02 17:20:34

标签: sql sql-server xml tsql

我有一个简单的查询,它解析了一些记录/ XML并为我做了一些数学运算。然后它返回带有我需要的数字的XML输出。

SELECT  A.[reimbursementDate],
                MONTH(A.[reimbursementDate]) as reimbursementMonth,
                YEAR(A.[reimbursementDate]) as reimbursementYear,
            (
                SELECT  SUM(ParamValues.x2.value('grandTotal[1]', 'INT')) as grandTotal,
                        SUM(ParamValues.x2.value('booksTotal[1]', 'INT')) as totalBooks,
                        SUM(ParamValues.x2.value('tuitionTotal[1]', 'INT')) as tuitionTotal
                FROM    A.[classXML].nodes('/courses') AS ParamValues(x2)
                FOR    XML PATH (''), TYPE, ELEMENTS
            )
        FROM   tuitionSubmissions as A
        WHERE A.[status] = 'Approved' AND YEAR(A.[reimbursementDate]) = YEAR(GETDATE())
        GROUP BY A.[reimbursementDate]
        FOR    XML PATH ('dataSet'), TYPE, ELEMENTS, ROOT ('root');

下面你将看到它产生的输出:

            <root>
              <dataSet>
                <reimbursementMonth>6</reimbursementMonth>
                <reimbursementYear>2014</reimbursementYear>
                <grandTuition>1158</grandTuition>
                <totalBooks>863</totalBooks>
                <tuitionTotal>295</tuitionTotal>
              </dataSet>
              <dataSet>
                <reimbursementMonth>8</reimbursementMonth>
                <reimbursementYear>2014</reimbursementYear>
                <grandTuition>253</grandTuition>
                <totalBooks>143</totalBooks>
                <tuitionTotal>110</tuitionTotal>
              </dataSet>
              <dataSet>
                <reimbursementMonth>7</reimbursementMonth>
                <reimbursementYear>2014</reimbursementYear>
                <grandTuition>1488</grandTuition>
                <totalBooks>842</totalBooks>
                <tuitionTotal>986</tuitionTotal>
              </dataSet>
              <dataSet>
                <reimbursementMonth>6</reimbursementMonth>
                <reimbursementYear>2014</reimbursementYear>
                <grandTuition>616</grandTuition>
                <totalBooks>425</totalBooks>
                <tuitionTotal>191</tuitionTotal>
              </dataSet>
            </root>

我的问题是我希望将月份/年份相同的数字组合在一起。所以第一个区块和最后一个区块是第6个月,所以这些区域应该与它们的总数合并为一个。我可以从查询中轻松完成此操作吗?

1 个答案:

答案 0 :(得分:0)

如果你想按月份和年份分组,你必须这样做吗

SELECT  A.[reimbursementDate],
            MONTH(A.[reimbursementDate]) as reimbursementMonth,
            YEAR(A.[reimbursementDate]) as reimbursementYear,
        (
            SELECT  SUM(ParamValues.x2.value('grandTotal[1]', 'INT')) as grandTotal,
                    SUM(ParamValues.x2.value('booksTotal[1]', 'INT')) as totalBooks,
                    SUM(ParamValues.x2.value('tuitionTotal[1]', 'INT')) as tuitionTotal
            FROM    A.[classXML].nodes('/courses') AS ParamValues(x2)
            FOR    XML PATH (''), TYPE, ELEMENTS
        )
    FROM   tuitionSubmissions as A
    WHERE A.[status] = 'Approved' AND YEAR(A.[reimbursementDate]) = YEAR(GETDATE())
    GROUP BY A.[reimbursementDate]
            MONTH(A.[reimbursementDate])
            YEAR(A.[reimbursementDate])
    FOR    XML PATH ('dataSet'), TYPE, ELEMENTS, ROOT ('root');

或者,如果您只想按reimbursementDate

分组
SELECT
  [reimbursementDate],
   MONTH([reimbursementDate]) as reimbursementMonth,
   YEAR([reimbursementDate]) as reimbursementYear,
   XMLCol
FROM
(
    SELECT  A.[reimbursementDate],
        (
            SELECT  SUM(ParamValues.x2.value('grandTotal[1]', 'INT')) as grandTotal,
                    SUM(ParamValues.x2.value('booksTotal[1]', 'INT')) as totalBooks,
                    SUM(ParamValues.x2.value('tuitionTotal[1]', 'INT')) as tuitionTotal
            FROM    A.[classXML].nodes('/courses') AS ParamValues(x2)
            FOR    XML PATH (''), TYPE, ELEMENTS
        ) as XMLCol
    FROM   tuitionSubmissions as A
    WHERE A.[status] = 'Approved' AND YEAR(A.[reimbursementDate]) = YEAR(GETDATE())
    GROUP BY A.[reimbursementDate]
            MONTH(A.[reimbursementDate])
            YEAR(A.[reimbursementDate])
    FOR    XML PATH ('dataSet'), TYPE, ELEMENTS, ROOT ('root')
) T