So I have
select count(*) from ( "query1")
select count(*) from ( "query2")
I want to divide the two and get the floating point result.
I was told to use something like this
SELECT (COUNT(smtg) * 1.0) / COUNT(smtg)
But Im not sure
You can just do:
select q1.cnt * 1.0 / q2.cnt
from (select count(*) as cnt from ( "query1") ) q1 cross join
(select count(*) as cnt from ( "query2") ) q2;
Or, if you prefer:
select ( (select count(*) from ( "query1")) * 1.0 /
(select count(*) from ( "query2"))
)
from sysibm.sysdummy1;
other solution (be carefull to not divide by 0)
with
query1 as (select count(*) as nb1 from ( "query1")),
query2 as (select count(*) as nb2 from ( "query2"))
select case when nb2=0 then null else nb1* 1.0/nb2 end as Result
from query1, query2
Related
I have a complex sql query, named qryARAT2B_EXT.
SELECT
*
FROM
(
SELECT
*
FROM
(
SELECT
*,
firstStudy,
ABS(DATEDIFF('d', firstStudy, Check_Date)) as diff
FROM
(
SELECT
*,
(
SELECT
TOP 1 Check_Date
FROM
qryARAT2B
WHERE
PATNR = [PАРАМ]
ORDER BY
Check_Date
)
AS firstStudy
FROM
(
SELECT
*
FROM
qryARAT2B
WHERE
PATNR = [PАРАМ]
)
AS myPatientsWithStudy
)
AS myPatientsFirstStudy
)
WHERE
diff = 0
)
AS T1
LEFT JOIN
(
SELECT
*
FROM
(
SELECT
*,
firstStudy,
ABS(DATEDIFF('d', firstStudy, Check_Date)) as diff
FROM
(
SELECT
*,
(
SELECT
TOP 1 Check_Date
FROM
qryARAT2B
WHERE
PATNR = [PАРАМ]
ORDER BY
Check_Date
)
AS firstStudy
FROM
(
SELECT
*
FROM
qryARAT2B
WHERE
PATNR = [PАРАМ]
)
AS myPatientsWithStudy
)
AS myPatientsFirstStudy
)
WHERE
diff = 4
)
As T2
ON T1.PATNR = T2.PATNR
When I open it in ms-access, it asks for the value of the [PARAM] and produces the result.
I have a table of patients.
tblPatient with the columns:
PATNR, and s.o.
That contains the PATNR's of patients:
000001
000002
...
XXXXXX
I need to write sql to calculate data for all PATNR's at once.
something like this:
SELECT (SELECT * FROM qryARAT2B_EXT WHERE [PARAM] = PATNR) from tblPatient
But it is not accepted from ms-access. I'm not able to pass parameter to qryARAT2B_EXT from the SQL. Is there any specific syntax for it in ms-access?
Ive been pulling my hair out to get around this..how can i convert this to a varchar and add the % symbol to the results?
I have placed the code below of the query and have tried to cast it but keep getting errors that i don't know how to fix. Can someone pleae make some suggestions on how to either simplify and prevent divide by zero issues also.
select 'Conversion Rate' as Type,
(
SELECT
(
COALESCE
(
CAST(CAST(nullif(t.ConvertedTrials,0) AS NUMERIC(18,2)) / (CAST(nullif(t.UnconvertedTrials,0) AS NUMERIC(18,2))) * 100 as decimal(10,2))
,
0
)
) AS Percentage
FROM (
SELECT
UnconvertedTrials = (
SELECT count(*) FROM
(
select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64' Group by membership.memberid
) as a
),
ConvertedTrials = (
SELECT count(*) FROM
(
SELECT membership.memberid
FROM Membership, Package
WHERE membership.PackageId = Package.Id
AND Package.PackageTypeId != 1
AND membership.memberid in (select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64')
Group by membership.memberid
) as b
)
) as t
) as Total
select 'Conversion Rate' as Type,
RTRIM(CAST((
SELECT
(
COALESCE
(
CAST(CAST(nullif(t.ConvertedTrials,0) AS NUMERIC(18,2)) / (CAST(nullif(t.UnconvertedTrials,0) AS NUMERIC(18,2))) * 100 as decimal(10,2))
,
0
)
) AS Percentage
FROM (
SELECT
UnconvertedTrials = (
SELECT count(*) FROM
(
select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64' Group by membership.memberid
) as a
),
ConvertedTrials = (
SELECT count(*) FROM
(
SELECT membership.memberid
FROM Membership, Package
WHERE membership.PackageId = Package.Id
AND Package.PackageTypeId != 1
AND membership.memberid in (select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64')
Group by membership.memberid
) as b
)
) as t
) AS NVARCHAR(50))) + '%' as Total
your actual percentage is quite a large sub-query, it's hard to see how to enclose it in brackets - I had about 10 goes - is it any good yet?
maybe it's easier to wrap the whole query in another select e.g
SELECT A,B FROM (<your query>) AS AQUERY
(which should give exactly same results as your query) - THEN work on formatting B in the outer query
Here is my second way of doing it
SELECT ORIGINAL.Type, RTRIM(CAST(ORIGINAL.Total as varchar(80))) + '%' AS Total
FROM
(
select 'Conversion Rate' as Type,
(
SELECT
(
COALESCE
(
CAST(CAST(nullif(t.ConvertedTrials,0) AS NUMERIC(18,2)) / (CAST(nullif(t.UnconvertedTrials,0) AS NUMERIC(18,2))) * 100 as decimal(10,2))
,
0
)
) AS Percentage
FROM (
SELECT
UnconvertedTrials = (
SELECT count(*) FROM
(
select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64' Group by membership.memberid
) as a
),
ConvertedTrials = (
SELECT count(*) FROM
(
SELECT membership.memberid
FROM Membership, Package
WHERE membership.PackageId = Package.Id
AND Package.PackageTypeId != 1
AND membership.memberid in (select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64')
Group by membership.memberid
) as b
)
) as t
) as Total
) ORIGINAL
so I'm trying to do simple division and of course SQL, being super logical that it is, makes it impossible. What I am trying to do is this:
SELECT * FROM
1 - ((SELECT COUNT(DISTINCT T.DID) FROM TGAMAZING T AS NUM) * 100 /
(SELECT COUNT(DISTINCT D.ID) FROM DIRECTORS D AS DENOM))
but how would I write this SQL (Oracle)?
Oracle
SQL Fiddle: http://sqlfiddle.com/#!4/34298/8
Method 1:
SELECT 1 - (COUNT(DISTINCT DID) * 100 / COUNT(DISTINCT ID))
FROM TGAMAZING
cross join DIRECTORS;
Method 2:
SELECT 1 -
(
(SELECT COUNT(DISTINCT DID) FROM TGAMAZING) * 100 /
(SELECT COUNT(DISTINCT ID) FROM DIRECTORS)
)
FROM DUAL;
SQL Server
SQL Fiddle: http://sqlfiddle.com/#!6/34298/3
Method 1
SELECT 1 - (COUNT(DISTINCT DID) * 100.0 / COUNT(DISTINCT ID))
FROM TGAMAZING
cross join DIRECTORS;
Method 2
SELECT 1 -
(
(SELECT COUNT(DISTINCT DID) FROM TGAMAZING) * 100.0 /
(SELECT COUNT(DISTINCT ID) FROM DIRECTORS)
)
Write the calculation as it were fields and use "FROM DUAL", as you don't want to query any of your tables.
SELECT
1 - ((SELECT COUNT(DISTINCT T.DID) FROM TGAMAZING T) * 100 /
(SELECT COUNT(DISTINCT D.ID) FROM DIRECTORS D))
AS RESULT
FROM DUAL
Reference: Selecting from the DUAL Table.
I am running an Oracle query. It seems to work except that it returns 4 duplicates of each result. Here is the query:
Select * from (
Select a.*, rownum rnum From (
SELECT NEW_USER.*, NEW_EHS_QUIZ_COMPLETE.datetime
FROM NEW_USER, NEW_EHS_QUIZ_COMPLETE
WHERE EXISTS (
select *
from NEW_EHS_QUIZ_COMPLETE
where NEW_USER.id=NEW_EHS_QUIZ_COMPLETE.USER_ID
)
ORDER by last_name ASC
) a
where rownum <= #pgtop#
)
where rnum >= #pgbot#
Does anyone know why this isn't working properly?
You have a cross join here:
SELECT
NEW_USER.*,
NEW_EHS_QUIZ_COMPLETE.datetime
FROM NEW_USER, NEW_EHS_QUIZ_COMPLETE
WHERE EXISTS(
select * from NEW_EHS_QUIZ_COMPLETE
where NEW_USER.id=NEW_EHS_QUIZ_COMPLETE.USER_ID
)
You probably mean this:
SELECT
NEW_USER.*,
NEW_EHS_QUIZ_COMPLETE.datetime
FROM NEW_USER
INNER JOIN NEW_EHS_QUIZ_COMPLETE
ON NEW_USER.id = NEW_EHS_QUIZ_COMPLETE.USER_ID
I am trying to fetch the sum of several counts in one query:
SELECT(
SELECT COUNT( * )
FROM comments +
SELECT COUNT( * )
FROM tags +
SELECT COUNT( * )
FROM search
)
I am missing something here. I get syntax error.
SELECT ( SELECT COUNT(*) FROM comments )
+ ( SELECT COUNT(*) FROM tags )
+ ( SELECT COUNT(*) FROM search )
One more (not sure if supported with MySQL, though - works in SQL Server):
SELECT SUM(Counts) FROM
(SELECT COUNT(*) AS Counts FROM COMMENTS UNION ALL
SELECT COUNT(*) FROM Tags UNION ALL
SELECT COUNT(*) FROM Search) s
SELECT (
SELECT COUNT(*)
FROM comments
) +
(
SELECT COUNT(*)
FROM tags
) +
(
SELECT COUNT(*)
FROM search
)
SELECT SUM(ThisCount)
FROM (
SELECT COUNT(*) AS ThisCount
FROM comments
UNION ALL
SELECT COUNT(*) AS ThisCount
FROM tags
UNION ALL
SELECT COUNT(*) AS ThisCount
FROM search
)