简体   繁体   中英

MS Access Query For Combining rows

I have a query which returns data in following format:

xxx yyy     count
-------------------------------
a   cat1    23
a   cat2    34
a   cat3    12
b   cat1    34
b   cat2    1
b   cat3    2
c   cat1    34
c   cat2    123
c   cat3    34
d   cat1    34
d   cat2    12
d   cat3    34

I need to modify my query in such a way that i need to categorize two values of yyy column into single and combine their count

xxx yyy         count
-------------------------------
a   cat1        23
a   cat2 & cat3 36
b   cat1        34
b   cat2 & cat3 3
c   cat1        34
c   cat2 & cat3 157
d   cat1        34
d   cat2 & cat3 36

Please suggest if you have any ideas!

Assuming you always want cat1 broken out and there are only 2 other cats...

SELECT xxx, yyy, COUNT(*) AS count
FROM table
WHERE yyy = 'cat1'
GROUP BY xxx, yyy
UNION
SELECT xxx, 'cat2 & cat3' AS yyy, COUNT(*) AS count
FROM table
WHERE yyy <> 'cat1'
GROUP BY xxx

If you there additional cats and you only want 2 and 3, change WHERE yyy <> 'cat1' to WHERE yyy IN ('cat2','cat3')

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