Building complex SQL command - sql

I am using sql for select, update, insert and some other features, but only simple ones until now. Now I need to write a complex command.
I have looked at using case but I do not know how to implement it.
This is what it should look like:
SELECT KVIZ_ISTORIJA.ID AS ISTORIJAID, KVIZ_PITANJA.PITANJE1, ~TACNO~
FROM KVIZ_ISTORIJA
INNER JOIN KVIZ_PITANJA ON KVIZ_ISTORIJA.PITANJEID = KVIZ_PITANJA.PITANJEID
WHERE REZULTATID = 12
I used ~TACNO~ to point out where I need a conditional value.
How I would write the condition in C# is like this (I will use column names from table as variables):
int ~TACNO~ = -1;
int I = -1;
if(KVIZ_PITANJA.ODGOVOR1_TACAN == 1)
I = 1;
else if(KVIZ_PITANJA.ODGOVOR2_TACAN == 1)
I = 2;
else if(KVIZ_PITANJA.ODGOVOR3_TACAN == 1)
I = 3;
else if(KVIZ_PITANJA.ODGOVOR4_TACAN == 1)
I = 4;
else if(KVIZ_PITANJA.ODGOVOR5_TACAN == 1)
I = 5;
switch(I)
{
case 1:
if(KVIZ_ISTORIJA.ODGOVORENO1 = 1)
~TACNO~ = 1;
break;
case 2:
if(KVIZ_ISTORIJA.ODGOVORENO2 = 1)
~TACNO~ = 1;
break;
case 3:
if(KVIZ_ISTORIJA.ODGOVORENO3 = 1)
~TACNO~ = 1;
break;
case 4:
if(KVIZ_ISTORIJA.ODGOVORENO4 = 1)
~TACNO~ = 1;
break;
case 5:
if(KVIZ_ISTORIJA.ODGOVORENO5 = 1)
~TACNO~ = 1;
break;
}
How can I write the equivalent of this C# condition in SQL query?

You can use a searched CASE like this:
case
when (KVIZ_PITANJA.ODGOVOR1_TACAN = 1 and KVIZ_ISTORIJA.ODGOVORENO1 = 1) then 1
when (KVIZ_PITANJA.ODGOVOR2_TACAN = 1 and KVIZ_ISTORIJA.ODGOVORENO2 = 1) then 1
when (KVIZ_PITANJA.ODGOVOR3_TACAN = 1 and KVIZ_ISTORIJA.ODGOVORENO3 = 1) then 1
when (KVIZ_PITANJA.ODGOVOR4_TACAN = 1 and KVIZ_ISTORIJA.ODGOVORENO4 = 1) then 1
when (KVIZ_PITANJA.ODGOVOR5_TACAN = 1 and KVIZ_ISTORIJA.ODGOVORENO5 = 1) then 1
else -1
end
You could also do it as a single when clause, by joining the conditions using OR, but I think that is less readable:
case
when (KVIZ_PITANJA.ODGOVOR1_TACAN = 1 and KVIZ_ISTORIJA.ODGOVORENO1 = 1)
or (KVIZ_PITANJA.ODGOVOR2_TACAN = 1 and KVIZ_ISTORIJA.ODGOVORENO2 = 1)
or (KVIZ_PITANJA.ODGOVOR3_TACAN = 1 and KVIZ_ISTORIJA.ODGOVORENO3 = 1)
or (KVIZ_PITANJA.ODGOVOR4_TACAN = 1 and KVIZ_ISTORIJA.ODGOVORENO4 = 1)
or (KVIZ_PITANJA.ODGOVOR5_TACAN = 1 and KVIZ_ISTORIJA.ODGOVORENO5 = 1) then 1
else -1
end
But as suggested by Arioch'The in the comments: consider normalizing your design, so you don't have repeating columns in a single table, but instead multiple rows with a discriminator column. It would simplify things like this.

Related

Cyclomatic Complexity of multiple if statements

if (k = = 1)
r + = a;
else if (k = = 2)
r + = b;
else if (k = = 3)
r + = c;
else
r + = d;
switch (k) {
case 1:
r + = a;
break;
case 2:
r + = b;
break;
case 3:
r + = c;
break;
default:
r + = d;
break;
}
I am trying to understand whether for both the multiple if statement and the switch case the sample control flow diagram is the below diagram. I am sure that it is true for the switch case but i am unable to draw one for the multiple if statement
The switch statement is nothing but syntactic sugar for the multiple if/else. The control flows are exactly the same. So is the cyclomatic complexity.

CASE expression in WHERE clause for diferent and

Can I use case expression to build where like this?
select *
from table
where
case
when x=y then z= j and t=v
when x=k then q= p and s=l
end
;
I need change where clause depending on the value of x variable.
Use or:
select *
from table
where (x = y and z = j and t = v) or (x = k and q = p and s = l);
An alternative to using OR is to use nested CASE statements:
SELECT *
FROM table_name
WHERE CASE
WHEN x = y THEN CASE WHEN z = j AND t = v THEN 1 ELSE 0 END
WHEN x = k THEN CASE WHEN q = p AND s = l THEN 1 ELSE 0 END
ELSE 0
END = 1;
or you could simplify it to:
SELECT *
FROM table_name
WHERE CASE
WHEN x = y AND z = j AND t = v THEN 1
WHEN x = k AND q = p AND s = l THEN 1
ELSE 0
END = 1;
However, you should check whether Oracle can use column indexes or if a separate function-based index is required with these solutions.

Rewrite subquery with calculation in SQL to CASE statement

I have, as a part of a bigger query, some subqueries that I would like to convert to CASE statements instead.
The subquery looks like this (and works):
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM [F0001].[dbo].[ProdTr] WHERE AcYrPr = '201601' AND ProdTr.TrTp = 1 AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1
However, I don't seem to find any logical way to put this into a CASE-statement.
Any help would be appreciated!
(
SELECT CASE
WHEN SUM(t1.DAm) <> 0
THEN (SUM(t1.DAm) + SUM(t1.StcCst)) * 100 / SUM(t1.DAm)
ELSE 0 /* or whatever you want to have in this case */
END AS 'DG'
FROM [F0001].[dbo].[ProdTr] t1
WHERE t1.AcYrPr = '201601' AND
t1.TrTp = 1 AND
t1.CustNo = '12773'
) AS dg_period_1
I also removed some unneeded parentheses and simplified an operation (x - (y * -1) = x + y)
You could use the following statement with CASE provided you want to return a null when SUM(DAm) is null or 0.
(SELECT CASE
WHEN SUM(DAm) IS NOT NULL and SUM(DAm) <> 0 THEN (((SUM(DAm) - (SUM(StcCst) * -1)) * 100) /SUM(DAm))
ELSE NULL
END AS 'DG'
FROM [F0001].[dbo].[ProdTr]
WHERE AcYrPr = '201601'
AND ProdTr.TrTp = 1
AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1

How to do this in Visual basic?

How to do the "a++" and "b++" in Visual basic?
What is the another codes for there in Vb?
The names there are just example.
int a = 0;
int b = 0;
{
if (ans1.Text == "James")
{
a++;
}
else
{
b++;
}
if (ans2.Text == "Ryan")
{
a++;
}
else
{
b++;
}
if (ans3.Text == "Mac")
{
a++;
}
else
{
b++;
}
t1.Text = a.ToString();
t2.Text = b.ToString();
}
Like this:
a += 1
b += 1
(...)
Like this
DIM a as integer = 0
DIM b as integer = 0
If ans1.Text = "James" Then
a += 1
Else
b += 1
End If
If ans2.Text = "Ryan" Then
a += 1
Else
b += 1
End If
If ans3.Text = "Mac" Then
a += 1
Else
b += 1
End If
t1.Text = a.ToString()
t2.Text = b.ToString()
Your question has already been answered but I think it would be useful to see how you could simplify your code:
Dim correctAnswers As Integer = 0
Dim totalQuestions As Integer = 3'you need to modify this is you add more questions
'increment the number of correct answers for each one we find
correctAnswers += If(ans1.text = "James", 1, 0)
correctAnswers += If(ans2.text = "Ryan", 1, 0)
correctAnswers += If(ans3.text = "Mac", 1, 0)
'show the number of correct and incorrect answers
t1.Text = correctAnswers.ToString()
t2.Text = (totalQuestions - correctAnswers).ToString() 'show the number of incorrect questions
Neither the postfix nor prefix ++ are defined in Visual Basic.
Your only realistic option is to use a = a + 1 (or, in later BASICs, a += 1) instead (note the lack of a ; for a statement terminator). But note that this will not evaluate to the previous value of a and the entire construct is not an expression in the C / C++ sense. You could build a function to mimic a++ but that would be too obfuscating.

Understanding case expression in the "Where" clause

I've got this code here and you can see from my Pseudocode what I'm trying to accomplish
select *
from dbo.BenefitsForms
inner join Dependents on BenefitsForms.UserId = Dependents.BenefitsForm_UserId
inner join CoverageLevels on BenefitsForms.MedicalId = CoverageLevels.Id
where (BenefitsForms.MedicalId > 0 AND BenefitsForms.MedicalId < 13)
AND Dependents.IsSpouse = CASE when CoverageLevels.[Level] = 2 then 1
when CoverageLevels.[Level] = 3 then 0 end
when CoverageLevels.[Level] = 4 then [any, it doesnt matter] <--- my desire but it doesn't work.
What can I do to get the effect I desire in the brackets? If Coverage Level = 4 then I don't care what Dependents.IsSpouse is, I don't even need to sort by it anymore.
Assuming that isSpouse can only be 0 or 1... if CoverageLevels.Level is 4, then compare isSpouse to itself, which will always result in true:
AND Dependents.IsSpouse = CASE
when CoverageLevels.[Level] = 2 then 1
when CoverageLevels.[Level] = 3 then 0
when CoverageLevels.[Level] = 4 then Dependents.IsSpouse
END
Alternately, this can also be expressed without the CASE:
WHERE
BenefitsForms.MedicalId > 0
AND BenefitsForms.MedicalId < 13
AND (
(Dependents.IsSpouse = 1 AND CoverageLevels.[Level] = 2)
OR (Dependents.IsSpouse = 0 AND CoverageLevels.[Level] = 3)
OR CoverageLevels.[Level] = 4
)