SQl - An expression of non-boolean type specified in a context where a condition is expected, near ')' - sql

Getting this error with the following query in SQL Server 2014.
SELECT
[Case]
,[Course]
,[Device]
,[IntegerValue]
,[Question]
,IFL.[QuestionSimplified]
,[Revision]
,[Script]
,[TextValue]
,[Timestamp]
,[Type]
,[Variable]
,[Wave]
FROM [dbo].[CosmosData] CD
Left Outer join [dbo].[ImportedFacilityList] IFL on CD.[Variable] = IFL.[Variablename]
where
CD.[Script] = 'CARD-F' and
(select * from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = '2')
When I run the above query I am getting the beloiw error,
Msg 4145, Level 15, State 1, Line 20
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Any help please?

You have this in the where clause:
and (select * from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = '2')
SQL needs a boolean expression. This is usually formed by using = or a similar comparison operator. In your case, I think you just exant exists:
exists (select * from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = 2)
That said, you might want a correlation clause as well.
Note: I removed the single quotes from the integer value. Only use single quotes for string and date constants.

based on the assumption that you are using case as a key in this table, you can use the follwing to return all rows from cosmos data where your conditions are applied and the select in your where clause has a match using the criteria within it.
SELECT
[Case]
,[Course]
,[Device]
,[IntegerValue]
,[Question]
,IFL.[QuestionSimplified]
,[Revision]
,[Script]
,[TextValue]
,[Timestamp]
,[Type]
,[Variable]
,[Wave]
FROM [dbo].[CosmosData] CD
Left Outer join [dbo].[ImportedFacilityList] IFL on CD.[Variable] = IFL. [Variablename]
where CD.[Script] = 'CARD-F'
and Case
IN
(select Case from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = '2')
Hope that helps any

Related

Issue With SQL Pivot Function

I have a SQL query where I am trying to replace null results with zero. My code is producing an error
[1]: ORA-00923: FROM keyword not found where expected
I am using an Oracle Database.
Select service_sub_type_descr,
nvl('Single-occupancy',0) as 'Single-occupancy',
nvl('Multi-occupancy',0) as 'Multi-occupancy'
From
(select s.service_sub_type_descr as service_sub_type_descr, ch.claim_id,nvl(ci.item_paid_amt,0) as item_paid_amt
from table_1 ch, table_" ci, table_3 s, table_4 ppd
where ch.claim_id = ci.claim_id and ci.service_type_id = s.service_type_id
and ci.service_sub_type_id = s.service_sub_type_id and ch.policy_no = ppd.policy_no)
Pivot (
count(distinct claim_id), sum(item_paid_amt) as paid_amount For service_sub_type_descr IN ('Single-occupancy', 'Multi-occupancy')
)
This expression:
nvl('Single-occupancy',0) as 'Single-occupancy',
is using an Oracle bespoke function to say: If the value of the string Single-occupancy' is not null then return the number 0.
That logic doesn't really make sense. The string value is never null. And, the return value is sometimes a string and sometimes a number. This should generate a type-conversion error, because the first value cannot be converted to a number.
I think you intend:
coalesce("Single-occupancy", 0) as "Single-occupancy",
The double quotes are used to quote identifiers, so this refers to the column called Single-occupancy.
All that said, fix your data model. Don't have identifiers that need to be quoted. You might not have control in the source data but you definitely have control within your query:
coalesce("Single-occupancy", 0) as Single_occupancy,
EDIT:
Just write the query using conditional aggregation and proper JOINs:
select s.service_sub_type_descr, ch.claim_id,
sum(case when service_sub_type_descr = 'Single-occupancy' then item_paid_amt else 0 end) as single_occupancy,
sum(case when service_sub_type_descr = 'Multi-occupancy' then item_paid_amt else 0 end) as multi_occupancy
from table_1 ch join
table_" ci
on ch.claim_id = ci.claim_id join
table_3 s
on ci.service_type_id = s.service_type_id join
table_4 ppd
on ch.policy_no = ppd.policy_no
group by s.service_sub_type_descr, ch.claim_id;
Much simpler in my opinion.
for column aliases, you have to use double quotes !
don't use
as 'Single-occupancy'
but :
as "Single-occupancy",

What is the syntax problem here using this subquery inside where clause

SELECT p.pnum, p.pname
FROM professor p, class c
WHERE p.pnum = c.pnum AND c.cnum = CS245 AND (SELECT COUNT(*) FROM (SELECT MAX(m.grade), MAX(m.grade) - MIN(m.grade) AS diff
FROM mark m WHERE m.cnum = c.cnum AND m.term = c.term AND m.section = c.section AND diff <= 20)) = 3
Incorrect syntax near ')'. Expecting AS, FOR_PATH, ID, or QUOTED_ID.
Consider the following example:
SELECT COUNT(1)
FROM SYSCAT.TABLES T
WHERE
(
-- SELECT COUNT(1)
-- FROM
-- (
SELECT COUNT(1)
FROM SYSCAT.COLUMNS C
WHERE C.TABSCHEMA=T.TABSCHEMA AND C.TABNAME=T.TABNAME
-- )
) > 50;
The query above works as is. But the problem is, that if you uncomment the commented out lines, you get the following error message: "T.TABNAME" is an undefined name. and least in Db2 for Linux, Unix and Windows.
You can't push external to the sub-select column references too deeply.
So, your query is incorrect.
It's hard to correct it, until you provide the task description with data sample and the result expected.
I can see a potential syntax errors: It seems that CS245 refers to a value c.cnum may take and not a column name. If that is the case, it should be enclosed in single quotes.

Can't find ORA-00907: missing the right parenthesis

I don't know what is wrong with this syntax.
SELECT Count (id_conv) AS NUM_CAMP
FROM csd_mx_mae_camp_dro
WHERE id_conv = (SELECT id_conv
FROM csd_mx_mae_conv_dro
WHERE num_cta = 60385300500)
AND id_cncpt = (SELECT A.id_cncpt
FROM csd_mx_mae_camp_dro A
INNER JOIN csd_mx_mae_cncpt_dro B
ON A.id_cncpt = B.id_cncpt
WHERE ( ( flg_tipo_camp = 'A'
AND txt_nombr_clase_logic IS NOT NULL )
OR ( flg_tipo_camp = 'C' ) )
AND txt_nom NOT IN ( 'Concepto' )
AND B.txt_cve = '84'
AND A.id_conv = (SELECT id_conv
FROM csd_mx_mae_conv_dro
WHERE num_cta = 60385300500)
AND rownum = 1
ORDER BY id_cmp)
AND flg_tipo_camp = 'A';
The expected result is 4, taking into account my records in the DB, however I have the error mentioned in the title (ORA-00907: missing the right parenthesis
00907. 00000 - "missing right parenthesis"
* Cause:
* Action:
Error in the line: 171, column: 90).
There are some subqueries where ORDER BY makes sense - and it is allowed by the syntax.
However, you use ORDER BY in a scalar subquery - one that is required to return a single value (one row / one column), and such subqueries do not allow ORDER BY.
You are using it incorrectly anyway (most likely) - you limit the number of rows to 1 by the condition ROWNUM = 1, which in conjunction with your ORDER BY probably means you wanted to order by ID_CMP and then take the first row from the result. That is not how it works; ORDER BY comes only after ROWNUM is assigned anyway. If that's what you were trying to do, remove ORDER BY as well as the condition on ROWNUM, and instead select MIN(ID_CMP) in the SELECT clause of the scalar subquery.
The specific error about the missing right parenthesis is caused by the ORDER BY clause: at that point, in a scalar subquery, the parser expects the closing parenthesis for the subquery, not any other token/clause/whatever.

isnull with AND : -An expression of non-boolean type specified in a context where a condition is expected, near 'and'

I am tryng to use isnull with AND operator
SELECT *
FROM contacts AS cont
LEFT JOIN contactphones AS contPhone
ON cont.contactid = contPhone.contactid
LEFT JOIN sys_phonetypedesc AS phont
ON phont.typeid = contPhone.phonetype
LEFT JOIN salutations AS tsal
ON tsal.salutid = cont.salutation
WHERE cont.contactid = '29'
AND ( Isnull(phont.typedesc, 1) )
AND ( Isnull(contPhone.phonenum, 1) )
ORDER BY phont.typedesc
but got following error
An expression of non-boolean type specified in a context where a
condition is expected, near 'and'.
I also tried using the case statement
SELECT *
FROM contacts AS cont
LEFT JOIN contactphones AS contphone
ON cont.contactid = contphone.contactid
LEFT JOIN sys_phonetypedesc AS phont
ON phont.typeid = contphone.phonetype
LEFT JOIN salutations AS tsal
ON tsal.salutid = cont.salutation
WHERE cont.contactid = '29'
AND (
CASE
WHEN phont.typedesc = NULL THEN 1
ELSE phont.typedesc
END as a)
but it is not working. I am looking for ifnull logic in MSSQL but case and if else not working correctly
please suggest
I don't think you need to use ISNULL() here, instead you are looking for IS NULL/IS NOT NULL. First let's see why you get this error message
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.
You get this error because ( Isnull(phont.typedesc, 1) ) is not a boolean expression and that what the WHERE clause needs (True or False).
eg: Let's assume phont IS NULL, then ISNULL() will return 1, so you are writing cont.contactid = '29' AND 1 AND ....
What should I do then to get ride of this error?
Just make it a boolean expression as Isnull(phont.typedesc, 1) = 1 or what ever you want instead of =1, it maybe other too because it's not clear what you need to check.
Now, the use of ISNULL() as I seeis point less, cause the possible cases I can see is like the follow:
Isnull(phont.typedesc, 1) = 1 then directly phont IS NULL.
Isnull(phont.typedesc, 1) <> 1 then directly phont IS NOT NULL.
Isnull(phont.typedesc, 1) = AnyValue then why not directly phont = value or phont IN(<Values>) if you are looking for more than 1 value.
You're not comparing the result of isnull to anything in your where clause.
(isnull(phont.TypeDesc,1))
gives you either the TypeDesc, or if it's null gives you 1, but then you move directly onto an AND statement. In other words, if all relevant fields are null, what you'd be trying to do is:
where cont.ContactID = '29' and 1 and 1
You either have the isnull in the wrong place (if you want see it in your selected fields), or you forgot to compare it to something.
If you want to select rows where those fields are actually null then what you want is:
where cont.ContactID = '29'
and phont.TypeDesc is null
and contPhone.PhoneNum is null
select * from contacts as cont left join ContactPhones as contPhone on cont.ContactID=contPhone.ContactID
left join SYS_PhoneTypeDesc as phont on
phont.TypeID = contPhone.PhoneType
left join SALUTATIONS as tsal on tsal.salutid = cont.Salutation where cont.ContactID = '29' and (isnull(phont.TypeDesc,1))=check what u want here
and (isnull(contPhone.PhoneNum,1))=check what u want here enter code here
order by
phont.TypeDesc
Isnull(contPhone.phonenum,1) return 1 when your contPhone.phonenum value will be null else it will return that column value but the point is you got error because you have not used any comparison value as a result it thrown error
Your condition will be like below
Where contactId=29 and isnull(phonenum,1)= // any value

How to give change working of having function dynamicaly on executing an sql statement?

I'm having a Sql code like as follows
Select a.ItemCode, a.ItemDesc
From fn_BOM_Material_Master('A', #AsOnDate, #RptDate, #BranchID, #CompID)a
Left Outer Join fn_INV_AsOnDate_Stock(#StockDate, #AsOnDate, #RptDate, #BranchID, #CompID, #Finyear)b
On a.ItemCode=b.ItemCode and b.WarehouseCode<>'WAP'
and a.BranchID=b.BranchID and a.CompID=b.COmpID
Where a.ItemNatureCode = 'F' and a.BranchID = #BranchID and a.CompID = #CompID
Group by a.ItemCode, a.ItemDesc
Having sum(b.CBQty)<=0
Here the problem is that im passing an "#ShowZeroStock" value as as bit if the "#ShowZeroStock" value is '1' then Having should not be validated or (i.e: All values from the table should be returned including zero)
So How to change the query based on passed bit value "#ShowZeroStock"
I can Use "If else " condition at the top and remove having in else part, but for a lengthy query i can't do the same.
Is this the logic you want?
Having sum(b.CBQty) <= 0 or #ShowZeroStock = 1