【问题标题】:Return a crosstab to its original table format将交叉表恢复为其原始表格格式
【发布时间】:2016-11-16 19:41:54
【问题描述】:

我经常以“交叉表”的方式从其他部门接收数据。

color_|_person1_|_person2_|_person3___|
red   | yes     | yes     | no
Blue  | no      | no      | yes
Green | no      | yes     | no

(这只是一个例子)

实际上,行 (Person[1-2-3]) 是另一个表中的 ID。

所以它应该看起来像这样:

IDPerson__|_Color__|_Activated_|
person1   | red    | yes
person1   | Blue   | yes
person1   | Green  | no
etc...

这些数据在 Excel 和 Access 2003 上, 有没有办法自动“取消”表格?

从常规表制作交叉表很简单,但是我还没有找到任何方法来做相反的事情。 :/

感谢您的帮助! ^^

【问题讨论】:

    标签: sql ms-access crosstab


    【解决方案1】:

    MS Access 没有将数据列转换为行的 UNPIVOT 函数,但您可以使用 UNION ALL 查询:

    SELECT 'person1' as IDPerson, color, person1 as activated
    FROM yourtable
    UNION ALL
    SELECT 'person2' as IDPerson, color, person2 as activated
    FROM yourtable
    UNION ALL
    SELECT 'person3' as IDPerson, color, person3 as activated
    FROM yourtable
    

    查看Demo(SQL Server 演示)查看工作版本。

    【讨论】:

    • 好主意,谢谢...只要我们不需要做 50 个联合 ^^'(通常是这种情况),就可以很好地工作 ----(我会离开线程打开一点以防万一有人会带来像自动化工具之类的东西,如果没有这样的东西,你的答案将是被接受的^^,)
    • 如果你想做一些通用的事情,那么你可能不得不考虑使用 VBA 来反透视数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多