SQL CASE Clauses with variable - sql

I get this error:
Incorrect syntax near '='.
Code:
WHERE
T.[ID] = -9769
AND TNS.Active = 1
AND CASE
WHEN T.[StatusID] IN (1,6)
THEN (T.[AuditUser_ID] = 2 AND TNX.Actor = 2)
END

WHERE
T.[ID] = -9769
AND TNS.Active = 1
AND 1 = CASE
WHEN T.[StatusID] IN (1,6)
and (T.[AuditUser_ID] = 2 AND TNX.Actor = 2) then 1 else 0
END

Related

ORA-00904: invalid identifier but table and column names are correct?

I have the below SQL query but it gives the error message ORA-00904: "KUST_ADR"."KU_NR": invalid identifier even though those are the correct table and column names. What else could be the cause?
update auf_adr
set email = (select k.ku_email
from auf_kopf k join
kust_adr ka
on k.kunr = ka.ku_nr
where auf_adr.auf_nr = k.auf_nr and
ka.ku_adr_art = 1 and
auf_adr.email <> ka.ku_email and
(select sum(s.rg_anz)
from auf_stat s
where s.auf_nr = k.auf_nr
) = 0
)
where auf_adr.adr_art = 2 and
exists (select 1
from auf_kopf k join
kust_adr ka
on k.kunr = ka.ku_nr
where auf_adr.auf_nr = k.auf_nr and
ka.ku_adr_art = 1 and
auf_adr.email <> ka.ku_email and
(select sum(s.rg_anz)
from auf_stat s
where s.auf_nr = k.auf_nr
) = 0
);
There is a "and" missing after each of the "where" clause line, this could be the issue.
where auf_adr.auf_nr = k.auf_nr AND

SQL - WHERE with CASE statement

SELECT TOP 1
CostValue
FROM
[~client_table~].[dbo].[CostRules] AS CostRule
WHERE
(CASE
WHEN DATALENGTH(CostRule.ModelName) = 0
THEN
CostRule.Type = 1
AND CostRule.Manufacturer = Printer.ManufacturerId
AND CostRule.ColorType = 1
ELSE
CostRule.Type = 2
AND CostRule.ModelName = Printer.ModelName
AND CostRule.ColorType = 1
END
)
) AS MonoCost
I want to define my where statement depending on the datalength of CostRule.ModelName. But i got an error: Incorrect syntax near '='. in CostRule.Type = 1 and i got a error in the ELSE statement.
Must be like this:
...
WHERE
(DATALENGTH(CostRule.ModelName) = 0
AND CostRule.Type = 1
AND CostRule.Manufacturer = Printer.ManufacturerId
AND CostRule.ColorType = 1)
OR
(DATALENGTH(CostRule.ModelName) != 0
AND CostRule.Type = 2
AND CostRule.ModelName = Printer.ModelName
AND CostRule.ColorType = 1)
The CASE-style from your query cannot work.
you can change your statement like this:
SELECT TOP 1
CostValue
FROM
[~client_table~].[dbo].[CostRules] AS CostRule
WHERE CostRule.ColorType=1
AND CostRule.Type=CASE WHEN DATALENGTH(CostRule.ModelName) = 0 THEN 1 ELSE 2 END
AND CostRule.Manufacturer=CASE WHEN DATALENGTH(CostRule.ModelName) = 0 THEN Printer.ManufacturerId ELSE Printer.ModelName END
You can't use a CASE statement to define where conditions like that. It will be easier to just use some boolean logic
SELECT *
FROM your_table
WHERE (DATALENGTH(CostRule.ModelName) = 0
AND CostRule.Type = 1
AND CostRule.Manufacturer = Printer.ManufacturerId
AND CostRule.ColorType = 1)
OR (DATALENGTH(CostRule.ModelName) != 0
AND CostRule.Type = 2
AND CostRule.ModelName = Printer.ModelName
AND CostRule.ColorType = 1)
There are some other things that could be removed (like CostRule.ColorType = 1 since it is the same in both branches) but I've left them in here to illustrate how to transform your CASE statement into a boolean logic set.
It looks like you would just need to change the WHERE statement:
It looks like you will just need to change your WHERE statement to use OR and remove the CASE Statement.
(SELECT TOP 1
CostValue
FROM
[~client_table~].[dbo].[CostRules] AS CostRule
WHERE
DATALENGTH(CostRule.ModelName) = 0
CostRule.Type = 1
AND CostRule.Manufacturer = Printer.ManufacturerId
AND CostRule.ColorType = 1
OR
DATALENGTH(CostRule.ModelName) <> 0
AND CostRule.Type = 2
AND CostRule.ModelName = Printer.ModelName
AND CostRule.ColorType = 1
) AS MonoCost

The multi-part identifier "SR_DOC_HDR.DOC_VERS_NO" could not be bound

This is the failed query.
Failed SQL:
Select SO_DOC_CD,
SO_DOC_DEPT_CD,
SO_DOC_ID,
SO_DOC_VERS_NO,
SR_DOC_CD,
SR_DOC_DEPT_CD,
SR_DOC_ID,
SR_DOC_VERS_NO,
DOC_LAST_DT,
DOC_PHASE_CD,
DOC_PHASE_CD_SO,
DOC_STA_CD,
VEND_CUST_CD,
DOC_SH_DSCR,
RESP_STA,
RESP_TM_WEB,
"Created By",
QRY_SRCH_STRING,
SO_DOC_REF,
SR_DOC_REF,
SO_DOC_FUNC_CD,
RESP_DT_ADV,
SO_SR_QRY.RESP_STA_ORD,
APPL_STA_CD,
SO_SR_QRY.APPL_STA_CD_ORD,
SO_SR_QRY.AWD_FL
From (
Select SO_DOC_HDR.DOC_CD As SO_DOC_CD,
SO_DOC_HDR.DOC_DEPT_CD As SO_DOC_DEPT_CD,
SO_DOC_HDR.DOC_ID As SO_DOC_ID,
SO_DOC_HDR.DOC_VERS_NO As SO_DOC_VERS_NO,
SO_DOC_HDR.SO_CLSNG_DT As SO_CLSNG_DT,
SO_DOC_HDR.SO_CLSNG_TM As SO_CLSNG_TM,
SR_DOC_HDR.DOC_CD As SR_DOC_CD,
SR_DOC_HDR.DOC_DEPT_CD As SR_DOC_DEPT_CD,
SR_DOC_HDR.DOC_ID As SR_DOC_ID,
SR_DOC_HDR.DOC_VERS_NO As SR_DOC_VERS_NO,
SR_DOC_HDR.DOC_LAST_DT As DOC_LAST_DT,
SR_DOC_HDR.DOC_PHASE_CD As DOC_PHASE_CD,
SR_DOC_HDR.APPL_STA_CD As APPL_STA_CD,
SO_DOC_HDR.DOC_PHASE_CD As DOC_PHASE_CD_SO,
SR_DOC_HDR.DOC_STA_CD As DOC_STA_CD,
SR_DOC_HDR.VEND_CUST_CD As VEND_CUST_CD,
SO_DOC_HDR.DOC_SH_DSCR As DOC_SH_DSCR,
SR_DOC_HDR.RESP_STA As RESP_STA,
SR_DOC_HDR.RESP_TM_WEB As RESP_TM_WEB,
SR_DOC_HDR.DOC_CREA_USID As "Created By",
SR_DOC_HDR.QRY_SRCH_STRING As QRY_SRCH_STRING,
SO_DOC_HDR.DOC_REF As SO_DOC_REF,
SR_DOC_HDR.DOC_REF As SR_DOC_REF,
SO_DOC_HDR.DOC_FUNC_CD As SO_DOC_FUNC_CD,
SR_DOC_HDR.RESP_DT_ADV As RESP_DT_ADV,
Case When RESP_STA = 7 Then 1
When RESP_STA = 1 Then 2
When RESP_STA = 2 Then 4
When RESP_STA = 3 Then 3
When RESP_STA = 4 Then 5
When RESP_STA = 5 Then 6
End As RESP_STA_ORD,
Case When APPL_STA_CD = 5 Then 1
When APPL_STA_CD = 4 Then 2
When APPL_STA_CD = 1 Then 3
When APPL_STA_CD = 3 Then 4
When APPL_STA_CD = 6 Then 5
When APPL_STA_CD = 2 Then 6
When APPL_STA_CD = 7 Then 7
End As APPL_STA_CD_ORD,
Case When Exists (
Select 1
From SR_DOC_COMMLN
Where SR_DOC_COMMLN.SO_DOC_ID = SR_DOC_HDR.SO_DOC_ID
And SR_DOC_COMMLN.SO_DOC_DEPT_CD = SR_DOC_HDR.SO_DOC_DEPT_CD
And SR_DOC_COMMLN.SO_DOC_VERS_NO = SR_DOC_HDR.SO_DOC_VERS_NO
And SR_DOC_COMMLN.SO_DOC_CD = SR_DOC_HDR.SO_DOC_CD
And (
SR_DOC_COMMLN.AWARD_CREATED = 1
Or SR_DOC_COMMLN.AWARD_FINALIZED = 1
)
) Then 1
Else 0
End As AWD_FL
From av3112jm1.dbo.SR_DOC_HDR SR_DOC_HDR,
av3112jm1.dbo.SO_DOC_HDR SO_DOC_HDR
Where SO_DOC_HDR.DOC_CD = SR_DOC_HDR.SO_DOC_CD
And SO_DOC_HDR.DOC_DEPT_CD = SR_DOC_HDR.SO_DOC_DEPT_CD
And SO_DOC_HDR.DOC_ID = SR_DOC_HDR.SO_DOC_ID
And SO_DOC_HDR.DOC_PHASE_CD = 3
) SO_SR_QRY
Where 1 = 1
And (
SR_DOC_HDR.DOC_VERS_NO = 1
And SR_DOC_HDR.DOC_DEPT_CD = '010'
And SR_DOC_HDR.DOC_CD = 'GFA'
And SR_DOC_HDR.DOC_ID = 'ESR09141700000000002'
)
Order By Case When SO_DOC_FUNC_CD = 3 Then 4
When SO_SR_QRY.AWD_FL = 1 Then 2
When Convert(DateTime, SO_CLSNG_DT, (108)) + Convert(DateTime, SO_CLSNG_TM, 114) <= '2017-09-20 02:44:23' Then 3
Else 1
End Asc,
SO_CLSNG_DT Desc,
SO_CLSNG_TM Desc;
The problem is that in your outer query, you're referencing SR_DOC_HDR.DOC_VERS_NO = 1, but your inner query has that column aliased as SR_DOC_VERS_NO.
You need to use the aliases given in the inner query.
Change your outer WHERE statement to:
Where 1 = 1
And (
SR_DOC_VERS_NO = 1
And SR_DOC_DEPT_CD = '010'
And SR_DOC_CD = 'GFA'
And SR_DOC_ID = 'ESR09141700000000002'
)
in your subquery you use twice the alias
sr_doc_hdr.doc_vers_no AS SR_DOC_VERS_NO

Case statement and OR in SQL

Can someone please advise on the below? I have a number of fields which I would like to use and combine these to get the data in one column. I am using the following case statement but am not getting the results I expect.
CASE
WHEN m.u_hearing = 1 THEN 'Hearing'
WHEN m.u_learning_reading_diff = 1 THEN 'Learning or reading Difficulty'
WHEN m.u_long_term_ill = 1 THEN 'Long Term Illness'
WHEN m.u_mental_illness = 1 THEN 'Mental Illness'
WHEN m.u_mobility = 1 THEN 'Mobility'
WHEN m.u_physical_coordination = 1 THEN 'Physical Co-ordination'
WHEN m.u_physical_dis = 1 THEN 'Physical Disability'
WHEN m.u_red_physical_cap = 1 THEN 'Reduced_physical_capacity'
WHEN m.u_speech = 1 THEN 'Speech'
WHEN m.u_vision = 1 THEN 'Vision'
WHEN m.u_other_dis = 1 THEN 'Other_Disability'
WHEN (m.u_hearing = 1 AND (m.u_learning_reading_diff = 1 OR (m.u_speech = 1))) THEN 'Multiple'
It is the last statement that is not giving the result I would like as if there are multiple fields with Yes then I would like multiple returned but it seems it is picking the first case.
Put the "hardest" condition first
case WHEN m.u_hearing = 1 AND (m.u_learning_reading_diff = 1 OR m.u_speech = 1) THEN 'Multiple'
WHEN m.u_hearing = 1 THEN 'Hearing' ...
Because a case stops at the first condition that is true
When using CASE statements, they will function in the order you have specified.
So if m.u_Hearing = 1 then it will stop at the first line and not reach the bottom one.
This will work for what you require, however list it in the order you prefer.
CASE
WHEN (m.u_hearing = 1 AND (m.u_learning_reading_diff = 1 OR (m.u_speech = 1))) THEN 'Multiple'
WHEN m.u_hearing = 1 THEN 'Hearing'
WHEN m.u_learning_reading_diff = 1 THEN 'Learning or reading Difficulty'
WHEN m.u_long_term_ill = 1 THEN 'Long Term Illness'
WHEN m.u_mental_illness = 1 THEN 'Mental Illness'
WHEN m.u_mobility = 1 THEN 'Mobility'
WHEN m.u_physical_coordination = 1 THEN 'Physical Co-ordination'
WHEN m.u_physical_dis = 1 THEN 'Physical Disability'
WHEN m.u_red_physical_cap = 1 THEN 'Reduced_physical_capacity'
WHEN m.u_speech = 1 THEN 'Speech'
WHEN m.u_vision = 1 THEN 'Vision'
WHEN m.u_other_dis = 1 THEN 'Other_Disability'
From what I know you cannot use your first statement again in another "when" like your
WHEN m.u_hearing = 1 THEN 'Hearing'
Above condition try to use else if it is working and feasible for you in end.
Else 'Multiple'

Understanding case expression in the "Where" clause

I've got this code here and you can see from my Pseudocode what I'm trying to accomplish
select *
from dbo.BenefitsForms
inner join Dependents on BenefitsForms.UserId = Dependents.BenefitsForm_UserId
inner join CoverageLevels on BenefitsForms.MedicalId = CoverageLevels.Id
where (BenefitsForms.MedicalId > 0 AND BenefitsForms.MedicalId < 13)
AND Dependents.IsSpouse = CASE when CoverageLevels.[Level] = 2 then 1
when CoverageLevels.[Level] = 3 then 0 end
when CoverageLevels.[Level] = 4 then [any, it doesnt matter] <--- my desire but it doesn't work.
What can I do to get the effect I desire in the brackets? If Coverage Level = 4 then I don't care what Dependents.IsSpouse is, I don't even need to sort by it anymore.
Assuming that isSpouse can only be 0 or 1... if CoverageLevels.Level is 4, then compare isSpouse to itself, which will always result in true:
AND Dependents.IsSpouse = CASE
when CoverageLevels.[Level] = 2 then 1
when CoverageLevels.[Level] = 3 then 0
when CoverageLevels.[Level] = 4 then Dependents.IsSpouse
END
Alternately, this can also be expressed without the CASE:
WHERE
BenefitsForms.MedicalId > 0
AND BenefitsForms.MedicalId < 13
AND (
(Dependents.IsSpouse = 1 AND CoverageLevels.[Level] = 2)
OR (Dependents.IsSpouse = 0 AND CoverageLevels.[Level] = 3)
OR CoverageLevels.[Level] = 4
)