SQL不会从不同列中提取预期数据

时间:2017-12-11 17:45:16

标签: sql sql-server sql-server-2012 union-all

数据:

enter image description here

我需要从最新的每个月开始提取最新的非零数字。我第一次尝试这个:

Select Top(1) DfcMonth1 as [Value], year_f
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth1 <> 0
Order by year_f DESC

似乎工作,直到我做了一个联盟,然后它开始撤回不同年份的数字,所以我尝试了这个:

Select Top(1) DfcMonth1 as [Value], max(year_f) as year_f, 1 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth1 <> 0
GROUP BY DFCMonth1

这似乎也有效,但是当我为所有12个月做过工会时,有些月份会从错误的年份中撤回意外数据。

Select Top(1) DfcMonth1 as [Value], max(year_f) as year_f, 1 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth1 <> 0
GROUP BY DFCMonth1
UNION ALL
Select Top(1) DfcMonth2 as [Value],max(year_f) as year_f, 2 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth2 <> 0
GROUP BY DFCMonth2
UNION ALL
Select Top(1) DfcMonth3 as [Value], max(CAST(year_f as INT)) as year_f, 3 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth3 <> 0
GROUP BY DFCMonth3
UNION ALL
Select Top(1) DfcMonth4 as [Value],max(year_f) as year_f, 4 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth4 <> 0
GROUP BY DFCMonth4
UNION ALL
Select Top(1) DfcMonth5 as [Value],max(year_f) as year_f, 5 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth5 <> 0
GROUP BY DFCMonth5
UNION ALL
Select Top(1) DfcMonth6 as [Value],max(year_f) as year_f, 6 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth6 <> 0
GROUP BY DFCMonth6
UNION ALL
Select Top(1) DfcMonth7 as [Value],max(year_f) as year_f, 7 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth7 <> 0
GROUP BY DFCMonth7
UNION ALL
Select Top(1) DfcMonth8 as [Value],max(year_f) as year_f, 8 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth8 <> 0
GROUP BY DFCMonth8
UNION ALL
Select Top(1) DfcMonth9 as [Value],max(year_f) as year_f, 9 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth9 <> 0
GROUP BY DFCMonth9
UNION ALL
Select Top(1) DfcMonth10 as [Value],max(year_f) as year_f, 10 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth2 <> 0
GROUP BY DFCMonth10
UNION ALL
Select Top(1) DfcMonth11 as [Value],max(year_f) as year_f, 11 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth11 <> 0
GROUP BY DFCMonth11
UNION ALL
Select Top(1) DfcMonth12 as [Value],max(year_f) as year_f, 12 AS [month]
FROM dfcsthmonths_mst
WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth12 <> 0
GROUP BY DFCMonth12
Order by [month], year_f DESC

结果:

 Value      year_fmonth
 11202.00   2016    1
 10656.00   2016    2
 15130.00   2014    3
 15551.00   2016    4
 21518.00   2016    5
 18946.00   2012    6
 13616.00   2016    7
 17026.11   2016    8
 19704.00   2014    9
     0.00   2016    10
  5045.00   2015    11
  7077.00   2015    12

有任何想法或建议吗?

谢谢你, 罗恩

1 个答案:

答案 0 :(得分:0)

使用联盟时,Order by应用于整个结果集,而不是每个单独的部分。因此,每个部分中的Top(1)返回一个随机行。很确定GROUP BY也不需要。

它不漂亮,但试试这个:

h1 {
position: absolute;
top: 50%;
transform: translateY(-50%);
}