【问题标题】:Include column with field name on union query在联合查询中包含具有字段名称的列
【发布时间】:2019-07-05 16:13:04
【问题描述】:

我有一个可访问的表,其中包含我的产品的所有条件,我需要创建一个查询,将有关条件的所有信息放在一个列中。为此,我使用了联合查询。现在我想在它旁边的列上有一个条件的字段名称。

有可能得到这个结果吗?

抱歉,由于我缺乏知识,所以将这篇文章以糟糕的格式发布,感谢您提供的任何帮助。

我的表结构如下:

+------+--------------+--------------+--------------+-------------+
| 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   |
+------+------------+

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

+------+-----------+----------------+
| 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    |
+------+-----------+----------------+

【问题讨论】:

    标签: sql ms-access ms-access-2010


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 2010-09-06
      相关资源
      最近更新 更多