Getting error ORA-00936: missing expression - sql

When I trying the below SQL I am getting the error ORA-00936: missing expression. Please help me on this, I want distinct on those to columns in Oracle SQL
SELECT rr.RPT_QUE_I,
DISTINCT (rr.ed_sbmt_m, rr.RPT_RUN_STAT_C),
rr.rpt_cstm_x,
rr.rpt_cmplt_m,
CASE
WHEN rr.rpt_run_stat_c = 25453 THEN 'PENDING'
WHEN rr.rpt_run_stat_c = 25454 THEN 'ACTIVE'
WHEN rr.rpt_run_stat_c = 25455 THEN 'FINISHED'
WHEN rr.rpt_run_stat_c = 25458 THEN 'ERROR'
WHEN rr.rpt_run_stat_c = 25460 THEN 'SCHEDULED'
END,
cc.pro_sym_I,
rr.usr_wad_ownr_i
FROM audit_REPORT_RUN rr,
CLIENT_COMPANY cc
WHERE rr.ED_SBMT_M > TO_DATE('06/01/2012','mm/dd/yyyy')
AND rr.ED_SBMT_M < TO_DATE('07/01/2012','mm/dd/yyyy')
AND rr.ORG_I = cc.ORG_I
ORDER BY rr.RPT_QUE_I

You should use DISTINCT keyword at first after the SELECT keyword, and remove the parenthesis,
or if you need to categorize some of it, use GROUP BY functions. hope you're getting the desired result.

Related

SQL query using if else

My SQL code looks like this:
SELECT
Scores.PupilId, Scores.BoysName, Scores.FormGroup,
IF (Scores.FormGroup = "10SB", "Great", "ok")
FROM
Scores
I get this message
no such function: if: SELECT Scores.PupilId, Scores.BoysName, Scores.FormGroup,
if(Scores.FormGroup="10SB","Great","ok")
FROM Scores
This is flat file database
Can anyone please help me understand why I am getting a message?
The correct ANSI-standard conditional expression in SQL is the case expression:
SELECT Scores.PupilId, Scores.BoysName, Scores.FormGroup,
(CASE WHEN Scores.FormGroup = '10SB' THEN 'Great' ELSE 'ok' END)
FROM Scores ;

CASE WHEN SQL Syntax Error

I am attempting to us the CASE statement for the first time and I cannot understand why I am getting a syntax error on the last WHEN and ELSE. I am trying to extract a substring if a value starts with a specific set of characters. My Code is below:
SELECT
CASE
WHEN LEFT ([RCode],2) = 'BB' THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT ([RCode],4) = 'APT-' THEN SUBSTRING([RCode],5,LEN([RCode])
WHEN LEFT ([RCode],4) = 'PS-' THEN SUBSTRING([RCode],4,LEN([RCode])
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]
Any guidance would be appreciated.
This won't (necessarily) fix your syntax problem, but I would recommend that you use like for the comparisons:
SELECT (CASE WHEN RCode LIKE 'BB%' THEN SUBSTRING([RCode], 3, LEN([RCode]))
WHEN RCode LIKE 'APT-%' THEN SUBSTRING([RCode], 5, LEN([RCode]))
WHEN RCode LIKE 'PS-%' THEN SUBSTRING([RCode], 4, LEN([RCode]))
ELSE [RCode]
END)
FROM [Xperdyte].[dbo].[tJCLines];
Then you don't have to count characters -- and the third condition will match.
Missed off two right parenthesis.
SELECT CASE WHEN LEFT([RCode],2) = 'BB'
THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT([RCode],4) = 'APT-'
THEN SUBSTRING([RCode],5,LEN([RCode]))
WHEN LEFT([RCode],4) = 'PS-'
THEN SUBSTRING([RCode],4,LEN([RCode]))
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]

sql select query - alias name for column in joins is not working

I have a query which is working fine..
SELECT
WSDEFN.WORKSPACE_DISPLAY_NM,
LAYDEFN.LAYOUT_DISPLAY_NM,
WSLMAP.POSITION,
LAYDEFN.LAYOUT,
LAYDEFN.PROPORTION,
LAYDEFN.LAYOUT_ID,
WSDEFN.WORKSPACE_ID,
LAYDEFN.BUNDLE_KEY
FROM
WORKSPACE_DEFINITION WSDEFN,
WORKSPACE_LAYOUT_MAP WSLMAP,
LAYOUT_DEFINITION LAYDEFN
WHERE
WORKSPACE_ID = WSLMAP.WORKSPACE_ID
AND WSLMAP.LAYOUT_ID = LAYDEFN.LAYOUT_ID
AND WSDEFN.OD_USER_NO = '-1'
AND WSDEFN.OD_GCIF = '-1'
ORDER BY
wsdefn.workspace_id, WSLMAP.POSITION
But I want to fetch the WSDEFN.WORKSPACE_ID as WORKSPACE_ID.
I tried this
WSDEFN.WORKSPACE_ID AS WORKSPACE_ID,
but I get an error
'column ambiguously defined'
Could you give me a solution?
It seems to be missing the table alias in the where clause.
Did you try this?
SELECT
WSDEFN.WORKSPACE_DISPLAY_NM,
LAYDEFN.LAYOUT_DISPLAY_NM,
WSLMAP.POSITION,
LAYDEFN.LAYOUT,
LAYDEFN.PROPORTION,
LAYDEFN.LAYOUT_ID,
WSDEFN.WORKSPACE_ID AS WORKSPACE_ID,
LAYDEFN.BUNDLE_KEY
FROM
WORKSPACE_DEFINITION WSDEFN,
WORKSPACE_LAYOUT_MAP WSLMAP,
LAYOUT_DEFINITION LAYDEFN
WHERE
WSDEFN.WORKSPACE_ID = WSLMAP.WORKSPACE_ID
AND
WSLMAP.LAYOUT_ID = LAYDEFN.LAYOUT_ID
AND
WSDEFN.OD_USER_NO='-1' AND WSDEFN.OD_GCIF='-1'
ORDER BY
wsdefn.workspace_id,WSLMAP.POSITION

Oracle: Case in Where clause

How to achieve this in Oracle query. I am getting missing expression error in IN clause
WHERE TBL_DTL_HOST.HOST_METHOD = 'CCtxnPostRq'
AND TRUNC(TBL_DTL_FEATURE.START_DATETIME) BETWEEN TO_DATE (i_startdate, 'DD/MM/YYYY') AND TO_DATE (i_enddate,'DD/MM/YYYY')
AND (SELECTED_DNIS IS NULL OR TBL_DTL_FEATURE.DNIS = SELECTED_DNIS)
AND (TEMP_CUSTOMER_ID IS NULL OR TBL_DTL_FEATURE.CUSTOMER_ID = TEMP_CUSTOMER_ID)
AND
(CASE WHEN i_Feature='All'
THEN (TBL_DTL_HOST.FEATURE_ID IN ('F020','F021'))
ELSE (TBL_DTL_HOST.FEATURE_ID IN ('F020'));
END)
am i missing anything..? Any help would be appreciated..Thanks
Oracle doesn't treat Boolean expressions like other expressions — it handles them as syntax rather than as a type — so CASE expressions can't evaluate to Booleans.
In your case, I think the clearest code is if you just rewrite it a bit:
AND ( TBL_DTL_HOST.FEATURE_ID = 'F020'
OR (i_Feature = 'All' AND TBL_DTL_HOST.FEATURE_ID = 'F021')
)
the last part of the clause should be like this to evaluate correctly
(CASE WHEN i_Feature='All' AND (TBL_DTL_HOST.FEATURE_ID IN ('F020','F021')) THEN 1
WHEN i_Feature<>'All' AND (TBL_DTL_HOST.FEATURE_ID IN ('F020')) THEN 1
ELSE 0
END) = 1

SQL Server 2008 view error with case statement

I cant figure out why this code executes as a query but when I execute it in a view it throws a the multi-part identifier could not be bound, error.
When I take out the case statement It works in the view, so I believe it is something that has to do with the case statement.
Any suggestions are appreciated.
WITH [cteFrostSum] AS
(
SELECT ID AS ID, theMonth as Mo,
SUM(dbo.Frost.[DRAmount]) AS [DRAmount]
FROM dbo.Frost
GROUP BY [ID], theMonth
)
SELECT DISTINCT
TOP (100) PERCENT
dbo.ternean.MemberID,
dbo.ternean.SSN,
dbo.ternean.GroupName,
dbo.ternean.CustomerID,
dbo.ternean.GroupNumber,
dbo.ternean.LastName,
dbo.Frost.DRAmount,
dbo.Frost.HittheBank,
dbo.Frost.MonthofPremium,
cte.[DRAmount] AS [SUM_Frost_Balance],
dbo.ternean.TotalCost,
cte.[DRAmount] - dbo.ternean.TotalCost AS Diff,
dbo.ternean.ACH_RoutingNo,
dbo.Frost.RTNum,
dbo.ternean.ACH_AcctNo,
dbo.Frost.AccountNumber,
CASE
WHEN dbo.Frost.RTNum <> SUBSTRING(dbo.ternean.ACH_RoutingNo, 2, 20)
THEN 'DO not match'
WHEN dbo.Frost.RTNum = SUBSTRING(dbo.ternean.ACH_RoutingNo, 2, 20)
THEN 'match'
END AS [Routing # match],
CASE
WHEN SUBSTRING(dbo.ternean.ACH_AcctNo, 2, 20) <> dbo.Frost.AccountNumber
THEN 'DO not match'
WHEN SUBSTRING(dbo.ternean.ACH_AcctNo, 2, 20) = dbo.Frost.AccountNumber
THEN 'match'
END AS [Account # match],
dbo.Frost.theMonth
FROM dbo.Frost
INNER JOIN dbo.ternean ON dbo.Frost.ID = dbo.ternean.CustomerID
AND dbo.Frost.theMonth = dbo.ternean.theMonth
INNER JOIN [cteFrostSum] cte ON dbo.Frost.ID = cte.ID
AND dbo.Frost.theMonth = cte.Mo
ORDER BY dbo.ternean.theMonth
I tried to replicate your error but couldn't.
Why are you using multi-part identifiers the field names anyway? The list of fields in the select statement can only refer to the tables in the from clause, at first reading this query seems to be referring to the tables directly in the dbo schema.
Give your tables some nice easy aliases i.e.
FROM dbo.Frost AS F
and use them like this
F.RTNum
Secondly you can simplify your case statements and only do one test i.e.:
CASE WHEN SUBSTRING(T.ACH_AcctNo, 2, 20) <> F.AccountNumber
THEN 'DO not match'
ELSE 'match'
END AS [Account # match]