This question already has answers here:
Rounding to 2 decimal places in SQL
(5 answers)
Closed last month.
SELECT
CONCAT((COUNT_PEOPLE_INSIDE_3MILES/ COUNT_TOTAL_PEOPLE*100),'%') Percentage_People_Within_3miles
FROM
(
SELECT count(*) COUNT_PEOPLE_INSIDE_3MILES
FROM CLIENT_DATA
WHERE (ABS(C_ADD_X) >=1 AND ABS(C_ADD_X) <=3) AND (ABS(C_ADD_Y) >=1 AND ABS(C_ADD_Y) <=3)
),
(
SELECT count(CLIENT_ID) COUNT_TOTAL_PEOPLE
FROM CLIENT_DATA
);
You can use the ROUND() function in MySQL To round to 2 decimal.
SELECT ROUND(your_value, 2);
SELECT ROUND(your_value, 2);
This can be done
Related
This question already has answers here:
Referencing other columns in a SQL SELECT
(2 answers)
Count distinct Alias in SQL?
(1 answer)
Closed 4 months ago.
I'm trying to get the percentage of times something occurs (a task requires an outbound call)
So I made a count distinct of the occurrence "CALLED THE CUSTOMER" and aliased it as outbound_calls. And counted all the tasks alias "total_tasks"
These two steps work and I get this
Now I'm trying to divide outbound_calls by total_taks to get the percentage but get an error saying that the name oubound_calls is not recognized
This is the query I'm writing
SELECT
COUNT(DISTINCT IF(Out_CONTACT_TYPE = 'CALLED THE CUSTOMER', booking_id, NULL )) AS outbound_calls,
COUNT(task_id) AS total_tasks,
DIV(outbound_calls, total_tasks)
FROM `cs-reporting...`
Any ideas?
Happy Friday :)
I hope this can help.
You can use a subquery in this case :
SELECT
outbound_calls,
total_tasks,
DIV(outbound_calls, total_tasks)
FROM
(
SELECT
COUNT(DISTINCT IF(Out_CONTACT_TYPE = 'CALLED THE CUSTOMER', booking_id, NULL )) AS outbound_calls,
COUNT(task_id) AS total_tasks
FROM `cs-reporting...`
)
You can also use a WITH bloc :
WITH reporting AS
(
SELECT
COUNT(DISTINCT IF(Out_CONTACT_TYPE = 'CALLED THE CUSTOMER', booking_id, NULL )) AS outbound_calls,
COUNT(task_id) AS total_tasks
FROM `cs-reporting...`
)
SELECT
outbound_calls,
total_tasks,
DIV(outbound_calls, total_tasks)
FROM reporting;
For divisions with BigQuery, you can use SAFE_DIVIDE with ROUND functions (example with 2 digits) :
SELECT
outbound_calls,
total_tasks,
ROUND(SAFE_DIVIDE(outbound_calls, total_tasks), 2) as divres
FROM
(
SELECT
5 as outbound_calls,
3 as total_tasks
);
According to the BigQuery documentation the aliases are visibles in the from not in the select bloc directly.
This question already has answers here:
SQL Server, division returns zero
(6 answers)
Closed 4 years ago.
I have a table consisting of databases, tables, variables, values and the frequency count. I want to query this and create a [Percent] column which will be the frequency out of the total at variable level.
I have managed to join on the total, but the percentage calculating doesn't seem to be working. I am getting 0 for every Percent value.
select t1.[database],
t1.[table],
t1.[variable],
t1.[value],
t1.[frequency],
t2.[vartotal],
([frequency]) / t2.vartotal *100 as [Percent]
from TestDB.dbo.Meta t1
join (select [database], [table_name], [variable], sum(frequency) as vartotal
from TestDB.dbo.Meta
group by [database],
[table],
[variable]) t2
on t1.[database]=t2.[database] and
t1.[table]=t2.[table] and
t1.[variable]=t2.[variable]
group by t1.[database], t1.[table_name], t1.variable,
t1.[value], t1.[frequency], t2.[vartotal]
Why is returning zeroes for all percentage calculations?
SQL Server does integer division, so 1/2 = 0, not 0.5. I usually just multiply by 1.0 to solve this problem:
([frequency] * 100.0 / t2.vartotal) as [Percent]
That said, you can use window functions for this calculation:
select . . .,
sum(frequency) over (partition by database, table, variable) as vartotal,
([frequency] * 100.0 / sum(frequency) over (partition by database, table, variable) ) as [Percent]
from TestDB.dbo.Meta t1;
This is much simpler and should have better performance than your query.
Probably an issue regarding what datatype frequency is. Try the following:
Also might want to add some extra parents to ensure order of operations.
select t1.[database],
t1.[table],
t1.[variable],
t1.[value],
t1.[frequency],
t2.[vartotal],
((TRY_CONVERT(FLOAT, [frequency])) / TRY_CONVERT(FLOAT, t2.vartotal)) * 100 as [Percent]
from TestDB.dbo.Meta t1
join (select [database], [table_name], [variable], sum(frequency) as vartotal
from TestDB.dbo.Meta
group by [database],
[table],
[variable]) t2
on t1.[database]=t2.[database] and
t1.[table]=t2.[table] and
t1.[variable]=t2.[variable]
group by t1.[database], t1.[table_name], t1.variable,
t1.[value], t1.[frequency], t2.[vartotal]
I have a single table database consists of the following fields:
ID, Seniority (years), outcome and some other less important fields.
Table row example:
ID:36 Seniority(years):1.79 outcome:9627
I need to write a query (sql server) in relatively simple code that returns the average outcome, grouped by the Seniority field, with leaps of five years (0-5 years, 6-10 etc...) with the condition that the average will be shown only if the group has more than 3 rows.
Result row example:
range:0-5 average:xxxx
Thank you very much
Use CASE statement to create different age groups. Try this
select case when Seniority between 0 and 5 then '0-5'
when Seniority between 6 and 10 then '6-10'
..
End,
Avg(outcome)
From yourtable
Group by case when Seniority between 0 and 5 then '0-5'
when Seniority between 6 and 10 then '6-10'
..
End
Having count(1)>=3
Since you have decimal places, If you want to count 5.4 to 0-5 group and 5.6 to 6-10 then use Round(Seniority,0) instead of Seniority in CASE statement
P.s.
0-5 contains 6 values while 6-10 contains 5.
select 'range:'
+ cast (isnull(nullif(floor((abs(seniority-1))/5)*5+1,1),0) as varchar)
+ '-'
+ cast ((floor((abs(seniority-1))/5)+1)*5 as varchar) as seniority_group
,avg(outcome)
from t
group by floor((abs(seniority-1))/5)
having count(*) >= 3
;
This would be something like:
select floor(seniority / 5), avg(outcome)
from t
group by floor(seniority / 5)
having count(*) >= 3;
Note: This breaks the seniority into equal sized groups which is 0-4, 5-9, and so on. This seems more reasonable than having unequal groups.
You can follow Gordon's answer(but you should to edit it a little), but I would do this with additional table with all possible intervals. You then can add appropriate index to boost it.
create table intervals
(
id int identity(1, 1),
start int,
end int
)
insert into intervals values
(0, 5),
(6, 10)
...
select i.id, avg(t.outcome) as outcome
from intervals i
join tablename t on t.seniority between i.start and i.end
group by i.id
having count(*) >=3
If creating new tables is not an option you can always use a CTE:
;with intervals as(
select * from
(values
(0, 5),
(6, 10)
--...
) t(start, [end])
)
select i.id, avg(t.outcome) as outcome
from intervals i
join tablename t on t.seniority between i.start and i.[end]
group by i.id
having count(*) >=3
This question already has answers here:
Get AVG ignoring Null or Zero values
(6 answers)
Closed 6 years ago.
I have a column where I can get 0 values, but I need to calculate the average of that column without counting those 0's as part of the divider.
I have the following query:
SELECT AVG(CAST(RATING AS NUMERIC(18, 2))) AS AVERAGE,
QUESTIONID,
Questions.SHORTVERSION,
COUNT(CASE
WHEN RATING > 0
THEN 1
ELSE NULL
END) AS COUNT
FROM AnswersRating
INNER JOIN Questions ON Questions.ID = QUESTIONID
WHERE SURVEYID IN
(
SELECT ID
FROM SURVEYS
WHERE FLIGHTDATAID = '8371'
)
GROUP BY QUESTIONID,
Questions.SHORTVERSION
ORDER BY QUESTIONID;
I read somewhere that I can use NULLIF to help this, but I couldn't figure out how to use it correctly. Anyone who can help me out?
You can use NULLIF to convert your 0s into NULLs as AVG will ignore NULLs:
NULLIF(RATING, 0)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Dynamic SELECT TOP #var In SQL Server
why am I getting a syntax error at select top #recNo here?
create procedure getTopAccounts
(
#recNo int
)
as
begin
select top 1 accDesc, accNum
from
(select top #recNo accDesc,accNum
from
ACCOUNTS_TABLE
order by
accNum desc)
as a order by accNum
end
...select top (#recNo) accDesc...
Parametrised TOP needs to be in parenthesis