How run SELECT COUNT1/COUNT2

jal

Registered User.
Local time
Today, 09:42
Joined
Mar 30, 2007
Messages
1,709
I feel like a complete idiot. I used to be able to write simple queries but I haven't had much practice lately and now it seems I've completely forgotten how. If you type SELECT 100/20 in a query window it will return "5" (try it for yourself), and in that same vein I want to run SELECT Count1/Count2, meaning I have two Count(*) queries. Here's the first one:

Code:
(Select COUNT(*) as TotalDownloaded FROM FilesToDownload WHERE Jobname LIKE 'Optum*')

This returns for me a count of 3251. Works fine. The second count is this (it also works fine),

Code:
(
SELECT COUNT(*) AS NumDays FROM
(
SELECT DISTINCT DepositDate FROM FilesToDownload WHERE JobName LIKE 'Optum*'

)
)

Now how do I divide Count1/Count2 ??? I was hoping to do it all in one query and can't figure out how to get the division worked in. I tried using the slashmark to divide Count1/Count2 like this:
Code:
SELECT
 
(Select COUNT(*) as TotalDownloaded FROM FilesToDownload WHERE Jobname LIKE 'Optum*') 
 
/
 
 
 
(
SELECT COUNT(*) AS NumDays FROM
(
SELECT DISTINCT DepositDate FROM FilesToDownload WHERE JobName LIKE 'Optum*'
)
)
but I get the error: "Query input must contain at least one table or query."
 
I've got a solution although i would think there was easier way. I pretended that the first Count-query returns a table, and the same with the second query. To each of these two tables (one value as a table) I added a second value (the number 1) so that I could join the two tables on this value, the syntax was "1 as JoinValue". The join produced a table containing both count1 and count2 allowing me to write count1/count2.

Code:
 
SELECT Count1/Count2 as theAverage FROM
(
Select COUNT(*) as Count1, 1 as JoinValue FROM FilesToDownload WHERE Jobname LIKE 'Optum*'
) as CountFiles
INNER JOIN

(
SELECT COUNT(*) AS Count2, 1 as JoinValue FROM
(
SELECT DISTINCT DepositDate FROM FilesToDownload WHERE JobName LIKE 'Optum*'

) 
) as CountDays
ON CountDAys.JoinValue = CountFiles.JoinValue
 
I guess the more concise way to do it is a scalar subquery:


Code:
Select COUNT(*)/
(SELECT COUNT(*) FROM
(
SELECT DISTINCT DepositDate FROM FilesToDownload WHERE JobName LIKE 'Optum*'
)) 
FROM FilesToDownload WHERE Jobname LIKE 'Optum*'

I wonder whether the join method is faster? Dunno.
 

Users who are viewing this thread

Back
Top Bottom