SQL Jet访问,插入列和排序依据

时间:2018-09-17 14:14:54

标签: sql ms-access jet

我通过excel使用Microsoft.ACE.OLEDB.12.0连接在工作簿中进行内部查询。

如何在查询中插入具有年份的列Year并对其进行排序,以使它们不在一起。所以他们会一个接一个地重复吗?

我已经添加了Year列,但是现在是排序问题。

这是我当前的查询

select [Data Type], [Currency], [Book Name], [1987] as Value
from [stage2$]
UNION ALL
select [Data Type], [Currency], [Book Name], [1990]
from [stage2$]

当前数据:

+--------------------------------+----------+-------+---------------+
|           Data Type            | Currency | Name  |   value       |
+--------------------------------+----------+-------+--------------
| Missing Non Historical Results | BSD      | Carib | (279)         |
| Missing Non Historical Results | BSD      | Carib |   18          |
| Missing Non Historical Results | BSD      | Carib |   898         |
| Missing Non Historical Results | BSD      | Carib |  (50)         |
+--------------------------------+----------+-------+---------------+

预期结果:

+--------------------------------+----------+-------+---------------+---------+
|           Data Type            | Currency | Name  | Year          |  value  |
+--------------------------------+----------+-------+---------------+---------+
| Missing Non Historical Results | BSD      | Carib |          1987 |  (279)  |
| Missing Non Historical Results | BSD      | Carib |          1990 |  898    |
| Missing Non Historical Results | BSD      | Carib |          1987 |  18     |
| Missing Non Historical Results | BSD      | Carib |          1990 |  (50)   |
+--------------------------------+----------+-------+---------------+---------+

1 个答案:

答案 0 :(得分:2)

您可以在select语句中添加另一列:

select [Data Type], [Currency], [Book Name], '1987' as Year, [1987] as Value
from [stage2$]
UNION ALL
select [Data Type], [Currency], [Book Name], '1990' as Year, [1990]
from [stage2$]
order by Year