i have a table with two column:
Name Values
----------------
Target 20
v1 10
v2 20
v3 10
Total 30
percent ?
i want to calculate the percentage with the single column value to get the formula as
--> Target/Total * 100.
i.e for ex: Total = SUM (Values) in that way......
by using two rows in the column in sql query, how to do calculation in single column with diff row values by implementing a formula in it.
i dont want to calculate total. i want to calculate percent:
formula:Target/Total * 100 . that gives me the value in percent row?
how? to query it?
You want an additional record to show up in your query output? That seems like you're trying to make it look & work like an Excel spreadsheet, but anyways, here's how you'd go about doing it:
SELECT Name, Values FROM table
UNION (Select function(inputs) AS Name, function(inputs) AS Values FROM table)
If you want to this in SQL...
;WITH
cTarget AS (SELECT Values AS Target FROM MyTable WHERE name = 'Target'),
cTotals AS (SELECT SUM(Values) AS Total FROM MyTable WHERE name <> 'Target')
SELECT * FROM MyTable
UNION ALL
SELECT 'Total', Total FROM cTotals
UNION ALL
SELECT
'Percentage', 1.0 * Target / Total * 100
FROM
cTarget
CROSS JOIN
cTotals
Related
I'm trying to query an inventory database to show me how much wax is remaining on hand. Currently this is how I have the query set up:
SELECT * FROM (
SELECT SUM(OC.Quantity) AS oz_Wax_Ordered
FROM Order_In AS OC
WHERE (OC.Component) like ('Wax')
GROUP BY OC.Component
UNION SELECT SUM(C.Wax_Used) AS oz_Wax_Used
FROM Fragrance_Oils AS FO INNER JOIN Candles AS C ON FO.FragranceID = C.Fragrance
GROUP BY FO.Component
);
This shows me a single column with the second SELECT statement totaling the wax used, and the third SELECT totaling the wax ordered, displayed in rows 1 and 2, respectively.
I'm trying to have the total wax used subtract from the total wax ordered. I can share more information about the database itself if needed. Thanks!
You could sum the values:
SELECT
SUM([oz_Wax]) AS oz_Wax_OnHand
FROM
(SELECT SUM(OC.Quantity) AS oz_Wax
FROM Order_In AS OC
WHERE (OC.Component) like ('Wax')
GROUP BY OC.Component
UNION ALL
SELECT -SUM(C.Wax_Used)
FROM Fragrance_Oils AS FO
INNER JOIN Candles AS C
ON FO.FragranceID = C.Fragrance
GROUP BY FO.Component);
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.
Need help with active record query, I have customers table with name and balance fields.
How can I make query that will return 6 values, first 5 is top customers by balance and 6th one is sum of all others?
need to select name and balance, for others it will custom name ''Other
Divide et impera: make two view then put results together
select *
from ( select * from view_top_5
union
select * from view_sum )
where view_top_5 is view (or a subquery) that gives you the top 5 customers, view_sum is a query giving you the sum. To get "all others" you can build your query from this skeleton:
-- skeleton for view "view_sum"
select <what you need>
from mytable
where customer_id not in (select client_id
from view_top_5)
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 got a table in hive with the following schema
(diference int,count_value int)
The values are
5 2,
30 1,
90 1,
100 1
Now i want to find percentage of each count_value with sum of count_value. Something like count_value/sum(count_value) for each row. Can anybody please help. Thanks in advance
With the new analytics and windowing functions introduced in Hive 0.11, you can do:
SELECT count_value / sum(count_value) over () as p from myTable
This avoids a join, plus easier to do the calculation if partitioned by another field. For example, if the source table had a key field and you wanted the calculation to use the sum from the rows with the same key, you could do:
SELECT count_value / sum(count_value) over (partition by key) as p from myTable
How about using a subquery to calculate the total first, then joining the total to each row?
SELECT
count_value / count_value_sum AS p
FROM
myTable t
JOIN
(SELECT SUM(count_value) AS count_value_sum FROM myTable) s
Hope that helps.