Simple SQL that I am stuck on - sql

What percent of customers are reward members?
ID Reward Member
1 y
2 y
3 n
4 y
5 n

Count how many are members, divide by total number of customers and multiply by 100.
SELECT sum(case when RewardMember = 'y' then 1 else 0 end)*100.0/count(*) as percentage
FROM Customers

You can use COUNT():
select
1.0 *
count(case when reward_member = 'y' then 1 else 0 end)
/ count(*)
from t

I would use conditional aggregation:
select avg( case when rewardmember = 'y' then 1.0 else 0 end) as ratio
from t;
Some databases allow shorter syntax, such as:
select avg( rewardmember = 'y' )
from t;
or:
select avg( (rewardmember = 'y')::int )
from t;

Related

Group by and divide

I have the table below on sql of accounting accounts (A is a cost account and B a headcount ) , and cc as cost center . So what i want to do is divide the amount of account A over account B for each cc
Account
cc
Amount
A
x
1
A
y
2
B
z
4
B
y
1
A
z
1
B
x
2
So the result would be :
Account
cc
Amount
A
x
1 /2
A
y
2
B
z
0
B
y
0
A
z
1 /4
B
x
0
I was thinking about a group by but Im a very beginner in sql and don't know how to use it thanks in advance for your help!
For the results you specify, you can use window functions:
select t.*,
(case when account = 'A'
then amount * 1.0 / sum(case when account = 'B' then amount end) over (partition by cc)
else 0
end) as ratio
from t;
I would just use a regular group by with some cases:
with cc_amts as (
select cc,
sum(case when account = 'A' then amount else 0 end) amt_a,
sum(case when account = 'B' then amount else 0 end) amt_b
from t
group by cc)
select cc, amt_a / amt_b
from cc_amts;

How to filter and count percentages and count in SQL

After a query I end up with a table like this
tbl_1:
category type count
cpg b 1
auto c 1
cpg c 1
auto v 1
I would like to calculate the total_count by category, and the percentage of count that is not (type<>'v') also by category in SQL.
Any ideas how I could do that ?
The resulting table should look like this:
category total_count percentage
cpg 2 1
auto 2 0.5
Is this what you want?
select category, sum(count),
sum(case when type <> 'v' then count else 0.0 end) / sum(count)
from t
group by category;
From the original table, you could just do:
select category, count(*) as cnt,
avg(case when type <> 'v' then 1.0 else 0 end) as ratio
from tbl1
group by category;

How to include percent calculations in pivot statement

Using a pivot statement, I am able to break down the count in a table of data by title:
select * from (
select * from ta
)
pivot (
COUNT(title)
for title in ( 'worker', 'manager') )
So the result looks like this:
STATUS 'worker' 'manager'
started 3 1
finished 4 5
ready 3 4
What I need to add to it is the percentages, i.e.
STATUS 'worker' percent 'manager' percent
started 3 30% 1 10%
finished 4 40% 5 50%
ready 3 30% 4 40%
Any idea how I can accomplish this within the same statement?
see http://sqlfiddle.com/#!4/e6c04a/1/0
Rather than using conditional aggregation, You may use -
select STATUS
,WORKER
,WORKER/SUM(WORKER) OVER()*100 percent
,MANAGER
,MANAGER/SUM(MANAGER) OVER()*100 percent
from (
select * from ta
)
pivot (
COUNT(title)
for title in ( 'worker' AS WORKER, 'manager' AS MANAGER))
Here is the demo.
Use conditional aggregation and window functions:
select status,
sum(case when title = 'worker' then 1 else 0 end) as worker,
sum(case when title = 'manager' then 1 else 0 end) as manager,
(sum(case when title = 'worker' then 1 else 0 end) /
sum(sum(case when title = 'worker' then 1 else 0 end)) over ()
) as worker,
(sum(case when title = 'manager' then 1 else 0 end) /
sum(sum(case when title = 'manager' then 1 else 0 end)) over ()
) as manager
from ta
group by status;
Here is a sql<>fiddle.

Best way to do a Count in TSQL

I am not so good in TSQL and i want to write a report in this manner:
input: Table A
ID Company Product Flag
1 A Car Y
2 A Van N
3 B Van Y
4 A Part N
Output
Company Y N
A 1 2
B 1 0
if one can assist in TSQL...
You could use conditional aggregation:
SELECT Company
,SUM(CASE WHEN Flag = 'Y' THEN 1 ELSE 0 END) AS Y
,SUM(CASE WHEN Flag = 'N' THEN 1 ELSE 0 END) AS N
FROM tab
GROUP BY Company
You are looking for conditional aggregation:
select company,
sum(case when flag = 'Y' then 1 else 0 end) as num_y,
sum(case when flag = 'N' then 1 else 0 end) as num_n
from t
group by company;
You can use CASE expressions (the people call it "conditional aggregation") to count the flagged products per customer like this (which will ignore a record when the Product column is empty):
SELECT Company
, COUNT(CASE Flag WHEN 'Y' THEN Product END) AS Y
, COUNT(CASE Flag WHEN 'N' THEN Product END) AS N
FROM YourTable
GROUP BY Company;
Or you can use this PIVOT query, which is a short form of writing the above:
SELECT Company, Y, N
FROM (SELECT Company, Product, Flag FROM YourTable) AS src
PIVOT (COUNT(Product) FOR Flag IN (Y, N)) AS pvt;
use case when
select company,
sum(case when flag='Y' then 1 else 0 end) as Y,
sum(case when flag='N' then 1 else 0 end) as N from tabe_data
group by company

conditional aggregation on two different groups in SQL

Here is my data
COUNTYID POLLUTANT TYPE EMISSION
1 A 1
1 A 2
1 B 1
1 B 2
2 A 1
2 A 2
2 B 1
2 B 2
3 A 1
3 A 2
3 B 1
3 B 2
if I do
SELECT sum(EMISSION) from table where POLLUTANT = 'A' group by COUNTYID;
I would get pollution from Polutant 'A'. how can I write a query to get following data:
column 1 with sum of A, column 2 with sum of B, column 3 with sum of A and B?
Thank you
You can use case for filter the value you need
select COUNTYID, sum(case when POLLUTANT='A'then EMISSION else 0 END) tot_a
, sum(case when POLLUTANT='B'then EMISSION else 0 END) tot_b
, sum(EMISSION) tot_a_b
from my_table
group by COUNTYID
You can use conditional aggregation. This moves the filtering conditions from the where clause to the sum()s:
select countyid,
sum(case when emission = 'A' then emission else 0 end) as A,
sum(case when emission = 'B' then emission else 0 end) as B,
sum(emission) as total
from t
group by countyid;
I would like to give some idea. We can use pivot table for answer your question
SELECT * from dbo.[Table_1]
PIVOT
(Sum([type_emmssion]) for [polutant] in ([A], [B]) ) as PivotTable
group by [CountryId] ;
you have to use case statemet:
SELECT
SUM( CASE WHEN POLLUTANT = 'A' THEN EMISSION ELSE 0 END) AS A_EMISSION
SUM( CASE WHEN POLLUTANT = 'B' THEN EMISSION ELSE 0 END) AS B_EMISSION
SUM(EMISSION) AS total_emission
FROM table
GROUP BY COUNTYID;