How to sum values on each row with condition? - sql

So, basically, I want to update my table by summing values on each row, but I don't want to sum negative values.
Here is some simple code that I've tried:
UPDATE recipe SET total_time = preparation_time + cooking_time;
But sometimes, preparation_time or cooking_time is equal to -1 and when it happens I won't to parse them to be equal to 0. How can i do that?

You can use case expressions:
SET total_time = ((case when preparation_time > 0 then preparation_time else 0 end) +
(case when cooking_time > 0 then cooking_time else 0 end)
)

Related

way to have query with case statement

I have table called Numbers in that column I have values from 0 - 10 but I like to keep value of 1-10 only change record of 0 too null
Case numbers
when 0
then ''
but I found this has changed all values and not values that have 0 is there way I can say else leave value as is?
Do you want this?
update t
set number = null
where number = 0;
Or as a select:
select t.*,
(case when number <> 0 then number end)
from t;
SELECT CASE WHEN [column] = 0 THEN NULL ELSE [column] END AS [SomeName]
FROM Numbers

returning the column values as a list or else 0

I had a query where i am trying to get the results of a query, the query can have multiple rows or it can be empty, i am trying if it is empty, it should return me 0 for a column i am looking which is called as sequence
My query is like this:
select CASE WHEN COUNT(1) > 0 THEN 1 ELSE 0 END AS Sequence
from dbo.mytable
it returns me the either 1 or 0, for 1 i want that column should return me values or it should combine all the rows and return me the value of that column as list like 1,2,3,4,5,6,7
This should work.
SELECT
CASE WHEN MY_COUNT > 0 THEN 1 ELSE 0 END AS SEQUENCE
FROM
(SELECT COUNT(*) AS MY_COUNT
FROM
DBO.MYTABLE);
If you want only one row in the result set, simply do:
select (case when count(*) > 0 then 1 else 0 end) as sequence
from mytable;
If you care at all about performance, the more efficient method is:
select (case when exists (select 1 from dbo.mytable) then 1 else 0
end) as sequence

How to add multiple case in sql

I want to add multiple cases in single line. In other words in below example if first item is NOT NULL and second item is equal to 1 then output should be 1 else 0. How can I do it?
MatchbyCatalog= (case when ISNULL(pc.ProductID,0) and tc.RawMatch=1) then 1 else 0 end)
Is this what you want?
CASE WHEN pc.ProductID IS NULL and tc.RawMatch=1 THEN 1 ELSE 0 END
You misunderstood ISNULL() , it's a function that replaces the value if it's NULL, it's not a condition . You were looking for IS NULL
That's all you need.
MatchbyCatalog= (case when ISNULL(pc.ProductID,0)=1 and tc.RawMatch=1 then 1 else 0 end)
when you say both I think you mean pc.ProductID and tc.RawMatch...
I have not you're entire sql query so I have to guess.
You have multiple solutions :
Nearly ss you have wrote when pc.ProductId = 1 and tc.RawMatch=1 then 1 else 0 :
MatchbyCatalog= (case when ISNULL(pc.ProductID,0)=1 and tc.RawMatch=1) then 1 else 0 end)
You could also use a trick : 1 * 1 should be 1 and 1 * 0 =0 so you could write (if tc.Rawmatch is a boolean sort of with values 0 or 1) :
MatchbyCatalog= ISNULL(pc.ProductID,0) * tc.RawMatch
case when {Condition} then {result}
When {Condition} then {result}
Else {result}

query sql returning custom value

I have a table with 3 columns (containing Integer values that assume only values from 0 to 10). I want extract, with a single query, a table with 1 column. This column must assume a value based on the following logic:
If one of these three columns has value 0 ----> the value of column of table generated by query must be 0 too.
If none of the last three columns has value 0 ----> the value of column must assume the value 1.
You are looking for CASE construct or IF function:
SELECT CASE WHEN (t.field1 = 0 OR t.field2 = 0 OR t.field3 = 0) THEN 0
ELSE 1 END AS value
FROM t;
In this specific case you might also use the fact that any member being zero will zero the product:
SELECT CASE WHEN (t.field1*t.field2*t.field3 = 0) THEN 0 ELSE 1 END AS value
FROM t;
Or
SELECT IF((t.field1*t.field2*t.field3)=0, 0, 1) AS value FROM t;
This is a simple case statement. Assuming there are no NULL values, try this:
select (case when col1 = 0 or col2 = 0 or col3 = 0 then 0 else 1 end)
Try this
SELECT
CASE
WHEN column1 = 0 THEN 0
WHEN column2 = 0 THEN 0
WHEN column3 = 0 THEN 0
ELSE 1
END
FROM urtable

SQL query to test my programming capabilities in different way

I have one table and one column in it. There is 15 rows (integers). I want to count
the positive numbers and negative numbers, and also sum of total numbers in one query.
Can any one help me?
Or...
SELECT
COUNT(CASE WHEN Col > 0 THEN 1 END) AS NumPositives,
COUNT(CASE WHEN Col < 0 THEN 1 END) AS NumNegatives,
SUM(Col) AS Tot
FROM TableName;
Or you could consider using SIGN(Col), which gives 1 for positive numbers and -1 for negative numbers.
I'll give you psudeo code to help you with your homework.
3 aggregates:
SUM
SUM (CASE < 0)
SUM (CASE > 0)
select (select sum(mycolumn) from mytable where mycolumn > 0) as positive_sum,
(select sum(mycolumn) from mytable where mycolumn < 0) as negative_sum,
sum(mycolumn) as total_sum
from mytable
Try this
SELECT SUM(CASE WHEN Col > 0 THEN 1 ELSE 0 END) AS Pos,
SUM(CASE WHEN Col < 0 THEN 1 ELSE 0 END) AS Neg,
SUM(Col) AS Tot
FROM Table