SQL select distinct count division - sql

I have a select statement with a distinct count and I need to divide the result of the select count with a number . How can I do it?
I have for example:
select distinct count(profile_entity_name)
from table_name
where visit_id like 'JWM%' and profile_entity_name not in ('JWM 2.0 COM','JWM 2.0 MASTER')
and it gives the result 2000
I want to divide the 2000 with 130 for example on the fly
How can I do it?
I tried the whole select with the division at the end '/130' and it doesn't work

As an complement to Md. Suman Kabir's Answer, if you want the select to return the exact division, just cast the "count" function and the divisor number as float, like this:
select cast(count(distinct profile_entity_name) as float) / cast(130 as float) from table_name

Use of distinct before count has no meaning. Perhaps you want to count distinct profile_entity_name, So you can try this :
select count(distinct profile_entity_name) / 130 from ....

save your result and divide with 130
select TB1.[total count]/130 from (select distinct count(profile_entity_name)as[total count]
from table_name
where visit_id like 'JWM%' and profile_entity_name not in ('JWM 2.0 COM','JWM 2.0 MASTER') ) as TB1

Related

query the percentage of occurrences in an SQL table

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.

calculating percentage in postgresql with conditions

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

how to perform multiple aggregations on a single SQL query

I have a table with Three columns:
GEOID, ParcelID, and PurchaseDate.
The PKs are GEOID and ParcelID which is formatted as such:
GEOID PARCELID PURCHASEDATE
12345 AB123 1/2/1932
12345 sfw123 2/5/2012
12345 fdf323 4/2/2015
12346 dfefej 2/31/2022 <-New GEOID
What I need is an aggregation based on GEOID.
I need to count the number of ParcelIDs from last month PER GEOID
and I need to provide a percentage of that GEOID of all total sold last month.
I need to produce three columns:
GEOID Nbr_Parcels_Sold Percent_of_total
For each GEOID, I need to know how many Parcels Sold Last month, and with that Number, find out how much percentage that entails for all Solds.
For example: if there was 20 Parcels Sold last month, and 4 of them were sold from GEOID 12345, then the output would be:
GEOID Nbr_Parcels_Sold Perc_Total
12345 4 .2 (or 20%)
I am having issues with the dual aggregation. The concern is that the table in question has over 8 million records.
if there is a SQL Warrior out here who have seen this issue before, Any wisdom would be greatly appreciated.
Thanks.
Hopefully you are using SQL Server 2005 or later version, in which case you can get advantage of windowed aggregation. In this case, windowed aggregation will allow you to get the total sale count alongside counts per GEOID and use the total in calculations. Basically, the following query returns just the counts:
SELECT
GEOID,
Nbr_Parcels_Sold = COUNT(*),
Total_Parcels_Sold = SUM(COUNT(*)) OVER ()
FROM
dbo.atable
GROUP BY
GEOID
;
The COUNT(*) call gives you counts per GEOID, according to the GROUP BY clause. Now, the SUM(...) OVER expression gives you the grand total count in the same row as the detail count. It is the empty OVER clause that tells the SUM function to add up the results of COUNT(*) across the entire result set. You can use that result in calculations just like the result of any other function (or any expression in general).
The above query simply returns the total value. As you actually want not the value itself but a percentage from it for each GEOID, you can just put the SUM(...) OVER call into an expression:
SELECT
GEOID,
Nbr_Parcels_Sold = COUNT(*),
Percent_of_total = COUNT(*) * 100 / SUM(COUNT(*)) OVER ()
FROM
dbo.atable
GROUP BY
GEOID
;
The above will give you integer percentages (truncated). If you want more precision or a different representation, remember to cast either the divisor or the dividend (optionally both) to a non-integer numeric type, since SQL Server always performs integral division when both operands are integers.
How about using sub-query to count the sum
WITH data AS
(
SELECT *
FROM [Table]
WHERE
YEAR(PURCHASEDATE) * 100 + MONTH(PURCHASEDATE) = 201505
)
SELECT
GEOID,
COUNT(*) AS Nbr_Parcels_Sold,
CONVERT(decimal(18,8), COUNT(*)) /
(SELECT COUNT(*) FROM data) AS Perc_Total
FROM
data t
GROUP BY
GEOID
EDIT
To update another table by the result, use UPDATE under WITH()
WITH data AS
(
SELECT *
FROM [Table]
WHERE
YEAR(PURCHASEDATE) * 100 + MONTH(PURCHASEDATE) = 201505
)
UPDATE target SET
Nbr_Parcels_Sold = source.Nbr_Parcels_Sold,
Perc_Total = source.Perc_Total
FROM
[AnotherTable] target
INNER JOIN
(
SELECT
GEOID,
COUNT(*) AS Nbr_Parcels_Sold,
CONVERT(decimal(18,8), COUNT(*)) /
(SELECT COUNT(*) FROM data) AS Perc_Total
FROM
data t
GROUP BY
GEOID
) source ON target.GEOID = source.GEOID
Try the following. It grabs the total sales into a variable then uses it in the subsequent query:
DECLARE #pMonthStartDate DATETIME
DECLARE #MonthEndDate DATETIME
DECLARE #TotalPurchaseCount INT
SET #pMonthStartDate = <EnterFirstDayOfAMonth>
SET #MonthEndDate = DATEADD(MONTH, 1, #pMonthStartDate)
SELECT
#TotalPurchaseCount = COUNT(*)
FROM
GEOIDs
WHERE
PurchaseDate BETWEEN #pMonthStartDate
AND #MonthEndDate
SELECT
GEOID,
COUNT(PARCELID) AS Nbr_Parcels_Sold,
CAST(COUNT(PARCELID) AS FLOAT) / CAST(#TotalPurchaseCount AS FLOAT) * 100.0 AS Perc_Total
FROM
GEOIDs
WHERE
ModifiedDate BETWEEN #pMonthStartDate
AND #MonthEndDate
GROUP BY
GEOID
I'm guessing your table name is GEOIDs. Change the value of #pMonthStartDate to suit yourself. If your PKs are as you say then this will be a quick query.

Dividing two simple select statements

I have two simple select statements and just want to divide the first one by the second one.
SELECT COUNT (DISTINCT INITIATIVE_ID)
FROM GPD_ERROR_WARNING_NEW
SELECT COUNT (DISTINCT SAVINGS_ID)
FROM GPD_SAVINGS_REGULAR
The first select statement results in 300
The second select statement results in about 1500, so just looking for that magic .2
I know it is simple to do by hand but looking to automate this and make it a view as it needs to be updated every hour.
This will work, but it leaves you open to a "divide by zero" error:
SELECT
(SELECT COUNT (DISTINCT INITIATIVE_ID) FROM GPD_ERROR_WARNING_NEW) /
(SELECT COUNT (DISTINCT SAVINGS_ID) FROM GPD_SAVINGS_REGULAR)
FROM DUAL;
This will give you a null result instead of an error if the second (denominator) count comes up as zero:
SELECT
(SELECT COUNT (DISTINCT INITIATIVE_ID) FROM GPD_ERROR_WARNING_NEW) /
NULLIF((SELECT COUNT (DISTINCT SAVINGS_ID) FROM GPD_SAVINGS_REGULAR), 0)
FROM DUAL;
You can write your query like this (in sql):
SELECT
(SELECT DISTINCT COUNT(INITIATIVE_ID) FROM GPD_ERROR_WARNING_NEW) /
ISNULL((SELECT DISTINCT COUNT(SAVINGS_ID) FROM GPD_SAVINGS_REGULAR), 0)
FROM DUAL;

How to find the SUM of the Calculated Percentage Column in SQL

I have a Column which calculates the Percentage , like this
Convert(decimal(5,2),(PatCount*100)/#PatientCount) as Per
Now i want the Total Sum of the above calculated Percentage. So i tried doing this,
,SUM(Convert(decimal(5,2),(PatCount*100)/#PatientCount)) as pa
But i am not geting the Desired Result . For ex
Per % Total %
6.00 6.00
7.00 7.00
85.00 85.00
I want the total Col to print as 98%. Please can Somebody help me with this.
Try this
SQL FIDDLE Example
select
cast(PatCount * 100 / #PatientCount as decimal(5,2)) as Per,
sum(cast(PatCount * 100 / #PatientCount as decimal(5,2))) over() as Total
from Patients as P
For the sum to work you will need a GROUPBY, then the sum is over the group.
Simplest is to do this in a separate query. There may be a way to do this with a sub-query or you could look into a cumulative sum: Create a Cumulative Sum Column in MySQL
You have some options but using a CROSS APPLY should have pretty good performance characteristics.
;WITH q AS (
SELECT SUM(CONVERT(decimal(5,2),(PatCount*100)/#PatientCount)) as Per
FROM YourTable
)
SELECT CONVERT(decimal(5,2),(PatCount*100)/#PatientCount) as Per
, q.Per
FROM YourTable
CROSS APPLY q
or perhaps even better (to read)
;WITH q AS (
SELECT CONVERT(decimal(5,2),(PatCount*100)/#PatientCount) as Per
FROM YourTable
)
SELECT q.Per, qtot.PerTot
FROM q CROSS APPLY (SELECT SUM(Per) as PerTot FROM q) qtot