开发者

sql subqueries, grouping and a bit of math

开发者 https://www.devze.com 2023-01-28 05:48 出处:网络
this counts all items in B and groups on A: SELECT A, Count(*) AS [Count All] FROM MyTable GROUP BY A; this counts all 1s in B and groups on A:

this counts all items in B and groups on A:

SELECT A, Count(*) AS [Count All]
FROM MyTable 
GROUP BY A;

this counts all 1s in B and groups on A:

SELECT A, Count(*) AS [Count Ones]
FROM My开发者_C百科Table
WHERE
     MyTable.[B]='1' 
GROUP BY A;

How do I put both columns (all and 1s) and additionally, how could I show percentage in another row

table columns:

[A] [COUNT ALL] [COUNT ONES] [ONES/ALL]


Since its MS-ACCESS you can use iif instead of case

SELECT 
     Table1.A, 
     Sum(IIf([B]=1,1,0)) AS Count1, 
     Count(Table1.A) AS total, 
     (Sum(IIf([B]=1,1,0))/ Count(Table1.A)) * 100 AS percentofones
FROM 
     Table1
GROUP BY 
     Table1.A;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号