T-SQL IF-ELSE - sql

Is it possible to do something like this with IF-ELSE, or something similar:
SELECT
MemberID,
ProfileTypeID
PrCountryID, -- 3
PrStateID, -- 4
PrStateInt
FROM Member
WHERE PrCity IS NOT NULL
IF #ShowUnclaimed = 'N'
AND Claimed = 'Y'
AND SBIcon = 'N'
END
AND Viewable = 'Y'
AND SystemID = 2
Many thanks in advance for any information.
neojakey

No need to use Case for this one
SELECT
MemberID,
ProfileTypeID
PrCountryID, -- 3
PrStateID, -- 4
PrStateInt
FROM Member
WHERE PrCity IS NOT NULL
AND Viewable = 'Y'
AND SystemID = 2
AND ( #ShowUnclaimed != 'N'
OR ( Clamed = 'Y' AND SBIcon = 'N' )
)

You're looking for the case expression (MSDN)
SELECT MemberID,
ProfileTypeID
PrCountryID, -- 3
PrStateID, -- 4
PrStateInt
FROM Member
WHERE PrCity IS NOT NULL
AND CASE
WHEN #ShowUnclaimed = 'N' AND Claimed = 'Y' AND SBIcon = 'N' THEN 1
END = 1
AND Viewable = 'Y'
AND SystemID = 2

If I understand your intention correctly there is no need for IF, or CASE for that matter.
Just something like this seems to be what you were trying to do...?
WHERE PrCity IS NOT NULL
AND (#ShowUnclaimed = 'Y'
OR (Claimed = 'Y'
AND SBIcon = 'N'))
AND Viewable = 'Y'
AND SystemID = 2

Give this a try using CASE:
SELECT
MemberID,
ProfileTypeID
PrCountryID, -- 3
PrStateID, -- 4
PrStateInt
FROM Member
WHERE PrCity IS NOT NULL
AND Viewable = 'Y'
AND SystemID = 2
AND Claimed = CASE WHEN #ShowUnclaimed = 'N' THEN 'Y' ELSE Claimed END
AND SBIcon = CASE WHEN #ShowUnclaimed = 'N' THEN 'N' ELSE SBIcon END
This is just one way to do it. Best of luck.

Related

case statement to find one value

I'm trying to show one row per id but it is returning three.
If the id has a 'y' then it should show a 'y'.
If it shows a 'y' and 'r' it should be 'y'.
If it has 'y', 'r', 'n' it should be 'y'.
If it is just id and 'r' it should be 'r' and id and just 'n' then 'n'.
I can't seem to get it to work using a case statement. Any ideas? Thanks.
I've tried this:
,CASE WHEN result = 'Y' THEN 'Y'
WHEN result = 'Y' AND result = 'R') THEN 'Y'
WHEN result = 'R' THEN 'R'
ELSE 'N' END AS CARE_PLAN
What it is returning:
ID result
3434 'y'
3434 'r'
3434 'n'
You can use Listagg function,
Writing a subquery and DISTINCT then use Listagg function.
SELECT id, Listagg (result, ', ')
within GROUP (ORDER BY result) as CARE_PLAN
FROM (SELECT DISTINCT id,
( CASE
WHEN result = 'Y' THEN 'Y'
WHEN result = 'Y'
AND result = 'R' THEN 'Y'
WHEN result = 'R' THEN 'R'
ELSE 'N'
END ) AS result
FROM t) T
GROUP BY id
sqlfiddle:http://sqlfiddle.com/#!4/02cd5/2
[Results]:
| ID | CARE_PLAN |
|------|-----------|
| 1234 | N, R, Y |
It shall be proper to use ASCII and CHR functions for your case instead of using CASE .. WHEN, as in the following :
SELECT ID, CHR(MAX(ASCII(result))) AS CARE_PLAN
FROM TAB
GROUP BY ID
ORDER BY ID;
SQL Fiddle Demo
You would seem to want aggregation with some conditional logic:
select id,
(case when sum(case when result = 'y' then 1 else 0 end) > 0 then 'y'
when sum(case when result = 'r' then 1 else 0 end) > 0 then 'r'
when min(result) = max(result) and min(result) = 'n' then 'n'
else '?'
end) as new_result
from t
group by id;
If there are only those three values, then perhaps this simplified logic works:
select id, max(result) as new_result
from t
group by id;

Venn operation in SQL query

I have a table with list of employees who can be admin, dev, both or none
EMP_ID IS_ADMIN IS_DEV
1 Y Y
2 Y N
3 N Y
4 N N
I want to write a query so that when I request
IS_ADMIN=Y returns 1,2
IS_DEV=Y returns 1,3
IS_DEV=Y, IS_ADMIN=Y returns 1,2,3
IS_DEV=N, IS_ADMIN=N or nothing returns 4
Is there anyway to incorporate this login in one query?
You specify all the options as parameters, passing null to any parameter you don't care for and checking for null as a query condition. Your logic is a bit bent so it have to be programmed for exactly, every combination:
SELECT
emp_id
FROM
emps
WHERE
(is_admin = 'Y' AND :isadmin = 'Y' AND :isdev is null)
OR
(is_dev = 'Y' AND :isdev = 'Y' AND :isadmin is null)
OR
(:isadmin = 'Y' AND :isdev = 'Y' AND (is_admin = 'Y' OR is_dev = 'Y'))
OR
(COALESCE(:isadmin, 'N') = 'N' AND COALESCE(:isdev, 'N') = 'N' AND is_admin = 'N' AND is_dev = 'N')
Your calling language of choice would set the parameters to 'Y', 'N' or null if it had no opinion, e.g. in c# for your case 1:
sqlCommand.Parameters["isadmin"].Value = "Y";
sqlCommand.Parameters["isdev"].Value = DBNull.Value;
using(var reader = sqlCommand.ExecuteReader()) ....

Replacing CASE in SQL query

I got three columns with binary values. Then i got a query which makes a string based on those values. I made this work using case. But it's quite huge (it's a part of a bigger query), and i was wondering maybe there is a better way of doing this?
SELECT (CASE
WHEN TableA.flag1 = 1 AND TableA.flag2 = 1 AND TableA.flag3 = 1 THEN 'CGR'
WHEN TableA.flag1 = 1 AND TableA.flag2 = 1 THEN 'CG'
WHEN TableA.flag1 = 1 AND TableA.flag3 = 1 THEN 'CR'
WHEN TableA.flag2 = 1 AND TableA.flag3 = 1 THEN 'GR'
WHEN TableA.flag1 = 1 THEN 'C'
WHEN TableA.flag2 = 1 THEN 'G'
WHEN TableA.flag3 = 1 THEN 'R'
ELSE 'nothing'
END)
FROM TableA
Im working on MSSQL 2000 server.
You can use left() instead.
select left('C', T.flag1)+
left('G', T.flag2)+
left('R', T.flag3)
from TableA as T
I don't know if this is "better" but it is more concise:
SELECT ((CASE WHEN TableA.flag1 = 1 THEN 'C' ELSE '' END) +
(CASE WHEN TableA.flag2 = 1 THEN 'G' ELSE '' END) +
(CASE WHEN TableA.flag3 = 1 THEN 'R' ELSE '' END)
)
FROM TableA;
Okay, this isn't exactly the same because you get '' instead of 'nothing'. But I think the empty string does a better job of representing "no flags" than 'nothing' does.
Try this:
SELECT
CASE WHEN flag1 = 1 THEN 'C' ELSE '' END +
CASE WHEN flag2 = 1 THEN 'G' ELSE '' END +
CASE WHEN flag3 = 1 THEN 'R' ELSE '' END
FROM TABLEA
Maybe you could use function and move this CASE statement to this function

counting records on the same table with different values possibly none sql server 2008

I have a inventory table with a condition i.e. new, used, other, and i am query a small set of this data, and there is a possibility that all the record set contains only 1 or all the conditions. I tried using a case statement, but if one of the conditions isn't found nothing for that condition returned, and I need it to return 0
This is what I've tried so far:
select(
case
when new_used = 'N' then 'new'
when new_used = 'U' then 'used'
when new_used = 'O' then 'other'
end
)as conditions,
count(*) as count
from myDB
where something = something
group by(
case
when New_Used = 'N' then 'new'
when New_Used = 'U' then 'used'
when New_Used = 'O' then 'other'
end
)
This returns the data like:
conditions | count
------------------
new 10
used 45
I am trying to get the data to return like the following:
conditions | count
------------------
new | 10
used | 45
other | 0
Thanks in advance
;WITH constants(letter,word) AS
(
SELECT l,w FROM (VALUES('N','new'),('U','used'),('O','other')) AS x(l,w)
)
SELECT
conditions = c.word,
[count] = COUNT(x.new_used)
FROM constants AS c
LEFT OUTER JOIN dbo.myDB AS x
ON c.letter = x.new_used
AND something = something
GROUP BY c.word;
try this -
DECLARE #t TABLE (new_used CHAR(1))
INSERT INTO #t (new_used)
SELECT t = 'N'
UNION ALL
SELECT 'N'
UNION ALL
SELECT 'U'
SELECT conditions, ISNULL(r.cnt, 0) AS [count]
FROM (
VALUES('U', 'used'), ('N', 'new'), ('O', 'other')
) t(c, conditions)
LEFT JOIN (
SELECT new_used, COUNT(1) AS cnt
FROM #t
--WHERE something = something
GROUP BY new_used
) r ON r.new_used = t.c
in output -
new 2
used 1
other 0
You can do it as a cross-tab:
select
sum(case when new_used = 'N' then 1 else 0 end) as N,
sum(case when new_used = 'U' then 1 else 0 end) as U,
sum(case when new_used = 'O' then 1 else 0 end) as Other
from myDB
where something = something

Case statement problem to show values without null in a row

select CASE WHEN ( rtt.us_code = 's' )
THEN rtt.name
ELSE '' END AS input_tax_name,
CASE WHEN ( rtt2.us_code = 'r')
THEN rtt2.name
ELSE '' END AS output_tax_name,
CASE WHEN ( rtt.us_code = 's' )
THEN rtt.acc_id
ELSE 0 END AS input_tax_rate,
CASE WHEN ( rtt2.us_code = 'r')
THEN rtt2.acc_id
ELSE 0 END AS output_tax_rate
from supplier_item si
JOIN ret_tx_type rtt
ON si.ret_tx_type_id = rtt.ret_tx_type_id
JOIN ret_tx_type rtt2
ON si.ret_tx_type_id = rtt2.ret_tx_type_id
where si.ret_tx_type_id is not null
I am trying to display input and output tax in the same row for a report depends on us_code is 'r' and 's'.
But i am getting the results like
if input tax is there ...output tax column will be null
if output tax is there ...input tax column will be null
i am want to get the both in same row and no need to make null in any rows
please help
SELECT
MAX(CASE rtt.us_code WHEN 's' THEN rtt.name END) AS input_tax_name,
MAX(CASE rtt.us_code WHEN 'r' THEN rtt.name END) AS output_tax_name,
MAX(CASE rtt.us_code WHEN 's' THEN rtt.acc_id END) AS input_tax_rate,
MAX(CASE rtt.us_code WHEN 'r' THEN rtt.acc_id END) AS output_tax_rate
FROM supplier_item si
INNER JOIN ret_tx_type rtt
ON si.ret_tx_type_id = rtt.ret_tx_type_id
GROUP BY ??? /* here you should supply a column,
presumably in 'si', that is common
to both of the related 'r'- and
's'-tax records */
You need to add a GROUP BY to your query on something (possibly GROUP BY si.id?)
And put the CASE statements into aggregates. e.g.
MAX(CASE WHEN ( rtt2.us_code = 'r')
THEN rtt2.acc_id
ELSE 0 END)