simple T-sql query - sql-server-2008

From the above sample data, i want to write a query that will return SalesAmount as 0.0 if ExpRow value = 1 and return ExpenseAmount as 0.0 if ExpRow value = 2

SELECT
ExpRow,
ExpenseDate,
CASE WHEN ExpRow = 2 THEN 0 ELSE ExpenseAmount END AS ExpenseAmount,
CASE WHEN ExpRow = 1 THEN 0 ELSE SalesAmount END AS SalesAmount
FROM
Table

Not sure if this is exactly what you are after but...
SELECT CASE([ExpRow]) WHEN 1 THEN 0 ELSE SalesAmount END AS 'SalesAmount',
CASE([ExpRow]) WHEN 2 THEN 0 ELSE ExpenseAmount END AS 'ExpenseAmount'
FROM [YOUR_TABLE]

Related

How to find 7 consecutive non zero values in SQL?

If we have a table of 15 columns, the first one is id, and the other columns have numeric
data type,
in MySQL or a similar languages, how can I find if the record have 7 consecutive non zero values, meaning that 7 of the contiguous columns have a value not equals to zero?
We can write a query has a lot of OR operations to check that each 7 columns don't have 0 value, but I want to know if there is shorter way
Try the following using CONCAT and INSTR functions, explanation is within query comments:
/*
check if the col value is not equal to 0,
this will return 1 if true and 0 if false,
and concatenate all of the returned values.
*/
with concat_cols as
(
select *,
concat(
col1<>0, col2<>0, col3<>0, col4<>0, col5<>0,
col6<>0, col7<>0, col8<>0, col9<>0, col10<>0,
col11<>0, col12<>0, col13<>0, col14<>0, col15<>0
) as con_col
from table_name
)
/*
using the instr function, check if the concatenated
result contains 7 consecutive 1s (7 not 0 values).
*/
select * from concat_cols
where instr(con_col, repeat('1', 7))
See demo
One way:
field(0,col1,col2,col3,col4,col5,col6,col7) = 0 or
field(0,col2,col3,col4,col5,col6,col7,col8) = 0 or
field(0,col3,col4,col5,col6,col7,col8,col9) = 0 or
field(0,col4,col5,col6,col7,col8,col9,col10) = 0 or
field(0,col5,col6,col7,col8,col9,col10,col11) = 0 or
field(0,col6,col7,col8,col9,col10,col11,col12) = 0 or
field(0,col7,col8,col9,col10,col11,col12,col13) = 0 or
field(0,col8,col9,col10,col11,col12,col13,col14) = 0 or
field(0,col9,col10,col11,col12,col13,col14,col15) = 0
SELECT id
FROM your_table
WHERE SUM(CASE WHEN column_2 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_3 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_4 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_5 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_6 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_7 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_8 > 0 THEN 1 ELSE 0 END) >= 7

i have been struck on this conversion

I have to convert this from MySQL to PostgreSQL:
IF(
IF(
MT.SLIPTYPE=8,
SUM(MT1.TOTAL),
0-SUM(MT1.TOTAL)
) IS NULL,
0.00,
IF(
MT.SLIPTYPE=8,
SUM(MT1.TOTAL),
0-SUM(MT1.TOTAL)
)
) AS Totalcamp
to a CASE statement. Can anyone help me?
I have tried like this, but it was not accepted:
case when MT.SLIPTYPE = 8
then
case when sum(MT1.TOTAL) is null then 0.00 else sum(MT1.TOTAL) end
else
case when (0 - sum(MT1.TOTAL)) is null then 0.00 else (0-SUM(MT1.TOTAL)) end
end as TOTALSCHEME
That should be straightforward:
CASE WHEN CASE WHEN MT.SLIPTYPE = 8
THEN SUM(MT1.TOTAL)
ELSE 0 - SUM(MT1.TOTAL)
END IS NULL
THEN 0.00
ELSE CASE WHEN MT.SLIPTYPE = 8
THEN SUM(MT1.TOTAL)
ELSE 0 - SUM(MT1.TOTAL)
END
END) AS Totalcamp
But it could be simplified considerably:
coalesce(
CASE WHEN MT.SLIPTYPE = 8
THEN SUM(MT1.TOTAL)
ELSE 0 - SUM(MT1.TOTAL)
END,
0.0
)

use select case in Mysql

The question I try to solve:
Return the value for "prefix" following these conditions:
If model_hyouji is a number and ranging from 0 to 9, set prefix = 0
If model_hyouji is a letter and ranging from a to z or A to Z, set prefix = 1
In all other cases, set prefix = 2
I used this SQL:
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case prefix
when 'a' then 1
when 'b' then 2
else 3
end
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
and I get this error:
Unknown column 'prefix' in 'field list'
assuming you column model_hyouji is varchar(1) (otherwise use substr(model_hyouji,1) )
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case
when model_hyouji between '0' and '9' then 0
when model_hyouji between 'a' and 'z' then 1
when model_hyouji between 'A' and 'Z' then 1
else 2
end prefix
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
or you can use regexp
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case
when model_hyouji REGEXP '^[0-9]{1}$' then 0
when model_hyouji REGEXP '^[A-Za-z]$' then 1
else 2
end prefix
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
SELECT
model_code,model_hyouji,model_name_prefix,model_kana_prefix,model_count,
case
when prefix = 'a' then 1 when 'b' then 2 else 3 end
You need CASE WHEN prefix =
But you also dont specify which prefix column you want to use hence the error on unknown column
E.G
SELECT
model_code,model_hyouji,model_name_prefix,model_kana_prefix,model_count,
case
WHEN model_kana_prefix = 'a' then 1
WHEN model_kana_prefix = 'b' then 2
else 3 end

Query to sum some values of a column with a condition

Having a table with this values:
name | executing | failed |
-------------------------------
task1 0 1
task2 1 0
task3 1 0
task4 0 0
With a query i want to get:
The total amount of executing task (2 in the example, task2 and task3)
The total amount of failed task (1 in the example, task1)
The total amount of pending task (those that are executing=0 and failed=0, 1 in the example, task4)
I can get the first two by uysing the following query:
SELECT IFNULL(SUM(executing), 0) Executing, IFNULL(SUM(failed), 0) Failed FROM mytable;
How can I expand my query so I can get another column with the sum of pending tasks?
Thanks in advance
Expected output:
executing | failed | pending
----------------------------
2 1 1
You don't specify how you want the results. I would do this as:
select (case when executing = 1 and failed = 0 then 'Executing'
when failed = 1 then 'Failed'
when executing = 0 and failed = 0 then 'Pending'
else 'Unknown'
end) as status, count(*) as cnt
from t
group by status;
You can also easily pivot the data using conditional aggregation:
select sum(executing = 1 and failed = 0) as Executing,
sum(failed = 1) as Failed,
sum(executing = 0 and failed = 0) as Pending
from t;
This uses a MySQL shorthand that treats boolean expressions as numbers -- with "1" for true and "0" for false.
SELECT SUM(executing) AS Executing
, SUM(failed) as Failed
, SUM(CASE
WHEN executing = 0 AND failed = 0 THEN 1
ELSE 0 END
) AS Pending
FROM mytable;

If two columns are 1 then make new column

i need a my sql statement which selects something similar to this
SELECT present, wholeday, attendance
present and wholeday is given, while attendance is generated by a combination of present and wholeday
if present == 1 and wholeday == 1 then attendance = 1
if present == 1 and wholeday == 0 then attendance = .5
if present == 0 and wholeday == 0 then attendance = 0
Your query would be:
SELECT PRESENT, WHOLEDAY,
CASE
WHEN (PRESENT = 1 AND WHOLEDAY = 1) THEN 1
WHEN (PRESENT = 1 AND WHOLEDAY = 0) THEN 0.5
ELSE 0
END as ATTENDANCE
FROM MY_TABLE
Case Syntax is :
CASE
WHEN condition_1 THEN commands
WHEN condition_2 THEN commands
...
ELSE commands
END CASE;