SQL 没有从不同的列中提取预期的数据

SQL not pulling expected data from different columns

数据:

我需要首​​先从最近一年的每个月中提取最新的非零数字。我第一次尝试这个:

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

似乎一直有效,直到我做了一个 Union 然后它开始从不同年份撤回数字所以我试过这个:

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

有什么想法或建议吗?

谢谢, 罗恩

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

这不是很漂亮,但试试这个:

SELECT * 
FROM 
   (
   Select Top(1) DfcMonth1 as [Value], year_f, 1 AS [month]
   FROM dfcsthmonths_mst
   WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth1 <> 0
   ORDER BY year_f DESC
   ) x 

UNION ALL

SELECT *
FROM
    (
    Select Top(1) DfcMonth2 as [Value], year_f, 2 AS [month]
    FROM dfcsthmonths_mst
    WHERE (item = N'A602BK') AND (type = 'B') AND DfcMonth2 <> 0
    ORDER BY year_f DESC
    ) x 

UNION ALL

    ....

ORDER BY [Month]