SQL: create another column that calculates ratio - sql

So I have a table that looks like the following:
car owner
non car owner
have dog
num ppl
1
0
1
60
0
1
1
80
1
0
0
90
1
0
0
98
I am trying to add another column to find the ratios. For example, the total number of car owners is 110. If I want to find the ratio of people who own car and have dog, then I have to divide 60/110 for the first row. Also, the total number of non car owners is 98. Therefore, if I want to find that ration, I need to divide 80 by 98 for the second row and so on.
So far, I have tried the following code:
with a as (
select
id,
case when car_owner = 1 then 1 else 0 end car_owner,
case when non_car_owner = 1 then 1 else 0 end as non_car_owner = 1
from `xyz_table`
),
b as (select
car_owner,
non_car_owner,
case when have_dog = 1 then 1 else 0 end have_dog,
count(distinct id) num_ppl
from `xyz_table`
join a using (id)
group by 1,2,3
order by 4 desc
)
select *, num_ppl/(select (case when dog_owner = 1 then 110 else 0 end) as ratio
from a)
from b
Unfortunately , it throws the following error:
Scalar subquery produced more than one element
Any help would be appreciated.
PS. I am running this code on google bigquery.

If I want to find the ratio of people who own car and have dog,
You can use avg():
select avg(car_owner * have_dog)
from t;

Related

pgsql rotate table and generate dynamic columns based on values

I want to make a dynamic pivot on pgsql table
Original table :
time
strategy
pnl
1
a
100
2
a
200
3
a
300
1
b
1000
2
b
2000
1
c
22
target table :
time
sum
a
b
c
1
1132
100
1000
32
2
2200
200
2000
0
3
22
0
0
22
the problem that the strategy content is dynamic i can have sometimes over 40 unique values (in this example there are only 3 a,b,c )
i have the following code it looks like a good start but the are some problems i cannot solve
SELECT time,
--sum(case when strategy='a' then pnl else 0 end) AS "a" ,
--sum(case when strategy='b' then pnl else 0 end) AS "b" ,
--sum(case when strategy='c' then pnl else 0 end) AS "c"
--generate the contect above, (when using the code above the function works)
(SELECT string_agg(clause, ',')
FROM (SELECT format('sum(case when strategy=''%s'' then pnl else 0 end) AS "%s" ',
strategy, strategy) AS clause
FROM (SELECT DISTINCT strategy FROM server_logs.logs where strategy != '' and subclass = 'pnl') s
ORDER BY strategy) clauses)
FROM (
select case when strategy is null then 'system' else strategy end as strategy,
time,
sum(case when value::float!=0 then 0::float else value::float end) as pnl
FROM server_logs.logs
where subclass='pnl'
group by rollup(strategy), time
) as t
group by time
order by time desc
;

SQL transform table with sum based on values

i have table like this:
operation_id
order_id
qty
qty_type
detail_type
1
1
240
ready
glued
1
1
199
ready
unglued
1
1
100
done
glued
1
2
50
ready
glued
and would like to transform into this. it means to add 4 columns and to sum them from above table based on a conditions, like detail_type = 'glued', qty_type = 'ready' etc.
operation_id
order_id
qty_glued_ready
qty_unglued_ready
qty_glued_done
qty_unglued_done
1
1
240
199
10
10
can somebody help me how query should look like?
I assume it is just an example that you have mentioned in your OP and it is not accurate according to your table data you have mentioned.
I don't understand how your qty_glued_done is 10
But here is something you can start working out with:
SELECT o.`operation_id`, o.`order_id`,
SUM(CASE WHEN `detail_type`='glued' AND o.`qty_type`='ready' THEN o.`qty` ELSE 0 END) AS qty_glued_ready,
SUM(CASE WHEN `detail_type`='unglued' AND o.`qty_type`='ready' THEN o.`qty` ELSE 0 END) AS qty_unglued_ready
(and so on)
FROM `operation_table` o GROUP BY o.`operation_id`

Show two different sum columns based on a single column

Show two different sum columns based on another column.
For this table:
ID Item Quantity Location
1 1 10 A
2 1 10 B
3 1 10 A
4 2 10 A
5 2 10 A
6 2 10 B
7 3 10 A
8 3 20 A
I need to see the total quantities for both location A and location B (to compare which is higher), but only for items that have a location B:
Expected result:
Item Quantity A Quantity B
1 20 10
2 20 10
I've been trying this but getting errors:
SELECT st.item, st.qty ALIAS(stqty),
(SELECT SUM(dc.qty)
FROM table dc
WHERE st.item = dc.item) ALIAS(dcqty))
FROM table st
WHERE location ='b'
I can do this easily with two queries obviously, but I was hoping for a way to do it in one.
you can use a sum with case statement to do your pivot then a having to exclude rows with no total for b
here is the fiddle
https://www.db-fiddle.com/f/rS8fgvWoFxn879Utc2CKbu/0
select Item,
sum(case when Location = 'A' then Quantity else 0 end),
sum(case when Location = 'B' then Quantity else 0 end)
from myTable
group by Item
having sum(case when Location = 'B' then Quantity else 0 end) > 0

SQL Server : how can I get difference between counts of total rows and those with only data

I have a table with data as shown below (the table is built every day with current date, but I left off that field for ease of reading).
This table keeps track of people and the doors they enter on a daily basis.
Table entrance_t:
id entrance entered
------------------------
1 a 0
1 b 0
1 c 0
1 d 0
2 a 1
2 b 0
2 c 0
2 d 0
3 a 0
3 b 1
3 c 1
3 d 1
My goal is to report on people and count entrances not used(grouping on people), but ONLY if they entered(entered=1).
So using the above table, I would like the results of query to be...
id count
----------
2 3
3 1
(id=2 did not use 3 of the entrances and id=3 did not use 1)
I tried queries(some with inner joins on two instances of same table) and I can get the entrances not used, but it's always for everybody. Like this...
id count
----------
1 4
2 3
3 1
How do I not display results id=1 since they did not enter at all?
Thank you,
You could use conditional aggregation:
SELECT id, count(CASE WHEN entered = 0 THEN 1 END) AS cnt
FROM entrance_t
GROUP BY id
HAVING count(CASE WHEN entered = 1 THEN 1 END) > 0;
DBFiddle Demo

Inserting a new indicator column to tell if a given row maximizes another column in SQL

I currently have a table in SQL that looks like this
PRODUCT_ID_1 PRODUCT_ID_2 SCORE
1 2 10
1 3 100
1 10 3000
2 10 10
3 35 100
3 2 1001
That is, PRODUCT_ID_1,PRODUCT_ID_2 is a primary key for this table.
What I would like to do is use this table to add in a row to tell whether or not the current row is the one that maximizes SCORE for a value of PRODUCT_ID_1.
In other words, what I would like to get is the following table:
PRODUCT_ID_1 PRODUCT_ID_2 SCORE IS_MAX_SCORE_FOR_ID_1
1 2 10 0
1 3 100 0
1 10 3000 1
2 10 10 1
3 35 100 0
3 2 1001 1
I am wondering how I can compute the IS_MAX_SCORE_FOR_ID_1 column and insert it into the table without having to create a new table.
You can try like this...
Select PRODUCT_ID_1, PRODUCT_ID_2 ,SCORE,
(Case when b.Score=
(Select Max(a.Score) from TableName a where a.PRODUCT_ID_1=b. PRODUCT_ID_1)
then 1 else 0 End) as IS_MAX_SCORE_FOR_ID_1
from TableName b
You can use a window function for this:
select product_id_1,
product_id_2,
score,
case
when score = max(score) over (partition by product_id_1) then 1
else 0
end as is_max_score_for_id_1
from the_table
order by product_id_1;
(The above is ANSI SQL and should run on any modern DBMS)