如何在 SQL 服务器中显示年份中的月份明智数据,输出中没有日期

How to Display Month Wise Data from Year in SQL Server without date in output

我试图在几个月内明智地获取数据,因为我已经编写了这样的查询,但没有得到我想要的结果

SET dateformat dmy 

SELECT * 
FROM   (SELECT 'Apr'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '04' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'May'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '05' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Jun'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '06' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Jul'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '07' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Aug'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '08' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Sep'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '09' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Oct'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '10' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Nov'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '11' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Dec'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '12' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Jan'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2016'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '01' 
               AND Year(dt) = '2016' 
        UNION 
        SELECT 'Feb'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2016'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '02' 
               AND Year(dt) = '2016' 
        UNION 
        SELECT 'Mar'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2016'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '03' 
               AND Year(dt) = '2016') AS tbl 
ORDER  BY year 

得到了这个结果

但我真的想要这样(年月明智)

所以我必须为此做些什么让我知道我必须在查询中更改的地方
谁能帮我..提前谢谢

您仅在年份应用 ORDER BY。为了达到预期的结果,您必须

  1. CONVERT(INT, MONTH(dt)) AS Month_Number
  2. 的子查询中添加月份数字
  3. ORDER BY 子句中添加 Month_Number 作为 ORDER BY year, Month_Number
SELECT * 
FROM   (SELECT 'Apr'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year',
               ------------------------------------------------
               CONVERT(INT, MONTH(dt)) AS Month_Number 
               ------------------------------------------------
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '04' 
               AND Year(dt) = '2015'
        GROUP BY MONTH(dt)

        UNION 
        SELECT 'May'                                 AS xData, 
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year',
               ------------------------------------------------
               CONVERT(INT, MONTH(dt)) AS Month_Number 
               ------------------------------------------------
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '05' 
               AND Year(dt) = '2015'
        GROUP BY MONTH(dt)


        -- Remaining Code --

) AS tbl 
ORDER  BY year, Month_Number
SET dateformat dmy 

SELECT 
xData,
asswt,
waxwt,
corewt
'Year'
FROM   (SELECT 'Apr'                                 AS xData, 
                 1                                   AS xNum,
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '04' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'May'                                 AS xData, 
                 2                                   AS xNum,
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '05' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Jun'                                 AS xData, 
                 3                                   AS xNum,
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '06' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Jul'                                 AS xData, 
                 4                                   AS xNum,
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '07' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Aug'                                 AS xData, 
                 5                                   AS xNum,
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '08' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Sep'                                 AS xData, 
                6                                    AS xNum,
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '09' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Oct'                                 AS xData, 
                 7                                   AS xNum,
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '10' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Nov'                                 AS xData, 
                 8                                   AS xNum,
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '11' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Dec'                                 AS xData, 
                9                                    AS xNum,
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2015'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '12' 
               AND Year(dt) = '2015' 
        UNION 
        SELECT 'Jan'                                 AS xData, 
               10                                    AS xNum,
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2016'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '01' 
               AND Year(dt) = '2016' 
        UNION 
        SELECT 'Feb'                                 AS xData, 
               11                                    AS xNum,
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2016'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '02' 
               AND Year(dt) = '2016' 
        UNION 
        SELECT 'Mar'                                 AS xData,
               12                                    AS xNum,       
               Sum(Isnull(( qty * assemblywt ), 0))  AS asswt, 
               Sum(Isnull(( qty * [waxwt/pcs] ), 0)) AS waxwt, 
               Sum(Isnull(( qty * corewt ), 0))      AS corewt, 
               '2016'                                AS 'Year' 
        FROM   tdcwaxweight 
        WHERE  Month(dt) = '03' 
               AND Year(dt) = '2016') AS tbl 
ORDER  BY year and xNum