如何在SQL Server中显示年份中的月份明智数据而在输出中没有日期

问题描述 投票:0回答:2

我正在尝试以这种方式获得数月的数据,因为我已经这样编写了Query,但没有得到我想要的结果

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 

并获得此结果Got This REsult

但我实际上想要这样(明智和明智的做法)Actual what i want

所以我要做的是让我知道我必须在查询中更改的地方谁能提前帮助我..thanx

sql-server-2014
2个回答
0
投票
您仅在一年中申请ORDER BY。为了获得预期的结果,您必须

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.