[英]Access: query with grouping criteria
I would like to group data of Table1
with a query that should give me Table2
as output. 我想用一个查询将Table1
数据分组,该查询应该给我Table2
作为输出。
Table2.FieldD
is the count of Table1.FieldB
where the grouping criteria is fulfilled. Table2.FieldD
是满足分组条件的Table1.FieldB
的计数。
The criteria is this: Group1
is made by all the record of Table1
where Table1.FieldA
starts with "AK" or "KN". 条件是这样的: Group1
由Table1
的所有记录组成,其中Table1.FieldA
以“ AK”或“ KN”开头。 Group2
is the complementary of Group1
. Group2
是Group1
的补充。
Use iif()
: 使用iif()
:
select iif(fielda like "ak*" or fielda like "kn*", "group1", "group2") as fieldc,
sum(fieldb)
from t
group by iif(fielda like "ak*" or fielda like "kn*", "group1", "group2")
SELECT 'Group1' AS FieldC
, SUM(FieldB) AS FieldD
FROM Table1
WHERE LEFT(FieldA,2) IN ('AK','KN')
UNION ALL SELECT 'Group2'
, SUM(FieldB)
FROM Table1
WHERE LEFT(FieldA,2) NOT IN ('AK','KN')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.