SQL Server 2008 view error with case statement - sql

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]

Related

Case statement handling logic differently than expected

I'm trying to assign a status based on the number of IDs using a metric. This is the query I've written (and it works):
select
x.yyyy_mm_dd,
x.prov_id,
x.app,
x.metric,
x.is_100,
case
when ((x.is_100 = 'true') or size(collect_set(x.list)) >10) then 'implemented'
when ((x.is_100 = 'false') and size(collect_set(x.list)) between 1 and 10) then 'first contact'
else 'no contact'
end as impl_status,
size(collect_set(x.list)) as array_size,
collect_set(x.list) as list
from(
select
yyyy_mm_dd,
prov_id,
app,
metric,
is_100,
list
from
my_table
lateral view explode(ids) e as list
) x
group by
1,2,3,4,5
However, the impl_status is incorrect for the second condition in the case statement. In the result set, I can see rows with is_100 = false, array_size between 1 and 10, however the impl_status ends up being 'no contact' instead of 'first contact'. I was thinking maybe between isn't inclusive but it seems to be according to the docs.
I am curious if this works:
(case when x.is_100 or count(distinct x.list) > 10
then 'implemented'
when (not x.is_100) and count(x.list) > 0
then 'first contact'
else 'no contact'
end) as impl_status,
This should be the same logic without the string comparisons -- here is an interesting viewpoint on booleans in Hive. I also think that COUNT() is clearer than the array functionality.
Be sure you have not some hidden space in the string
when (( trim(x.is_100) = 'false') and size(collect_set(x.list)) between 1 and 10) then 'first contact'

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",

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]

Can't pinpoint error near the LIKE keyword

I'm running into an error with a sql inquery near my LIKE statement. Am I formatting the LIKE clause incorrectly within the WHEN clause of a CASE? Here is the code.
SELECT
substring (Cases.FileNumber,4,6) AS [FileNumber]
,Charge.ChargeCode
,Cases.BookedLastName
,Cases.BookedFirstName
,Cases.ArrestDate
,Cases.BookedDOB
,Cases.BookedAge
,Charge.OffenseToDate
,Cases.BookedRace
,Cases.BookedSex
,Charge.OffenseStreetAddress1
,Charge.OffenseCity
,Charge.OffenseState
,Charge.OffenseZipCode
,Charge.ChargeDescription
,(SELECT CASE
WHEN charge1.ChargeDescription LIKE N'%heroin%' THEN 'Heroin'
ELSE 'Not Heroin'
END
FROM tblCsCharge AS charge1
INNER JOIN tblCsCases AS cases1
ON charge1.FileNumber = cases1.FileNumber
WHERE
charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedDOB = Cases.BookedDOB
AND cases2.ChargeCode IN (N'579.015-001Y201735')) AS HeroinYN
FROM
tblCsCases AS Cases
INNER JOIN tblCsCharge AS Charge
ON Cases.FileNumber = Charge.FileNumber
WHERE Cases.IssuedDate >= 01/01/2017
AND
Cases.IssuedDate <= #EndDate
AND
Charge.ChargeCode IN (N'579.015-001Y201735')
AND
Cases.BookedLastName NOT IN (N'Bogus')
ORDER By
Cases.BookedLastName
This is a sub-query within a bigger query focused on building a column that will simply output Heroin or Not Heroin based off of a large text field in our database.
EDITs:
Despite the changes that should make the code compile, the following error now occurs:
Invalid column name 'BookedLastName'.
Invalid column name 'BookedLastName'.
Invalid column name 'BookedDOB'.
The multi-part identifier "cases2.ChargeCode" could not be bound.
You have two errors in your code, both of which were pointed out, though not in the same answer.
Your case statement should be without the column name in parenthesis - I commented it out in the copy of your code below)
You're missing a closing parenthesis at the end of the query, right before the alias - I added it below.
.
,(SELECT CASE--(charge1.ChargeDescription)
WHEN charge1.ChargeDescription LIKE N'%heroin%' THEN 'Heroin'
ELSE 'Not Heroin'
END
FROM tblCsCharge AS charge1
INNER JOIN tblCsCases AS cases1
ON charge1.FileNumber = cases1.FileNumber
WHERE
charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedDOB = Cases.BookedDOB
AND cases2.ChargeCode IN (N'579.015-001Y201735')) AS HeroinYN
This seems to be the issue..
AND cases2.ChargeCode IN (N'579.015-001Y201735') AS HeroinYN
Since this is part of a sub-query, you need another parenthese
AND cases2.ChargeCode IN (N'579.015-001Y201735')) AS HeroinYN
ALSO
You can't put the column name after a CASE statement when you are using an equality operator.
This is acceptable
select
case columnName
when 'X' then 1
when 'Y' then 0
end
This is not
select
case columnName
when = 'X' then 1
when columnName like '%Y%' then 0
end
CORRECTED SCRIPT
,(SELECT CASE
WHEN charge1.ChargeDescription LIKE N'%heroin%' THEN 'Heroin'
ELSE 'Not Heroin'
END
FROM tblCsCharge AS charge1
INNER JOIN tblCsCases AS cases1
ON charge1.FileNumber = cases1.FileNumber
WHERE
charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedDOB = Cases.BookedDOB
AND cases2.ChargeCode IN (N'579.015-001Y201735')) AS HeroinYN
The proper syntax for that is:
CASE
WHEN charge1.ChargeDescription LIKE N'%heroin%' THEN 'Heroin'
ELSE 'Not Heroin'
END
Don't put the field name after CASE in this syntax.

how do I join two tables sql

I have an issue that I'm hoping you can help me with. I am trying to create charting data for performance of an application that I am working on. The first step for me to perform two select statements with my feature turned off and on.
SELECT onSet.testName,
avg(onSet.elapsed) as avgOn,
0 as avgOff
FROM Results onSet
WHERE onSet.pll = 'On'
GROUP BY onSet.testName
union
SELECT offSet1.testName,
0 as avgOn,
avg(offSet1.elapsed) as avgOff
FROM Results offSet1
WHERE offSet1.pll = 'Off'
GROUP BY offSet1.testName
This gives me data that looks like this:
Add,0,11.4160277777777778
Add,11.413625,0
Delete,0,4.5245277777777778
Delete,4.0039861111111111,0
Evidently union is not the correct feature. Since the data needs to look like:
Add,11.413625,11.4160277777777778
Delete,4.0039861111111111,4.5245277777777778
I've been trying to get inner joins to work but I can't get the syntax to work.
Removing the union and trying to put this statement after the select statements also doesn't work. I evidently have the wrong syntax.
inner join xxx ON onSet.testName=offset1.testName
After getting the data to be like this I want to apply one last select statement that will subtract one column from another and give me the difference. So for me it's just one step at a time.
Thanks in advance.
-KAP
I think you can use a single query with conditional aggregation:
SELECT
testName,
AVG(CASE WHEN pll = 'On' THEN elapsed ELSE 0 END) AS avgOn,
AVG(CASE WHEN pll = 'Off' THEN elapsed ELSE 0 END) AS avgOff
FROM Results
GROUP BY testName
I just saw the filemaker tag and have no idea if this work there, but on MySQL I would try something along
SELECT testName, sum(if(pll = 'On',elapsed,0)) as sumOn,
sum(if(pll = 'On',1,0)) as numOn,
sum(if(pll ='Off',elapsed,0)) as sumOff,
sum(if(pll ='Off',1,0)) as numOff,
sumOn/numOn as avgOn,
sumOff/numOff as avgOff
FROM Results
WHERE pll = 'On' or pll='Off'
GROUP BY testName ;
If it works for you then this should be rather efficient as you do not need to join. If not, thumbs pressed that this triggers another idea.
The difficulty you have with the join you envisioned is that the filtering in the WHERE clause is performed after the join was completed. So, you would still not know what records to use to compute the averages. If the above is not implementable with FileMaker then check if nested queries work. You would then
SELECT testName, on.avg as avgOn, off.avg as avgOff
FROM ( SELECT ... FROM Results ...) as on, () as off
JOIN on.testName=off.testName
If that is also not possible then I would look for temporary tables.
OK guys... thanks for the help again. Here is the final answer. The statement below is FileMaker custom function that takes 4 arguments (platform, runID, model and user count. You can see the sql statement is specified. FileMaker executeSQL() function does not support nested select statements, does not support IF statements embedded in select statements (calc functions do of course) and finally does not support the SQL keyword VALUES. FileMaker does support the SQL keyword CASE which is a little more powerful but is a bit wordy. The select statement is in a variable named sql and result is placed in a variable named result. The ExecuteSQL() function works like a printf statement for param text so you can see the swaps do occur.
Let(
[
sql =
"SELECT testName, (sum( CASE WHEN PLL='On' THEN elapsed ELSE 0 END)) as sumOn,
sum( CASE WHEN PLL='On' THEN 1 ELSE 0 END) as countOn,
sum( CASE WHEN PLL='Off' THEN elapsed ELSE 0 END) as sumOff,
sum( CASE WHEN PLL='Off' THEN 1 ELSE 0 END) as countOff
FROM Results
WHERE Platform = ?
and RunID = ?
and Model = ?
and UserCnt = ?
GROUP BY testName";
result = ExecuteSQL ( sql ; "" ; ""
; platform
; runID
; model
; userCnt )
];
getAverages ( Result ; "" ; 2 )
)
For those interested the custom function looks like this:
getAverages( result, newList, pos )
Let (
[
curValues = Substitute( GetValue( data; pos ); ","; ¶ );
sumOn = GetValue( curValues; 2 ) ;
countOn = GetValue( curValues; 3 );
sumOff = GetValue( curValues; 4 );
countOff = GetValue( curValues; 5 );
avgOn = sumOn / countOn;
avgOff = sumOff / countOff
newItem = ((avgOff - avgOn) / avgOff ) * 100
];
newList & If ( pos > ValueCount( data); newList;
getAverages( data; If ( not IsEmpty( newList); ¶ ) & newItem; pos + 1 ))
)