在MS Access中对列标题进行分组

时间:2018-11-06 22:35:23

标签: ms-access ms-access-2013 ms-access-2016

好,所以我知道在Access Access女士报表或交叉表查询中对行进行分组,但是可以对列标题进行分组吗?

我有一个包含以下字段的查询

DaysWork ___ Source ___ Opening ___ CashIN ___ CashOUT

1/10/18 ____收银员1 ___ 1500 ________ 800 ______ 200

1/10/18 ____ Cashier2 ___ 130 _______ 100 ______ 900

2/10/18 _____收银员1__1500 ________ 500 ______ 250

2/10/18 _____收银员2__1300______150_______25

我可以让查询在报告或其他查询中以这种方式显示

DaysWork ___________ Cashier1_______________________Cashier2 ___________开幕___现金IN___现金OUT___开幕___现金IN___现金OUT 1/10/18 ______ 1500_______800______200_______1300_____100______900 2/10/18 ______ 1500 _______ 500 ______ 250 _______ 1300 _____ 150 _______ 25

1 个答案:

答案 0 :(得分:0)

不幸的是,SQL没有必需的功能。 如果出纳员姓名是固定的,并且出纳员数量已知,则可以使用以下查询:

SELECT DaysWork
    ,Max(IIf(Source <> 'Cashier1', NULL, Opening)) AS Opening_1
    ,Max(IIf(Source <> 'Cashier1', NULL, CashIN)) AS CashIN_1
    ,Max(IIf(Source <> 'Cashier1', NULL, CashOUT)) AS CashOUT_1
    ,Max(IIf(Source <> 'Cashier2', NULL, Opening)) AS Opening_2
    ,Max(IIf(Source <> 'Cashier2', NULL, CashIN)) AS CashIN_2
    ,Max(IIf(Source <> 'Cashier2', NULL, CashOUT)) AS CashOUT_2
FROM MyTable
GROUP BY DaysWork

您还可以创建查询并使用它代替MyTable,因为每个收银员都有一个对应的编号,并在报表查询中使用Source列中的这些编号。在这种情况下,您无需知道收银员姓名,但应限制不同收银员的数量

SELECT DaysWork
    ,Max(IIf(CashierNo <> 1, NULL, Opening)) AS Opening_1
    ,Max(IIf(CashierNo <> 1, NULL, CashIN)) AS CashIN_1
    ,Max(IIf(CashierNo <> 1, NULL, CashOUT)) AS CashOUT_1
    ,Max(IIf(CashierNo <> 2, NULL, Opening)) AS Opening_2
    ,Max(IIf(CashierNo <> 2, NULL, CashIN)) AS CashIN_2
    ,Max(IIf(CashierNo <> 2, NULL, CashOUT)) AS CashOUT_2
FROM qry_OnMyTable
GROUP BY DaysWork