将列转换为行并获取 Access 中复选框的总和
Convert columns to rows and get the sum of checkboxes in Access
我有一个 Access 数据库,其中一个 table 有复选框。我需要得到复选框的总和,但是我想将复选框列转换为行而不是列。
这是它在 table 中的显示方式示例:
Name DND Other CAP SAP DAP EXP
Person One -1 -1 NULL NULL -1 -1
Person Two NULL -1 -1 -1 NULL NULL
Person Three NULL -1 -1 NULL NULL NUll
下面是我希望报表显示数据的方式:
Discharge Count
DND 1
Other 3
CAP 2
SAP 1
DAP 1
EXP 1
我尝试使用 Case 语句,但结果不正确
SELECT 'DND' AS Discharge, COUNT(*) AS [Count] FROM tbl WHERE DND = -1
UNION ALL
SELECT 'Other' AS Discharge, COUNT(*) AS [Count] FROM tbl WHERE Other = -1
UNION ALL
等等
我认为没有更简单的方法。
这是航空代码,不确定您是否需要GROUP BY
条款。
比较How to simulate UNPIVOT in Access?
使用 union query 并检查 Null:
Select
T.Discharge, Abs(Sum(T.[Count])) As DischargeCount
From
(Select "DND" As Discharge, [DND] Is Not Null As [Count] From YourTable
Union ALL
Select "Other" As Discharge, [Other] Is Not Null As [Count] From YourTable
Union ALL
Select "CAP" As Discharge, [CAP] Is Not Null As [Count] From YourTable
Union ALL
Select "SAP" As Discharge, [SAP] Is Not Null As [Count] From YourTable
Union ALL
Select "DAP" As Discharge, [DAP] Is Not Null As [Count] From YourTable
Union ALL
Select "EXP" As Discharge, [EXP] Is Not Null As [Count] From YourTable) As T
Group By
T.Discharge
我有一个 Access 数据库,其中一个 table 有复选框。我需要得到复选框的总和,但是我想将复选框列转换为行而不是列。
这是它在 table 中的显示方式示例:
Name DND Other CAP SAP DAP EXP
Person One -1 -1 NULL NULL -1 -1
Person Two NULL -1 -1 -1 NULL NULL
Person Three NULL -1 -1 NULL NULL NUll
下面是我希望报表显示数据的方式:
Discharge Count
DND 1
Other 3
CAP 2
SAP 1
DAP 1
EXP 1
我尝试使用 Case 语句,但结果不正确
SELECT 'DND' AS Discharge, COUNT(*) AS [Count] FROM tbl WHERE DND = -1
UNION ALL
SELECT 'Other' AS Discharge, COUNT(*) AS [Count] FROM tbl WHERE Other = -1
UNION ALL
等等
我认为没有更简单的方法。
这是航空代码,不确定您是否需要GROUP BY
条款。
比较How to simulate UNPIVOT in Access?
使用 union query 并检查 Null:
Select
T.Discharge, Abs(Sum(T.[Count])) As DischargeCount
From
(Select "DND" As Discharge, [DND] Is Not Null As [Count] From YourTable
Union ALL
Select "Other" As Discharge, [Other] Is Not Null As [Count] From YourTable
Union ALL
Select "CAP" As Discharge, [CAP] Is Not Null As [Count] From YourTable
Union ALL
Select "SAP" As Discharge, [SAP] Is Not Null As [Count] From YourTable
Union ALL
Select "DAP" As Discharge, [DAP] Is Not Null As [Count] From YourTable
Union ALL
Select "EXP" As Discharge, [EXP] Is Not Null As [Count] From YourTable) As T
Group By
T.Discharge