Simplify this code - sql

I hate code that looks like its been hacked together. I have just written this:
update table1.dbo.totals
set #FEE = case
when isnull(g.SGROUPS,0) > 1
then #GROUPPRICE * case
when CHARINDEX('JMCG', g.GROUPS) > 0
then (g.SGROUPS - 2)
else (g.SGROUPS - 1)
end
else 0
end
from #GROUPMEM as g
if #FEE < 0
begin
set #GROUPFEE = 0
end
I'm particularly wanting to get rid of that nested CASE. Any suggestions?

Ok so this a little odd but may be cool. It gets rid of your nested case and uses some bitwise operations...
update table1.dbo.totals
set #FEE = COALESCE((g.SGROUPS^1)&1,0) * #GROUPPRICE *
case
when CHARINDEX('JMCG', g.GROUPS) > 0 then (g.SGROUPS - 2)
else (g.SGROUPS - 1)
end
from #GROUPMEM as g
if #FEE < 0
begin
set #GROUPFEE = 0
end
You're probably asking what (g.SGROUPS^1)&1 does... This basically converts g.SGROUPS to one if it has a value, allowing us to use it in the multiplication.

OK, I'm going to play some math games here and take advantage of the fact that y*(x-1)-y = y*(x-2).
EDIT: Realized I had my (SGROUPS-1) vs. (SGROUPS-2) logic backwards and fixed.
update table1.dbo.totals
set #FEE = #GROUPPRICE * isnull(g.SGROUPS-1,0) - case when isnull(CHARINDEX('JMCG', g.GROUPS),0)>0 then g.SGROUPS else 0 end
from #GROUPMEM as g
if #FEE < 0
begin
set #GROUPFEE = 0
end

For no CASE statements at all, try:
update table1.dbo.totals
set #FEE = #GROUPPRICE * isnull(nullif(sign(g.SGROUPS-1),-1),0)
* (isnull(g.SGROUPS,0) - 1 - sign(CHARINDEX('JMCG', g.GROUPS))
from #GROUPMEM as g
if #FEE < 0
begin
set #GROUPFEE = 0
end

Related

Update Set value 1 if value else set 0

I was just woking trough some old final exams as I stumbled across this task:
Table:
Create a single SQL query from this table which will check if Art_MWStSatz is 7% and set Art_Markierung to 1 if it is, else set Art_Markierung to 0.
I dont't have any idea on how to solve this. I asked my teacher by she didn't know an answer either.
Some translations:
Artikel = Item
MWSt Satz = vat rate
Markierung = mark
I don't have access to the solution so it all depends in your answers.
Use an UPDATE and a CASE. This assumes that '7%' is a string and not the int '7'
UPDATE table
SET
Art_Markierung = CASE
WHEN Art_MWStSatz = '7%'
THEN 1
ELSE 0
END;
I presume that 1 or 0 is true or false. In this situation, you can use a CASE like this:
UPDATE MyTable
SET Art_Markierung = CASE Art_MWStSatz
WHEN '7%' THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END
Assume Art_Markierung and Art_MWStSatz are integer:
UPDATE Artikel
SET Art_Markierung =
CASE
WHEN Art_MWStSatz = 7 THEN 1
ELSE 0
END

calculated field using if and or in ms-sql

IF(
[dbo.tblx.Category] = 'WS' OR [dbo.tblx.Category] = 'SEM',
0,
[dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
)
this returns the entire text and not the "calculation"
can someone help me...what am I doing wrong?
i tried
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000
END AS Metal
Do perhaps insert something before "CASE WHEN"? it is not running
As I understood, you are trying to create a computed column, using value from other table.
I think in this case you have to use a User Defined function in the formula for computed column. If you give more information I can try to write some code for you.
Instead, if you just want to add a column in a query you can use:
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblPTx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000 END AS NAME_OF_YOUR_NEW_COLUMN
Update
DECLARE #myvar INT;
SET #myvar = CASE WHEN ([dbo.tblx.Category] = WS OR [dbo.tblPTx.Category] = SEM) THEN 0
ELSE [dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
END

case statement in where clause SQLSERVER

I tried searching around but couldn't find anything that would help me out.
I'm trying to do this in SQL:
DECLARE #onlyMM INT
SET #onlyMM = 1
SELECT *
FROM cdn.magnag
WHERE CASE #onlyMM
WHEN 1
THEN man_zrdtyp NOT IN (
1616
,2001
)
ELSE - 1
END
I have a problem with:
where case #onlyMM when 1 then man_zrdtyp not in (1616,2001) else -1 end
how to properly make a case for the operator not in?
I think you need to formulate the WHERE clause in a different way. You could use OR instead of case statement. Like this:
WHERE
(#onlyMM=1 AND man_zrdtyp not in (1616,2001))
OR #onlyMM<>1
You don't want to do this?
where (case when #onlyMM = 1 then man_zrdtyp else -1 end) not in (1616,2001)

T-SQL and decision making constructs

Please help me with the syntax of this query. It doesn't compile. It says there's a syntax error near the keyword END.
Obviously, I've got these BEGINs and ENDS mixed up.
I am using Microsoft SQL Server 2008 R2. I am not sure of the syntax of these BEGINS and ENDs.
Please don't mind the condition 1 = 0. That's something that will be replaced with a proper predicate later.
IF EXISTS (SELECT * FROM StringCategory WHERE ResourceKeyId = 18134 AND CategoryId = 0)
BEGIN
UPDATE StringCategory
SET CategoryId = 0
WHERE ResourceKeyId = 18134
END
ELSE
BEGIN
IF 1 = 0
BEGIN
DELETE FROM StringCategory WHERE ResourceKeyId = 18134
END
ELSE
BEGIN
INSERT INTO StringCategory
VALUES(18134, 0)
END
END
END
Your last END is an extra. You can think of the BEGINs and ENDs like { and } in C# for the IF constructs (They serve to mark the beginning and end of the block to be executed in the IF/ELSE statement).
Here is a much simpler way to do what you appear to be attempting.
insert into StringCategory
(ResourceKey, CategoryId)
select 18134, 0
where not exists (
SELECT *
FROM StringCategory
WHERE ResourceKeyId = 18134
AND CategoryId = 0)

SQL simplifying case in case in case

I got the following SQL code (part of a select Statement):
Case
When HilfsTab2.Gruppe = 'HST' And Basis.Breite_FLA = Basis.Breite Then 0
Else Case When HilfsTab2.Gruppe = 'SA' Or HilfsTab2.Gruppe = 'HO / TB' Or
HilfsTab2.Gruppe = 'PR' Then 0 Else Case
When HilfsTab2.Gruppe Is Null Then -1 Else 1 End End
End As IsHST_Fluegel
Now, I run this over a table of several million entries. From my understanding, SQL checks the first case when for all rows, then the second for all entries and so on. This takes ages. Now I was thinking, there needs to be an easier way to do this.
I was thinking of a stored procedure / custom function that basically outputs -1, 0 or 1 depending on the entry.
Thanks in advance
For a possible speed improvement, do the NULL check first, the column comparison last and refactor to remove the nested CASE:
CASE WHEN HilfsTab2.Gruppe IS NULL
THEN -1
WHEN HilfsTab2.Gruppe IN ('SA', 'HO / TB', 'PR')
OR (HilfsTab2.Gruppe = 'HST' AND Basis.Breite_FLA = Basis.Breite)
THEN 0
ELSE 1
END AS IsHST_Fluegel
Your case could be simplified as:
Case
When HilfsTab2.Gruppe = 'HST' And Basis.Breite_FLA = Basis.Breite Then 0
When HilfsTab2.Gruppe in ('SA', 'HO / TB', 'PR') Then 0
When HilfsTab2.Gruppe Is Null Then -1
Else 1
End As IsHST_Fluegel
But this will not speed up your query. If you want to select millions of rows, it would take time anyway.