如何在SQL Server中显示年份中的月份明智数据而在输出中没有日期
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在SQL Server中显示年份中的月份明智数据而在输出中没有日期相关的知识,希望对你有一定的参考价值。
我正在尝试以这种方式获得数月的数据,因为我已经这样编写了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
所以我要做的是让我知道我必须在查询中更改的地方谁能提前帮助我..thanx
答案
您仅在一年中申请ORDER BY。为了获得预期的结果,您必须另一答案
None以上是关于如何在SQL Server中显示年份中的月份明智数据而在输出中没有日期的主要内容,如果未能解决你的问题,请参考以下文章
如何在包含单词、三个字母月份和两位数字年份的字符串中搜索月份和年份并将它们转换为 SQL 中的日期?