在联合查询中包含带有字段名的列

Include column with field name on union query

我有一个 table 访问权限,它具有我的产品的所有标准,需要创建一个查询,将有关标准的所有信息放在一个列中。为此,我使用了联合查询。现在我想在它旁边的列上有一个标准的字段名称。

有可能得到这个结果吗?

抱歉,我缺乏知识,所以 post 的格式很糟糕,感谢您能给我的任何帮助。

我的 table 具有以下结构:

+------+--------------+--------------+--------------+-------------+
| P/N  |  Criteria 1  |  Criteria 2  |  Criteria 3  |  Criteria 4 |
+------+--------------+--------------+--------------+-------------+
| 12A  |  blue        |  plastic     |  Box         |  5mm        |
| 20C  |  neutral     |  metal       |  Pack        |  120mm      |
+------+--------------+--------------+--------------+-------------+

我在 Access 中有一个联合查询,结果如下:

+------+------------+
| P/N  |  Criteria  |
+------+------------+
| 12A  |    blue    |
| 12A  |    plastic |
| 12A  |    Box     |
| 12A  |    5mm     |
| 20C  |    neutral |
| 20C  |    metal   |
| 20C  |    pack    |
| 20C  |    120mm   |
+------+------------+

我想要我想要的最终结果是table这种格式:

+------+-----------+----------------+
| P/N  |  Criteria |  Criteria Name |
+------+-----------+----------------+
| 12A  |  blue     |  Criteria 1    |
| 12A  |  plastic  |  Criteria 2    |
| 12A  |  Box      |  Criteria 3    |
| 12A  |  5mm      |  Criteria 4    |
| 20C  |  neutral  |  Criteria 1    |
| 20C  |  metal    |  Criteria 2    |
| 20C  |  pack     |  Criteria 3    |
| 20C  |  120mm    |  Criteria 4    |
+------+-----------+----------------+

假设您的联合查询看起来像这样 -

select t.[p/n], t.[criteria 1] as criteria
from yourtable t
union all
select t.[p/n], t.[criteria 2] as criteria
from yourtable t
union all
select t.[p/n], t.[criteria 3] as criteria
from yourtable t
union all
select t.[p/n], t.[criteria 4] as criteria
from yourtable t

然后你可以这样添加字段名:

select t.[p/n], t.[criteria 1] as criteria, 'Criteria 1' as [Criteria Name]
from yourtable t
union all
select t.[p/n], t.[criteria 2] as criteria, 'Criteria 2' as [Criteria Name]
from yourtable t
union all
select t.[p/n], t.[criteria 3] as criteria, 'Criteria 3' as [Criteria Name]
from yourtable t
union all
select t.[p/n], t.[criteria 4] as criteria, 'Criteria 4' as [Criteria Name]
from yourtable t