2008R2 SQL Code for case when using the SUM? - sql-server-2008

How do I code the PASS or FAIL using the case when statement when sum the total?
If score is 46, then pass. If equal or less than 45, then fail.
sum(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3) as CScore,
--Fail??
sum(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)/46 as CPASS

SELECT
CASE
WHEN sum(ISNULL(qa.scripting1,0)+ISNULL(qa.conduct1,0)+ISNULL(qa.conduct2,0)+ISNULL(qa.conduct3,0) >= 45 THEN "PASS"
WHEN sum(ISNULL(qa.scripting1,0)+ISNULL(qa.conduct1,0)+ISNULL(qa.conduct2,0)+ISNULL(qa.conduct3,0) <= 44 THEN "FAIL"
END
FROM Data ;

Try this:
Case sum(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3) when > 45 then 'pass' else 'fail' end

Related

MYSQL case then update

I am creating a MYSQL case, where i want to say:
if age in fieldname "age" = over 20 years old
then update the field "points" to 15 (example value)
It does not accept the "update" statement after the then. Is there another way to do it?
It is a "case" statement from dropdown boxes, so i cannot put too many "if else" statements.
Fieldnames are age, score:
SELECT `FieldName`, `FieldValue`,
CASE WHEN `FieldValue` = "over 20 years old"
THEN (UPDATE `table`
SET `FieldValue`= 15
WHERE `FieldName` = 'points')
When ...(if age less than 20, update points to 10)...
ELSE something else
END
FROM table where `FieldName` = 'age'
UPDATE,
Something is missing, it runs but not updating the prices:
UPDATE `TABLE` SET `FieldValue` = CASE
WHEN `FieldValue` = 'over 20' THEN 5
When `FieldValue` = 'less than 20' THEN 10
ELSE `FieldValue` = 3
END WHERE`FieldName` = 'points'
try this:
UPDATE table SET points=IF(age>20,15,10)

Convert searched case to simple case else

I've the following code
select * from weeks
where case
when wet>1000 then wenumber=1
when wet>500 then wenumber=2
when wet>100 then wenumber=3
else wenumber= 22
end
it gives me the result of both ( when > 1000 and else )
through my search I understood that it is search case, but
I need to make it a simple case
simply if the first condition were true stop evaluating "else" statement
any help
Better way of doing it
select *
from weeks
where ( wet>1000 and wenumber = 1 ) or wenumber = 22
Try this:
SELECT *
FROM weeks
WHERE wenumber = CASE WHEN wet > 1000 THEN 1
WHEN wet BETWEEN 500 AND 1000 THEN 2
WHEN wet BETWEEN 100 AND 500 THEN 3
ELSE 22
END;
You need to specify which column should be compared with result of CASE like:
SELECT *
FROM weeks
WHERE wenumber = (CASE
WHEN wet > 1000 THEN 1
ELSE 22
END)

What's the best way to optimize the case statements?

For example..
I have three case statements, the values of which i want to save it in a variable..
Select
Case when 1 then variable='123' else
case when 2 then variable='456' else
case when 3 then variable='123456'
from
table X where
some conditions ;
try this for example
Select x.field,
Case x.value
when 1 then '123'
when 2 then '456'
when 3 then '123456'
ELSE '789'
END AS variable
from
table X where
some conditions ;

How can I use "OR" condition in MySQL CASE expression?

I have a procedure that contains CASE expression statement like so:
BEGIN
....
WHILE counter < total DO
....
CASE ranking
WHEN 1 OR 51 OR 100 OR 167 THEN SET
project_name = 'alpha';
WHEN 2 THEN SET
project_name = 'beta';
WHEN 10 OR 31 OR 40 OR 61 THEN SET
project_name = 'charlie';
....
ELSE SET
project_name = 'zelta';
END CASE;
INSERT INTO project (id, name) VALUES (LAST_INSERT_ID(), project_name);
SET counter = counter + 1;
END WHILE;
END
$$
DELIMITER ;
When I call the above procedure, cases with OR statements are either skipped completely or only the first item in the list is matched. What am I doing wrong?
CASE ranking
WHEN 1 THEN 'alpha'
WHEN 2 THEN 'beta'
WHEN 10 THEN 'charlie'
ELSE 'zelta'
END CASE;
You can use one of expresions that WHEN has, but you cannot mix both of them.
1) WHEN when_expression
Is a simple expression to which input_expression is compared when the simple CASE format is used. when_expression is any valid expression. The data types of input_expression and each when_expression must be the same or must be an implicit conversion.
2) WHEN Boolean_expression
Is the Boolean expression evaluated when using the searched CASE format. Boolean_expression is any valid Boolean expression.
You could program:
1)
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
2)
CASE
WHEN ListPrice = 0 THEN 'Mfg item - not for resale'
WHEN ListPrice < 50 THEN 'Under $50'
WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
ELSE 'Over $1000'
END
But in any case you can expect that the variable ranking is going to be compared in a boolean expresion.
http://msdn.microsoft.com/en-us/library/ms181765.aspx
you can use in to compare the values both numeric or character
CASE
WHEN ranking in(1,2,3) THEN '1Q'
WHEN ranking in(4,5,6) THEN '2Q'
ELSE '3Q'
END CASE;
CASE
WHEN ranking in('1','2','3') THEN '1Q'
WHEN ranking in('4','5','6') THEN '2Q'
ELSE '3Q'
END CASE;
this will also work in select statement and stored procedure also.
select case when month(curdate()) in (4,5,6) then 1 when month(curdate()) in (7,8,9) then 2 else 3 end as fiscal_quarter ;
This is also possible:
select (case when (var1 = 0 or var2 = 1) then 'x' else 'y' end)
from...

Use CodeIgniter Active Record to convert MySQL query

Can anybody convert the normal mysql query into codeigniter update strcuture.
UPDATE game_rounds
SET from_score = CASE WHEN from_id = 2 THEN 65 ELSE from_score END,
to_score = CASE WHEN to_id = 2 THEN 65 ELSE to_score END
WHERE round_id=5
I tried update_batch but not able to find correct solution.
Thanks in advance
$this->db
->set('from_score', 'CASE WHEN from_id = 2 THEN 65 ELSE from_score END', FALSE)
->set('to_score', 'CASE WHEN to_id = 2 THEN 65 ELSE to_score END', FALSE)
->where('round_id', 5)
->update('game_rounds');
$this->db->set() enables you to set values for inserts or updates.
It can be used instead of passing a data array directly to the insert or update functions.
The optional third parameter ($escape), that will prevent data from being escaped if set to FALSE.
CI Active Record Class
Here is a simple way to do this
$data['from_score'] = 'CASE WHEN from_id = 2 THEN 65 ELSE from_score END';
$data['to_score'] = 'CASE WHEN to_id = 2 THEN 65 ELSE to_score END ';
$this->db->where('round_id',5);
$this->db->update('game_rounds',$data);
After that run echo $this->db->last_query() to see if it is generating correct query string.