简体   繁体   中英

Access: query with grouping criteria

I would like to group data of Table1 with a query that should give me Table2 as output.

Table2.FieldD is the count of Table1.FieldB where the grouping criteria is fulfilled.

The criteria is this: Group1 is made by all the record of Table1 where Table1.FieldA starts with "AK" or "KN". Group2 is the complementary of Group1 .

在此处输入图片说明

Use 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM