How to find percentage in SQL from list of zeros and ones? - sql

I have result set like -
id achieved
1 0
2 1
3 1
4 0
5 0
The Percentage should be 2/5 i.e. 40 %. How can I write a SQL Query to achieve something like this ? I would prefer not to use and nested select as the actual query is already doing quite a bit. Thanks !

select avg(achieved) from ...
Note that you will have to use a group by function to include categories:
select gender, avg(achieved) from ... group by gender

Related

How to calculate a progressive sum within an Access query

I have a query, let's call it qry_01, that produces a set of data similar to this:
ID N CN Sum
1 4 0 0
2 3 3 3
5 4 4 7
8 3 3 10
The values shown in this query actually come from a chain of queries and from a bunch of different tables.
The corrected value CN is calculated within the query, and counts N if the ID is not 1, and 0 if it is 1.
The Sum is the value I want to calculate by progressively summing up the CN values.
I tried to use DSUM, but I came out with nothing.
Can anyone please help me?
You could use a correlated subquery in the following way:
select t.id, t.n, t.cn, (select sum(u.cn) from qry_01 u where u.id <= t.id) as [sum]
from qry_01 t

MS Access SQL - SELECT COUNT

I have a table with some entries and i need help with an sql command.
The Table consists round about 50 entries with 6 columns.
Table: tbl-planung
ID SID STATUS ... ... ...
1 MDT Yes ... ... ...
2 ABC Yes ... ... ...
3 BLA NO ... ... ...
I need a command which counts the total amount of entries in that table
+ the amount of entries with STATUS = Yes
Like:
TOTAL DONE
50 2
But my command returns
TOTAL DONE
50 50
SQL Command
SELECT Count([tbl-planung].Abgeschlossen) AS Total,
Count([tbl-planung].Abgeschlossen) AS Done
FROM [tbl-planung]
WHERE ((([tbl-planung].Abgeschlossen)=Yes));
Use a conditional SUM
SELECT Count(Abgeschlossen) AS Total,
sum(iif(Abgeschlossen = Yes, 1, 0)) AS Done
FROM [tbl-planung]
Use SUM instead, without WHERE:
SELECT Count([tbl-planung].*) AS Total,
-Sum([tbl-planung].Abgeschlossen) AS Done
FROM [tbl-planung]

Sum of a record source in access forms

I have a query that has the following display:
type $ value
1 5
1 3
1 2
2 1
2 3
This query is the record source of my form I would like the form to display the data as so
type Sum of value
1 10
2 4
is there a vba or setting way to achieve that ?
The proper way to do that is to use an aggregation function in your query, in your case it would be sum. Your query will look like this.
SELECT Type, SUM(value) As Total FROM table GROUP BY Type;
if you use the graphical query builder then you will have to follow this

SQL Nested Aggregation

I am working with SQL Language.
I have a table named parta. I want to count the fields 40b1 and 40b2 and find the sum of this. My query is here.
select
count(40b1) as 40b1,
count(40b2) as 40b2,
sum(count(40b1) + count(40b2) ) as sum,
code/100 as code
from parta
where 40b1=true and mandays>=1000
group by code/100 ;
Expected output
40b1 40b2 sum code verticalsum
5 5 10 20 7
2 2 4 21 7
How it done? Please help.
For getting this verticalsum column, what query can I use?
You don't need to SUM() the COUNT()'s. Just add them together.
select count(40b1) as 40b1,
count(40b2) as 40b2,
count(40b1) + count(40b2) as sum,
code/100 as code
from parta
where 40b1=true and mandays>=1000
group by code/100 ;

removing duplicates in ms access

please tell me how to write this query
i have an access table
number
2
2
1
2
2
1
1
3
2
i want a query that gives
number count
2 5
1 3
3 1
any help appreciated
something like...
SELECT number, count(number) AS count
FROM table
GROUP BY number
It's a bad idea to have a column named number since it is a reserved keyword.
You probably want something like
select number_, count(*) as count from ... group by number_