how to calculate prevalence with given data [duplicate] - sql

This question already has answers here:
how to calculate prevalence using sql code
(3 answers)
Closed 3 years ago.
I have a dataset called 'disease' and I would like to calculate prevalence of certain disease using given dataset.
I have two column 'disease_id' and 'person_id' and I know there are total number of samples are 1453477 and I am going to filter certain disease using 'where' statement to 'disease_id'.
As I know the total number of sample which is 1453477 and let say I want to calculate prevalence for people who has diabetes and there is 851415 people who applies to that condition.
so 851415/1453477=0.58577 but when I run the query below I am keep getting 1 for the answer.
select count(disease__id) / count(person_id) as prevalence
from disease
where disease_id=12345;
I know that disease_id for diabetes is 12345 and there is 851415 people who are diabetes so 'count(disease_id)' should be 851415 and 'count(person_id)' should result 1453477.
can some one please help me on this?

Use this.
select
cast (
(
select
count(disease__id)
from
disease
where
disease_id = 12345
) as decimal
)
) / (select count(person_id) from disease)

Related

I want to collect duplicated values in SQL and convert them to a number [duplicate]

This question already has answers here:
How to use count and group by at the same select statement
(11 answers)
Closed 6 months ago.
I want to count a certain value in a column and output it as a number.
Here is an example:
id
job
1
police
2
police
3
ambulance
Now I want to count the value "police" in the "job" column and make the result in a number, so because there are two entries with "police" in the column it would be as output the number two. With the value "ambulance" it is only one entry so the result would be 1.
Can anyone tell me how to write this as code?
I have now searched a lot on the Internet and tried myself but I have found nothing that worked.
You're saying you want to count how many of each type of job there is, right?
SELECT COUNT(*), job
FROM tablename
GROUP BY job

how to manipulate string column of numbers separated by dots using t-sql [duplicate]

This question already has answers here:
How Can I Sort A 'Version Number' Column Generically Using a SQL Server Query
(4 answers)
Closed 5 years ago.
I have a column of nvarchar(255) type that represents software version
numbers:
VersionNumber
---------------
1.0.0.505
1.0.0.506
1.0.0.507
1.0.0.508
1.0.0.509
1.0.1.2
I need to extract the maximum version number (the min version number in the example above is 1.0.0.505 and the max version number is 1.0.1.2, values arranged from the smallest to the highest).
in order to explain exactly what i need - if i could use imperative programming language i think i would do something like that to detect the max version number:
lets say version number is d.c.b.a.
i would separate each version number to four different variables: a b
c d
that i will sum each series.
a will be summed by tens
b will be summed by hundreds
c will be summed by thousands
d will be summed by milions
than the maximum total sum of each Max(a+b+c+d) will be the max version.
but what is the technic to achieve something like that in sql?
for future readers: based on #AlexK. link that is the solution:
select TOP 1 VersionNumber from Users order by (cast('/' + replace(VersionNumber , '.', '/') + '/' as hierarchyid)) DESC;
try this
select max(replace(version,'.','')) from yourtable

Very special join on float value

I got stuck with a tricky query (in MS Access 2013). I'd like to do a fairly simple Thing:
I have got two Tables (see example below): Table "scores" with scores of an exam and table "grading_key".
The scores table has a field named "quotient" which contains a float value representing the percentage of success (1.0 being all questions answered correctly). The grading_key table has quotient limits which separate one grade level from the next. Thus the “grading_key” table can be used to get a grade for any quotient value.
A grade can be found by performing:
SELECT TOP 1 Grade FROM grading_key WHERE {ANY_QUOTIENT_VALUE} <= Quotient
Sample Tables:
|-grade_key-| |-----scores-----|
Quotient Grade StudentId Quotient
0,92 1 123 0,85
0,87 1,5 321 0,8
0,81 2 766 0,91
0,76 2,5 222 0,78
My Problem is, I’d like to join scores and grades in a query resulting in associating each quotient in table “scores” with one grade in table “grade_key” (see desired_result below). Unfortunately I can’t simply join, as the quotients in “scores” do not necessarily match the grade limits defined in “grade_key”.
Currently I used a VBA function (calculateScoreForQuotient()) but I want to remove the VBA dependency as the resulting table should be called from outside MS Access and in this case VBA functions do not work.
|--------Desired_Result-------|
StudentId Quotient Grade
123 0,85 2
312 0,8 2,5
Does anyone know a way to get desired table with plain SQL? I played around with different combinations of JOINs and and WHEREs for quite a while now but my best result was to associate all available grades with each student (not really meaningful).
Any help would safe my day ;-)
You can use a co-related sub-query to return the grade based on the quotient of the student. You could use Max() or TOP 1 with an order by clause, whichever you prefer.
select
StudentID,
Quotient,
(select Max(grade) from Grades where grades.quotient <= student.quotient) as grade
from Student

Counting totals and partials

I have a table answers in a PostgreSQL engine powered database, with a number associated to a question. I need to count how many numbers are there and how many of them are below 6 grouped by question.
What I want is something like:
SELECT question, count(*) AS Qanswers, count_below_6(*) AS Qanswers_below_6
FROM answers
GROUP BY question;
question | Qanswers | Qanswers_below_6
-----------------------+----------+------------------
How do you feel? | 1234 | 53
Was clear the webinar? | 8444 | 20
How much that hurt? | 3666 | 142
Currently I'm doing
SELECT question, count(*) AS Qanswers
FROM answers
GROUP BY question;
And then
SELECT question, count(*) AS Qanswers
FROM answers
WHERE value < 6
GROUP BY question;
After that I merge the two results manually.
Can I make a single sentence that gives me the result as I need?
SELECT question
,count(*) AS answers
,count(value < 6 OR NULL) AS under_6
FROM answers
GROUP BY question;
The trick is that count(expression) only counts non-null values (while count(*) counts all rows). Here is a detailed explanation why we get NULL in the right places:
Compute percents from SUM() in the same SELECT sql query
There is a number of equivalent techniques to achieve that. Detailed comparison with a benchmark:
For absolute performance, is SUM faster or COUNT?

Need MDX query for total count

Last time I had posted a question which was a bit confused. But today I got the same question from my manager to get the MDX query.
Here is the scenario:
Fact Table
Ticket No / Ticket ID
1 S
2 S
3 S
3 D
4 D
Dimension Table
Ticket ID / [Count]
S 1
D 1
My manager said they are not using dimension table that they are not using just for understanding they have mentioned that. there is no use of considering it here.
So please ignore the Dimension table data.
The Output will be like this if we do group by based on Ticket ID:
Ticket ID / [Count]
S 3
D 2
If we do so we will get the total Count is
5
But I need the total count as 4 based on Ticket No.
Need help here.
Thanks in Advance.
My educated guess is that you're starting with OLAP/MDX. It's worth taking a bit of time reading in the web about MDX, something like MDX Gentle Tutorial.
Without a dimension you can not have a cube. The minimum is one dimension and one measure in your facts.
In your case
Ticket ID -> dimension with two possible values (S,D)
Ticket No -> the measure, as Aaron pointed out. use unique count as aggregation type.
The MDX would looks like :
Select
{[Ticked ID].allmembers} on 0,
{[Measures].members on 1
from [MyCube]
We could find other ways of solving this but they would be certainly slower and more complicated.