Defaulting result in case expression using count - sql

I currently am casing off of a big select statement. I am trying to work through the case portion. I currently have the case set up where it will take the name 'Brad' where units are 'inches'.
If I get rid of the units, it will show more than one result. Is there a way to default in the case statement where the case returns more than one result for 'brad'? Where if there is only 1 result, it will show that result, otherwise if there is more than 1 result, use units = 'inches'?
Here is the working case statement:
And case when R.name = 'Brad' and R.units ='Inches' then 0 else 1 End = 1
Here is a non working semi-psedu version of what I'd like to do:
And case when Count(R.name ='Brad') > 1 then (R.name = 'Brad' and R.units = 'Inches') else (R.name = 'Brad' ) End
Regardless it should return 1 result, default to inches if it returns more than 1 for the name 'Brad'
Here is the portion of the statement how it is joined.
select r.name, r.units, r.blah
From items I
Inner Join Results.R (NoLock)
ON I.Code = R.Code
WHERE r.status IN ('Y','N')
And case when R.name = 'Brad' and R.units ='Inches' then 0 else 1 End = 1

Related

Update Set column only if its NULL else move to next column and so on update with same data

I want to update a column when it's only NULL, else update the same data in the next column and so on.
I don't want to write 4 IF conditions, can it be possible in single CASE like below?
Below is something I am trying to achieve.
UPDATE I
SET
(CASE
WHEN I."CA_Status1" ISNULL THEN I."CA_Status1"
WHEN I."CA_Status1" IS NOTNULL THEN I."CA_Status2"
WHEN I."CA_Status2" IS NOTNULL THEN I."CA_Status3"
WHEN I."CA_Status3" IS NOTNULL THEN I."CA_Status4"
END
)
= "7".StatusCode
,I."ENC" = "7".ActionCode
FROM [dbo].[Out_P] I
INNER JOIN #TempOut_P "7"
ON I.ID = "7".Number
Since the conditions that determine whether or not a particular column is updated are related, selecting the target column could be done in a CROSS APPLY. This would simplify the resulting assignments, making them consistent and easier to read.
UPDATE I
SET
CA_Status1 = CASE WHEN S.Selector = 1 THEN "7".StatusCode ELSE I.CA_Status1 END,
CA_Status2 = CASE WHEN S.Selector = 2 THEN "7".StatusCode ELSE I.CA_Status2 END,
CA_Status3 = CASE WHEN S.Selector = 3 THEN "7".StatusCode ELSE I.CA_Status3 END,
CA_Status4 = CASE WHEN S.Selector = 4 THEN "7".StatusCode ELSE I.CA_Status4 END,
ENC = "7".ActionCode
FROM dbo.Out_P I
INNER JOIN #TempOut_P "7"
ON I.ID = "7".Number
CROSS APPLY (
SELECT Selector = CASE
WHEN I.CA_Status1 IS NULL THEN 1
WHEN I.CA_Status2 IS NULL THEN 2
WHEN I.CA_Status3 IS NULL THEN 3
WHEN I.CA_Status4 IS NULL THEN 4
END
) S

Case when with subquery

I'm trying to create a query that flags every line item that falls into a list of loan numbers that i'm pulling together with a subquery.
So far my script is written as follows:
case when loannumber in
(
select distinct cast(loannumber as float)
from tbl_one
where exists (select a.loannumber,
case when a.FlagA = 0 and b.[NFC] = 'N/A' then 'x'
when a.FlagA = 0 and b.[NFC] <> 'N/A' and sum(a.Amount) > b.[NFC] then 'x'
when a.FlagA = 1 and b.[FC] = 'on approval' then 'x'
when a.FlagA = 1 and b.[FC] <> 'on approval' and sum(a.Amount) > b.[FC]then 'x'
end as [error flag]
from tbl_one a
inner join lkp_table b
on a.State = b.state
group by a.loannumber, FlagA, [NFC], [FC])
)
then 'Error 1' else null end as [Error 1],
When I run the subquery, it correctly identifies all loan numbers that should be flagged, but when I put it into the case when statement, it ends up flagging every loan number in the population. Is there a way to fix this?

UPDATE using case when exists ( don't get the right result)

i'm trying to update a table that is alimented by 2 flows, the 1st one i have to make FillRateCode (the column i want to update) equal to FillRateCode from BWH_OTC_Order but for the 2nd flow i put it equal to '-1'
this is my script:
use BITS
;with tmp as (
select SalesOrderItemNum, SalesOrderNum, FillRateCode
From
BWH_OTC_Order
INNER JOIN REF_Company Comp
ON (Comp.CompanyCode= BWH_OTC_Order.CompanyCode AND Comp.DivisionCode='TEE')
where RevisedPGIDate is not null
)
UPDATE bits_tee.dbo.DMT_TEE_OTC_OrderFulFill
SET FillRateCode = case when exists ( select 1 from tmp) then tmp.FillRateCode else '-1' end
FROM bits_tee.dbo.DMT_TEE_OTC_OrderFulFill DMT
left outer join tmp
on tmp.SalesOrderItemNum = DMT.SalesOrderItemNum
and tmp.SalesOrderNum = DMT.SalesOrderNum
and this is the result i get
NB BWH_FillRateCode DMT_FillRateCode
124457 NULL NULL
73991 0 0
457507 1 1
28632 -1 -1
4849 2 2
34262 3 3
for nulls the correct resault is to get '-1' in DMT_FillRateCode
any issues?
Thx by advence
You can use ISNULL or COALESCE to replace NULL with something else. Your exists( select 1 from tmp) is pointless since it only checks if there are any rows (so not only related rows).
WITH tmp
AS (SELECT SalesOrderItemNum,
SalesOrderNum,
FillRateCode
FROM BWH_OTC_Order
INNER JOIN REF_Company Comp
ON ( Comp.CompanyCode = BWH_OTC_Order.CompanyCode
AND Comp.DivisionCode = 'TEE' )
WHERE RevisedPGIDate IS NOT NULL)
UPDATE DMT
SET DMT.FillRateCode = ISNULL(tmp.FillRateCode, '-1')
FROM bits_tee.dbo.DMT_TEE_OTC_OrderFulFill DMT
LEFT OUTER JOIN tmp
ON tmp.SalesOrderItemNum = DMT.SalesOrderItemNum
AND tmp.SalesOrderNum = DMT.SalesOrderNum
Maybe I'm misinterpreting your question, but if you just want to replace null values with -1 you can use the isnull function
isnull(tmp.FillRateCode,-1) -- I'm guessing the -1 is an int, and not a char '-1'
instead of
case when exists ( select 1 from tmp) then tmp.FillRateCode else '-1' end

SQL Case statement how to get value regardless of other results

Here is my statement:
SELECT
CASE
WHEN #UserRole = 1 THEN 1
ELSE 0
END AS [CanEdit],
F.FundingStreamName
FROM FundingStream AS F
LEFT JOIN Projects AS P ON P.FundingStream = F.FundingStreamID
WHERE ProjectNumber = #ProjectNumber
I noticed if FundingStreamID is null, the case statement will return nothing as well, how can I get the case statement to execute regardless if there is a funding stream or not? Thanks.
I think the problem is that your where clause is "undoing" your left outer join. Try moving the condition to the on clause:
SELECT (CASE WHEN #UserRole = 1 THEN 1
ELSE 0
END) AS [CanEdit],
F.FundingStreamName
FROM FundingStream F LEFT JOIN
Projects P
ON P.FundingStream = F.FundingStreamID AND
P.ProjectNumber = #ProjectNumber ;
Using #MicSim answer:
SELECT
CASE
WHEN #UserRole = 1 THEN 1
ELSE 0
END AS [CanEdit],
F.FundingStreamName
FROM FundingStream AS F
RIGHT JOIN Projects AS P ON P.FundingStream = F.FundingStreamID
WHERE ProjectNumber = #ProjectNumber
Thanks again for the help!

TSQL - TOP and COUNT in one SELECT

i try to combine these two statements to one, but all my tries failed!
Is it possible to merge them?
-- Is there a open answer?
SELECT CASE COUNT(tbl_Communication.pk_Communication) WHEN 0
THEN 0 ELSE 1 END AS hasAnsweredCom
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
WHERE tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
-- Get the answer text
SELECT TOP 1 tbl_Communication.subject AS hasAnsweredComStepName
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
WHERE tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
ORDER BY tbl_Communication.pk_Communication
Right join trick.
SELECT TOP 1
CASE WHEN tbl_CommunicationElements.pk_Communication IS NULL THEN 0 ELSE 1 END hasAnsweredCom
, tbl_Communication.subject AS hasAnsweredComStepName
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
RIGHT JOIN (VALUES(1)) AS Ext(x) ON (
tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
)
If you are willing to put the two results on one line, the following works:
select (CASE count(*) WHEN 0 THEN 0 ELSE 1 END) AS hasAnsweredCom,
MAX(case when seqnum = 1 then subject end) as hasAnsweredComStepName
from (SELECT tbl_Communication.pk_Communication, tbl_Communication.subject,
ROW_NUMBER() over (order by pk_communication) as seqnum
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
WHERE tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
) t
The second value will be NULL if there are no answers.
As for returning two rows. My guess is that subject is a string whereas hasAnsweredCom is an integer. The types conflict, so any sort of union or bringing the results together will probably result in a type conflict on the second row.