Multiple Case Statements With Identical Expressions - tsql

I'm trying to evaluate a specific column to return five different columns - but the columns are based off the same expression in the CASE statements.
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 1
ELSE 0
END AS [Invalid] ,
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 0
ELSE 1
END AS [validMICcode] ,
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 0
ELSE 1
END AS [validSerialNumber] ,
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 0
ELSE 1
END AS [validFormat] ,
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 0
ELSE 1
END AS [validProductionYear] ,
I feel like is causing the table / column in question to be searched for the pattern five times, but I cannot figure out how rewrite it - or if it is even possible - to have one pattern search and define the columns based on the one search.
I'm trying different variations, but I cannot come up with the correct syntax for this issue:
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 1
ELSE 0
CASE WHEN 1 THEN 1 END AS [Invalid]
CASE WHEN 0 THEN 0 END AS [validMICode]
CASE WHEN 0 THEN 0 END AS AS [validSerialNumber]
CASE WHEN 0 THEN 0 END AS AS [validFormat]
CASE WHEN 0 THEN 0 END AS AS [validProductionYear]
END

One way would be a sub query and Bitwise NOT.
select
result Invalid,
~result validMICcode,
~result validSerialNumber,
~result validFormat,
~result validProductionYear
from
(
select
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%'
THEN CAST(1 As bit)
ELSE CAST(0 As bit)
End result
from ...
) tbl

You could do that with a CTE..
with cte as(
select *,
CASE
WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%'
THEN 1
ELSE 0
END as SomeColumn)
select *,
CASE WHEN SomeColumn = 1 THEN 1 END AS [Invalid]
CASE WHEN SomeColumn = 0 THEN 0 END AS [validMICode]
...
from cte

Related

TSQL - nested case

I ask if the nested houses are used as follows:
SELECT
CASE
WHEN Col1 < 2 THEN
CASE Col2
WHEN 'X' THEN 10
ELSE 11
END
WHEN Col1 = 2 THEN 2
.....
ELSE 0
END as Qty,
......,
FROM ....
explanation: If Col1 <2 shows something, but that something if X gives me the value 10 otherwise 11 If Col1 = 2 shows 2 otherwise 0 everything in the column name Qty
Is the reasoning correct?
Thanks in advance
It's should return what you say you need, but it's easier to read this way:
SELECT
CASE
WHEN Col1 < 2 AND Col2 = 'X' THEN 10
WHEN Col1 < 2 THEN 11
WHEN Col1 = 2 THEN 2
--.....
ELSE 0
END AS Qty
FROM
-- ...

Sum Booleans and then display only over a certain count

I've been banging my head against the wall a while.
I have a table that has DwgID (integer) and Current (boolean) as part of the table.
What I want to do is filter just the DwgID's that have more than 1 Current selected.
This code gives me the count correctly:
`SELECT "DwgID", SUM( CASE WHEN "Current" = 1 THEN 1 ELSE 0 END ) "Current" FROM "tblRev" GROUP BY "DwgID"`
But if I try to add a criteria where the Current Count is > 1, it fails with a data content could not be loaded.
`SELECT "DwgID", SUM( CASE WHEN "Current" = 1 THEN 1 ELSE 0 END ) "Current" FROM "tblRev" GROUP BY "DwgID" HAVING ( ( SUM( CASE WHEN "Current" = 1 THEN 1 ELSE 0 END ) > 1 ) )`
What am I doing wrong?
You need to choose a different alias because Current is the column name. Let's call it NumCurrent instead.
SELECT "DwgID", SUM( CASE WHEN "Current" = 1 THEN 1 ELSE 0 END ) "NumCurrent" FROM "tblRev" GROUP BY "DwgID" HAVING ( ( SUM( CASE WHEN "Current" = 1 THEN 1 ELSE 0 END ) > 1 ) )

Count valid values per user

I have a table with a list of values. -1 is a blank value:
ID FieldType1A FieldType1B FieldType2A FieldType2B Person
1 15 14 10 -1 1
2 16 -1 12 10 1
3 17 -1 5 6 1
4 6 -1 7 -1 2
...
So the result should be:
Person FieldType1 FieldType2
1 4 5
2 1 1
there is a users table with a list of user IDs, would there be a way of iterating over that list of values to generate the person list in the result set (0 for the field types being perfectly valid as it is merely counts)? I think the answer to T-SQL Column Values Count is a step in the direction I'm attempting to go, but unsure how to combine columns that are the same (the A/Bs allow for a list of answers). That and I'm interested in combining all valid values as not attempting to count the number of each valid response.
You can use a CASE expression to change all non-negative-one values to 1, and -1 values to 0, and then sum them up.
SELECT Person,
SUM(CASE WHEN FieldType1A <> -1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN FieldType1B <> -1 THEN 1 ELSE 0 END) AS FieldType1,
SUM(CASE WHEN FieldType2A <> -1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN FieldType2B <> -1 THEN 1 ELSE 0 END) AS FieldType2
FROM YourTable
GROUP BY Person
SELECT Person,
count(nullif(FieldType1A, -1)) + count(nullif(FieldType1B, -1)) as FieldType1,
count(nullif(FieldType2A, -1)) + count(nullif(FieldType2B, -1)) as FieldType2
FROM yourtable
GROUP BY person

Changing position of data in columns depending on actual data

Suppose I have a table like below
ID Marks1 Marks2 Marks3
-------------------------
1 10 0 4
2 0 40 90
Now, I need to select from this table in a way that will give precedence to positive values first. So if the marks are 0 then it will be shifted to right. The SELECT should give following output
ID Marks1 Marks2 Marks3
-------------------------
1 10 4 0
2 40 90 0
Can you please guide me for the approach? It will be great if it can be done in a select statement itself. Thanks in advance.
Something like this you will need to check for each subsequent row that the previous column isn't 0. Have selected the values out as null as it makes the code slightly easier to read as i can use coalesce
Select
Coalesce(Marks1, Marks2, Marks3,0) as Marks1,
Case when marks1 is not null
then Coalesce(Marks2, Marks3, 0) else 0
end as Marks2,
case when marks1 is not null
and marks2 is not null
then Coalesce(Marks3,0)
end as Marks3
from
(
Select
Case when Marks1 =0 then null else Marks1 end as Marks1,
Case when Marks2 =0 then null else Marks2 end as Marks2,
Case when Marks3 =0 then null else Marks3 end as Marks3
From mytbl
)

crystal report(case)

im using asp.net crystal report ........
sql query:
CONVERT(NUMERIC(17,3), CASE
WHEN
CASE WHEN GLDD_DOC_AMOUNT > 0 THEN GLDD_DOC_AMOUNT ELSE 0 END = 0 THEN NULL
ELSE
CASE WHEN GLDD_DOC_AMOUNT > 0 THEN GLDD_DOC_AMOUNT ELSE 0 END
END) DR,
CONVERT(NUMERIC(17,3), CASE
WHEN (- 1 *
CASE WHEN GLDD_DOC_AMOUNT < 0 THEN GLDD_DOC_AMOUNT ELSE 0 END) = 0 THEN NULL
ELSE - 1 *
CASE WHEN GLDD_DOC_AMOUNT < 0 THEN GLDD_DOC_AMOUNT ELSE 0 END
END) CR,
the above query is which is from sql server 2005.....
i have to use this query in crystal report Formula editor.....
how?
You can either paste the two SQL parts into SQL Expression formulas (one for the CR part and another for the DR part), or rewrite them as Crystal formulas.
Both formulas have some redundancy, and are easier in SQL as
CONVERT(NUMERIC(17,3), Case When GLDD_DOC_AMOUNT > 0 Then GLDD_DOC_AMOUNT Else null End) AS DR,
CONVERT(NUMERIC(17,3), Case When GLDD_DOC_AMOUNT < 0 Then -1*GLDD_DOC_AMOUNT Else null End) AS CR
In Crystal Syntax, you can use If...Else