I want to calculate the sum of the column with an if condition. Here my code:
SELECT
SUM(CASE WHEN properties = 'revenue' THEN property.value ELSE 0 END) AS TotalRevenue,
FROM deneme-319116.events.eventstable;
Sample data:
Properties Property.value
Search / null
Revenue / 15
Count / 25
Revenue / 40
I need to find 55 (15+40) as a result
In my eventstable I have columns properties and the property.value. I want to sum the property.value values that properties equal to 'revenue' but bigquery gives error: 'cannot acces field key on a value with type ARRAYvalue....
Plz help, thx
Hmmm . . . then you need to unnest(). If you want the sum per row in the original table:
SELECT (SELECT SUM(el.value)
FROM unnest(et.properties) el
WHERE el.name = 'revenue' -- or whatever the name is
) as TotalRevenue,
FROM deneme-319116.events.eventstable et;
If you want the overall sum over all rows:
SELECT SUM(el.value) as TotalRevenue,
FROM deneme-319116.events.eventstable et CROSS JOIN
unnest(et.properties) el
WHERE el.name = 'revenue' -- or whatever the name is
Related
I'm a beginner in SQL and my question is about calculating a percentage of the overall disclosed total from a table called merged. I want to calculate the number of 'SUPPORT' from committee_position (a column in the table merged)
How to calculate the percentage in that case.
I start with:
SELECT Sum (amount) *100
from merged
where merged.committee_position == 'SUPPORT';
Help me continue it, Thank you
If I followed you correctly, you can do conditional aggregation:
select
100.0 * sum(case when committee_position = 'SUPPORT' then amount else 0 end) / sum(amount)
from merged
This gives you the percentage of amount that have committee_position = 'SUPPORT' over the total amount in the table.
Here you go.
SELECT a.Support_Amount/b.Total_Amount*100
FROM (SELECT Sum (amount) as Support_Amount
from merged
where merged.committee_position = 'SUPPORT') as a
CROSS JOIN
(SELECT Sum (amount) as Total_Amount
from merged) as b
I have a table of names, where each row has the columns name, and occurrences.
I'd like to calculate the percentage of a certain name from the table.
How can I do that in one query?
You can get it by using SUM(occurrences):
select
name,
100.0 * sum(occurrences) / (select sum(occurrences) from users) as percentage
from
users
where name = 'Bob'
Try this:
SELECT name, cast(sum(occurance) as float) /
(select sum(occurance) from test) * 100 percentage FROM test
where name like '%dog%'
Demo here
It is not very elegant due to the subquery in the field list but this will do the job if you want it in one query:
SELECT
`name`,
(CAST(SUM(`occurance`) AS DOUBLE)/CAST((SELECT SUM(`occurance`) FROM `user`) AS DOUBLE)) as `percent`
FROM
`user`
WHERE
`name`='miroslav';
Example Fiddle
Hope this helps,
I think conditional aggregation is the best approach:
select sum(case when name = #name then occurrences else 0 end) / sum(occurrences) as ratio
from t;
If you want an actual percentage between 0 and 100 multiply by 100.
I want to apply a calculation in a column in 2 cases:
When the Etablissements in: ("E10", "E20")
Then apply this calcul: Price * 10 in the column name: Calcul
When Etablissement in ("E30", "E40") then apply another calcul in the same column Calcul: Price * 50
I wish that you get it, and thank you.
You just want CASE expression :
SELECT t.*,
(CASE WHEN Etablissements in ('E10','E20')
THEN Price*10
WHEN Etablissement in ('E30','E40')
THEN Price*50
END) AS Calcul
FROM table t;
I have one table and I want to calculate the percentage of one column
I tried to do so in two ways.
but I am actually face with error.
The error is 'syntax error at or near "select"'
This is my code in below:
WITH total AS
(
select krs_name,count(fclass) as cluster
FROM residentioal
GROUP BY krs_name
)
SELECT total.cluster,
total.krs_name
(select count(fclass)
FROM residentioal
where fclass='village'
or fclass='hamlet'
or fclass='suburb'
or fclass='island'
AND krs_name = total.krs_name
)::float / count(fclass) * 100 as percentageofonline
FROM residentioal, total
WHERE residentioal.krs_name = total.krs_name
GROUP BY total.krs_name total.krs_name
My table has 5437 rows in which there is 8 group of krs_name and in the other column namely fclass, there is 6 group. Therefore I want to calculate the percentage of 4 groups from fclass for each krs_name . thus, i have to first query the count(fclass) group by krs_name and then query the count of fclass where fclass is equal to my condition group by krs_name and finally count(fclass) "with condition" / count(fclass) "total fclass" * 100 goup by krs_name?
I'm using Postgresql 9.1.
The problem is in this line:
SELECT total.cluster, total.krs_name (
The open paren makes no sense.
But, this seems to do what you want and it is much simpler:
SELECT r.krs_name, COUNT(*) as total,
AVG( (fclass in ('village', 'hamlet', 'suburb', 'island'))::int ) * 100 as percentageofonline
FROM residentioal r
GROUP BY r.krs_name
I have a table called vegetation with 2 columns:
type, count
I want to sum the count for all the rows where count value is smaller than the average count and all those for which it is higher.
I don't know how to reflect this in the group by clause... (or somewhere else?).
I guess that another way of doing it should be by assigning a value to all less-than-average data and another value to all higher-than-average data and then group by this value. But I just started and can not figure out how to do that either.
SELECT sum(CASE WHEN ct <= x.avg_ct THEN ct ELSE 0 END) AS sum_ct_low
,sum(CASE WHEN ct > x.avg_ct THEN ct ELSE 0 END) AS sum_ct_hi
FROM vegetation v
,(SELECT avg(ct) AS avg_ct FROM vegetation) x
The average is a single value, you can just CROSS JOIN the subquery to the base table (comma separated list of tables means cross-joining).
Then filter with a simple CASE statement.
I use ct instead of count, since that's a reserved word in SQL.