Dividing two simple select statements - sql

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;

Related

No results for a query, but getting a number when turning into a subquery and making a count in redshift

I have the following query:
select count(*), cvc_date, cvc_product_id, cvc_seller_product_id, competitor_wid
from dw.f_catalogvscompetitors
group by cvc_date, cvc_product_id, cvc_seller_product_id, competitor_wid
having count(*) > 1
When I execute it, I get 0 rows as a result. However, when I make this:
select count(*) from (
select count(*), cvc_date, cvc_product_id, cvc_seller_product_id, competitor_wid
from dw.f_catalogvscompetitors
group by cvc_date, cvc_product_id, cvc_seller_product_id, competitor_wid
having count(*) > 1)
I get the number 22584. How is this possible?

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

Combining separate queries from the same table as separate columns

I have a query that I am trying to combine but with different columns. The specifics of this are:
Same table
Different Where clause
Oracle DB
Ran in DBVisualizer
This is what I was trying to do: it runs but it does not join the two columns. they are being outputted into separate Result tabs in DBVisualizer
Select count (distinct CODE) AS Comp_PCT
from cons.GM
Where POLICY='NR'
And PCT is null
UNION
Select count (distinct CODE) AS Comp_DTY
from cons.GM
Where POLICY='NR'
And DTY is null
I don't know that this answer is a correct form:
SELECT
(Select count (distinct CODE)
from cons.GM
Where POLICY='NR'
And PCT is null) AS Comp_PCT,
(Select count (distinct CODE)
from cons.GM
Where POLICY='NR'
And DTY is null) AS Comp_DTY

Sum in subquery

My Query is
select count(*) as cnt,
EXTRACT(day FROM current_date - min(txdate))::int as days,
sum (Select opening from acledgerbal l
where acname='Arv'
union all
Select sum(v2.debit-v2.credit) as opening from acvoucher2 v2 where
txdate<='05/03/2014') as opening
from acduebills acb,acledger l
where (acb.opening+acb.debit-acb.credit) > 0
and acb.unitname='Sales'
and l.acname='Arv'
and l.acno=acb.acno
Here it show more than one row returned by a subquery used as an expression Error.
How do using sum for the subquery.
I'm using postgresql 9.1
EDIT:
I want to get count of rows in acduebills tables which is (acb.opening+acb.debit-acb.credit) > 0 and acb.unitname='Sales'. After that I want to get difference of day which is minimum date in same condition. After that I want to get opening, which comes from two tables: acledgerbal and acvoucher2. acvoucher is table checked by the txdate condition.
How to get those detail in single query?. How to get Same details in multiple schema's?
Something like this:
SELECT count(*) AS cnt
, current_date - min(txdate)::date AS days -- subtract dates directly
, (SELECT round(sum(opening)::numeric, 2)
FROM (
SELECT opening
FROM acledgerbal
WHERE acname = 'Arv'
UNION ALL
SELECT debit - credit
FROM acvoucher2
WHERE txdate <= '2014-05-03'
) sub
) AS opening
FROM acduebills b
JOIN acledger l USING (acno)
WHERE ((b.opening + b.debit) - b.credit) > 0
AND b.unitname ='Sales'
AND l.acname = 'Arv';
round() to decimal places only works with type numeric, so I cast the sum.
The problem here in the following statement:
sum ( Select opening from acledgerbal l
where acname='Arv'
union all
Select sum(v2.debit-v2.credit) as opening from acvoucher2 v2,
txdate<='05/03/2014' )
You use UNION so this subquery returns at least 2 rows. So you get an error that subquery can't return more than one row: "more than one row returned by a subquery used as an expression"
Try to change it to:
(Select SUM(opening) from acledgerbal l WHERE acname='Arv')
+
(Select SUM(v2.debit-v2.credit) as opening from acvoucher2 v2
WHERE txdate<='05/03/2014')

Oracle SQL Developer - Count function

This is the output of a select * from table1, I have a doubt with count function... I want to count that NULL, in order to do that the proper option is to do this:
select count(*) from table1 where fecha_devolucion is null --> This gives me the proper answer counting 1 however if i do:
select count(fecha_devolucion)
from table1
where fecha_devolucion is null --> this returns 0, why? Isn't the same syntax?
What's the difference between choosing a specific field and * from a table?
From the documentation (http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions032.htm):
If you specify expr, then COUNT returns the number of rows where expr
is not null. ...
If you specify the asterisk (*), then this function returns all rows...
In other words, COUNT(fecha_devolucion) counts non-NULL values of that column. COUNT(*) counts the total number of rows, regardless of the values.
This is the another way how you can get the count :
SELECT SUM(NVL(fecha_devolucion,1)) FROM table1 WHERE fecha_devolucion IS NULL;
Let's compare the two queries:
select count(*)
from table1
where fecha_devolucion is null;
select count(fecha_devolucion)
from table1
where fecha_devolucion is null;
I think you misunderstand the count() function. This function counts the number of non-NULL values in its argument list. With a constant or *, it counts all rows.
So, the first counts all the matching rows. The second counts all the non-NULL values of fecha_devolucion. But there are no such values because of the where clause.
By the way, you can also do:
select sum(case fecha_devolucion is null then 1 else 0 end) as Nullfecha_devolucion
from table1;