Using calculated column within SQL query - sql

I need help with a subquery. I think I’m close but not quite sure. I want to use a calculated column within another calculation. In the example want to use “numerator” in the calculation of the “denominator”. I’m new to subqueries.
SELECT
sum(case when denominator=1 and q10=1 then 1 else 0 end) as numerator
FROM (
SELECT sum(case when q5=1 and q6=1 then 1 else 0 end) as denominator
FROM datasource
q5
q6
q10
1
1
0
0
1
0
0
0
1
I want to calculate a “denominator” column where it’s 1 if q5 and q6 are equal to 1 or else “denominator”=0. Then calculate a “numerator” column if the calculated denominator column is 1 and q10 is 1 then “numerator”=1 or else “numerator”=0. You can ignore the sum command cause I just wanted to sum the columns at the end but for the purpose of the question it isn’t important
Update…
I tried the below query as suggested and get the error “Expression not in GROUP BY key”
SELECT sum(case when denominator=1 and q10 = 1 then 1 else 0 end) as numerator
FROM (
SELECT q10,
sum(case when q5=1 and q6=1 then 1 else 0 end) as denominator
FROM datasource) t

I haven't added the aggregation part, but I think this will get you close to what you are trying to do.
SELECT case when denominator=1 and q10 = 1 then 1 else 0 end as numerator
FROM (
SELECT q10,
case when q5=1 and q6=1 then 1 else 0 end as denominator
FROM datasource
GROUP BY q10) t

Related

Sql loop over unique elements of a column

I am trying to get data of rows into columns. This is my data before query.
This is my query.
SELECT
Date,
SUM(CASE WHEN (Terms="Art") THEN 1 ELSE 0 END) AS Art,
SUM(CASE WHEN (Terms="Action") THEN 1 ELSE 0 END) AS Action,
SUM(CASE WHEN (Terms="Board") THEN 1 ELSE 0 END) AS Board,
SUM(CASE WHEN (Terms="Puzzle") THEN 1 ELSE 0 END) AS Puzzle,
SUM(CASE WHEN (Terms="Adventure") THEN 1 ELSE 0 END) AS Adventure,
SUM(CASE WHEN (Terms="Others") THEN 1 ELSE 0 END) AS Others
FROM
__table__
GROUP BY
Date
After Query this is my data.
Which is fine and intended data. But problem is in query as you can see I have to explicitly write each term. I want to automate that using loop. Theoretical solution is to write subquery to get all unique terms and then loop over them. But I don't know how.

SQL using SUM in CASE in SUM

I had this query select
sum(CASE WHEN kpi.average >= temp.average THEN 1 ELSE 0 END) AS recordOrder,
which worked fine, but I had to change it to this
sum(CASE WHEN sum(kpi.averageUse) / sum(kpi.averageTotal) >= temp.average THEN 1 ELSE 0 END) AS recordOrder,
These queries have to get number of rows, where some value (average) is greater than average from TEMP table. But in the second query I have more accurate data (weighted average).
but I am getting error
1111 invalid use of group function
Any ideas how to write SUM in CASE in SUM?
Thanks!
This code is just non-sensical because you have nested sum functions:
sum(CASE WHEN sum(kpi.averageUse) / sum(kpi.averageTotal) >= temp.average THEN 1 ELSE 0 END) AS recordOrder,
Without seeing your larger query, it is not possible to know what you really intend. But I would speculate that you don't want the internal sum()s:
sum(CASE WHEN (skpi.averageUse / kpi.averageTotal) >= temp.average THEN 1 ELSE 0 END) AS recordOrder,

Use a 'in' statement inside a 'case' expression

How can I use IN in my "case expression"?
This is my query where I want count if any match like mumbai found then as mumbai:
select TYPEOFPRODUCT,
SUM(CASE(BRANCH) WHEN IN('Mumbai1' OR 'Mumbai2') THEN 1 ELSE 0 END) AS 'Mumbai'
SUM(CASE(BRANCH) WHEN IN('Delhi1' OR 'DelhiMain') THEN 1 ELSE 0 END) AS 'Delhi'
from los_ext
group by TYPEOFPRODUCT
You were close, you just need to comma separate values for IN rather than using OR, and there are two formats for CASE expressions, putting the field with the criteria after WHEN is more flexible, so I prefer to use it exclusively:
SELECT TYPEOFPRODUCT
,SUM( CASE WHEN BRANCH IN('Mumbai1','Mumbai2') THEN 1 ELSE 0 END) AS Mumbai
,SUM( CASE WHEN BRANCH IN('Delhi1', 'DelhiMain') THEN 1 ELSE 0 END) AS Delhi
FROM los_ext
GROUP BY TYPEOFPRODUCT
Its query select sum of individual branchs
select TYPEOFPRODUCT,
SUM(CASE WHEN (BRANCH = 'Mumbai1' OR BRANCH = 'Mumbai2' ) THEN 1 ELSE 0 END) AS 'Mumbai',
SUM(CASE WHEN (BRANCH = 'Delhi1' OR BRANCH = 'DelhiMain' ) THEN 1 ELSE 0 END) AS 'Delhi'
from los_ext
group by TYPEOFPRODUCT

SQL percentage with rows same table with different where condition

I want to do a query like:
select
count(asterisk) where acción='a'/count(asterisk) where acción='b' * 100
from
same_table
grouped by day
but I don't want use subquery, is it possible with joins?
I`m not sure the syntax is correct, but you can use something like this:
SELECT day,
SUM(CASE WHEN "acción" = 'a' THEN 1 ELSE 0 END) AS SUM_A,
SUM(CASE WHEN "acción" = 'b' THEN 1 ELSE 0 END) AS SUM_B,
SUM(CASE WHEN "acción" = 'a' THEN 1 ELSE 0 END) AS SUM_A / SUM(CASE WHEN "acción" = 'b' THEN 1 ELSE 0 END) * 100 AS result
FROM your_table
GROUP BY day
The concept is to actually sum the the values that you need, instead of count.

Choosing one value from partially duplicate rows

I am trying to choose between two rows of data in order to get a total count of type.
Table - Evaluations
EvaluationID (link to minEval_ID and max_EvalID)
EstablishedDelays
Table - Outcome
min_EvalID
max_EvalID
EligTypeRecalc
This is what my current query is:
SELECT "NewEligType"=
COUNT (*),
SUM (CASE WHEN a.EligTypeRecalc IN (1,4,5,7) Then 1 Else 0 END) 'Established Condition',
SUM (CASE WHEN a.EligTypeRecalc=6 Then 1 Else 0 END) 'Established Delay & At-Risk',
SUM (CASE WHEN a.EligTypeRecalc=2 and b.EstablishedDelays=1 Then 1 Else 0 END) 'Established Delay only: One Delay',
SUM (CASE WHEN a.EligTypeRecalc=2 and b.EstablishedDelays=2 Then 1 Else 0 END) 'Established Delay only: Two Delays',
SUM (CASE WHEN a.EligTypeRecalc=2 and b.EstablishedDelays>=3 Then 1 Else 0 END) 'Established Delay only: Three+ Delays',
SUM (CASE WHEN a.EligTypeRecalc=8 Then 1 Else 0 END) 'Clinical Judgement'
from Outcome a
join Evaluations b
on a.max_EvalID=b.evaluationid and a.min_evalID=b.evaluationID
where a.EligTypeRecalc<>3
The problem I'm encountering is picking the correct Eval_ID to choose the correct number of delays and not count the other. The EstablishedDelays associated with max_EvalID is correct unless the EligTypeRecalc is 0, then it should count the delays associated with min_EvalID.
So far I've come up with this basic logic but I'm having a mind block on how to get it to the next step:
CASE WHEN EligTypeRecalc=max_EvalID
THEN EstablishedDelays=max_EvalID
ELSE EstablishedDelays=min_EvalID
Bonus points: I only have Read access to the database.
**What's the proper query syntax to use to select the row associated and exclude the other?
Thanks for your advice!
You might try wrapping your summary of the Evaluations table into a CTE and then join that to the Outcome table.
Here is the code I used, its a little long:
WITH min_eval AS
(
SELECT
a.DataMatch,
a.max_EvalID,
a.EligTypeRecalc,
b.evaluationid,
b.EstablishedDelays
FROM Outcomes a
JOIN Evaluations b
ON a.min_EvalID=b.evaluationid
WHERE a.EligTypeRecalc<>3
),
max_eval AS
(
SELECT
a.DataMatch,
a.max_EvalID,
a.EligTypeRecalc,
b.evaluationid,
b.EstablishedDelays
FROM Outcomes a
JOIN Evaluations b
ON a.max_EvalID=b.evaluationid
WHERE a.EligTypeRecalc<>3
)
SELECT "NewEligType"=
COUNT (*),
SUM (CASE WHEN a.EligTypeRecalc IN (1,4,5,7) THEN 1 ELSE 0 END) 'Established Condition',
SUM (CASE WHEN a.EligTypeRecalc=6 THEN 1 ELSE 0 END) 'Established Delay & At-Risk',
SUM (CASE WHEN (a.EligTypeRecalc=2 AND a.EstablishedDelays=1) OR (a.EligTypeRecalc=2 AND a.EstablishedDelays=0 AND b.EligTypeRecalc=2 AND b.EstablishedDelays=1) THEN 1 ELSE 0 END) 'Established Delay only: One Delay',
SUM (CASE WHEN (a.EligTypeRecalc=2 AND a.EstablishedDelays=2) OR (a.EligTypeRecalc=2 AND a.EstablishedDelays=0 AND b.EligTypeRecalc=2 AND b.EstablishedDelays=2) THEN 1 ELSE 0 END) 'Established Delay only: Two Delays',
SUM (CASE WHEN (a.EligTypeRecalc=2 AND a.EstablishedDelays>=3) OR (a.EligTypeRecalc=2 AND a.EstablishedDelays=0 AND b.EligTypeRecalc=2 AND b.EstablishedDelays>=3) THEN 1 ELSE 0 END) 'Established Delay only: Three+ Delays',
SUM (CASE WHEN a.EligTypeRecalc=8 THEN 1 ELSE 0 END) 'Clinical Judgement'
FROM max_eval a
JOIN min_eval b
ON a.DataMatch=b.DataMatch