Error on hive query: Invalid function MOD - hive

I need to create a View on HIVE by converting a query from Oracle, but I'm getting an error when running the query.
Here is the error message from HUE:
Error while compiling statement: FAILED:SemanticException [Error 10011]:Invalid function MOD
Here is a sample of my query:
CREATE VIEW crp_db_name.v_new_view (
OAA_ID COMMENT ' r',
DT_RF COMMENT ' ',
OAR_CD COMMENT ' ',
AAAA COMMENT ' ',
BBBB COMMENT ' ',
CCCCC COMMENT ' ',
DDDD COMMENT '',
EEEE COMMENT ' ',
FFFF COMMENT '',
GGGG COMMENT ' ',
HHHHH COMMENT ' ',
IIII COMMENT ' ',
JJJJ COMMENT ' ',
KKKK COMMENT ' ',
LLLLL COMMENT ' ',
MMMM COMMENT ' ',
NNNN COMMENT ' ',
OOOO COMMENT ' ',
RA_CD COMMENT ' ',
RA_NM COMMENT ' ',
RA_GGGG COMMENT ' ',
RA_HHHHH COMMENT ' ',
PPPP COMMENT ' ',
QQQQ COMMENT ' ',
RRRRR COMMENT ' ',
SSSSS COMMENT ' ',
TTTTT COMMENT ' ',
UUUUU COMMENT ' ',
VVVVV COMMENT ' ',
WWWW COMMENT ' ',
XXXXX COMMENT ' ',
YYYYY COMMENT ' ',
ZZZZZ COMMENT ' ',
AQAQAQA COMMENT ' ',
SWSWSWS COMMENT ' ',
FRFRFRFFR COMMENT ' '
) COMMENT ' '
AS
SELECT OA.EA_ID AS OAA_ID,
OA.DT_RF,
OA.ER_CD AS OAR_CD,
OA.AAAA,
DTST.BBBB,
TR.CCCCC,
OA.DDDD,
OA.EEEE,
OA.FFFF,
OA.GGGG,
OA.HHHHH,
OA.IIII,
OA.JJJJ,
OA.KKKK,
OA.LLLLL,
OA.MMMM,
OA.NNNN,
OA.OOOO,
CASE
WHEN OA.DDDD IN ('G1', 'G2')
THEN
OA.ER_CD
WHEN OA.DDDD = 'G3'
THEN
CASE
WHEN TR.CCCCC = 'T1M'
THEN
CASE
WHEN OA.ZZZZZ = 'HOST'
THEN
OA.ER_CD
ELSE
OA.KKKK
END
ELSE
CASE
WHEN OA.AQAQAQA = 'HOST'
THEN
OA.ER_CD
ELSE
OA.KKKK
END
END
WHEN OA.DDDD = 'G4'
THEN
OA.KKKK
END
AS RA_CD,
CASE
WHEN OA.DDDD IN ('G1', 'G2')
THEN
OA.FFFF
WHEN OA.DDDD = 'G3'
THEN
CASE
WHEN TR.CCCCC = 'T1M'
THEN
CASE
WHEN OA.ZZZZZ = 'HOST' THEN OA.FFFF
ELSE OA.LLLLL
END
ELSE
CASE
WHEN OA.AQAQAQA = 'HOST' THEN OA.FFFF
ELSE OA.LLLLL
END
END
WHEN OA.DDDD = 'G4'
THEN
OA.LLLLL
END
AS RA_NM,
CASE
WHEN OA.DDDD IN ('G1', 'G2')
THEN
OA.GGGG
WHEN OA.DDDD = 'G3'
THEN
CASE
WHEN TR.CCCCC = 'T1M'
THEN
CASE
WHEN OA.ZZZZZ = 'HOST' THEN OA.GGGG
ELSE OA.MMMM
END
ELSE
CASE
WHEN OA.AQAQAQA = 'HOST' THEN OA.GGGG
ELSE OA.MMMM
END
END
WHEN OA.DDDD = 'G4'
THEN
OA.MMMM
END
AS RA_GGGG,
CASE
WHEN OA.DDDD IN ('G1', 'G2')
THEN
OA.HHHHH
WHEN OA.DDDD = 'G3'
THEN
CASE
WHEN TR.CCCCC = 'T1M'
THEN
CASE
WHEN OA.ZZZZZ = 'HOST' THEN OA.HHHHH
ELSE OA.NNNN
END
ELSE
CASE
WHEN OA.AQAQAQA = 'HOST' THEN OA.HHHHH
ELSE OA.NNNN
END
END
WHEN OA.DDDD = 'G4'
THEN
OA.NNNN
END
AS RA_HHHHH,
CASE
WHEN OA.VVVVV = 1
THEN
CASE
WHEN OA.IIII = 1
THEN
CASE
WHEN TR.CCCCC = 'T2Q' THEN 'ITS + 15th WD'
ELSE 'RD + 30th WD'
END
ELSE
CASE
WHEN TR.CCCCC = 'T2Q' THEN 'ITS + 20th WD'
ELSE 'RD + 35th WD'
END
END
ELSE
'-99'
END
AS PPPP,
CASE
WHEN OA.VVVVV = 1
THEN
CASE
WHEN MOD (OA.DT_RF, 100) IN (3,
6,
9,
12)
THEN
1
ELSE
CASE WHEN OA.WWWW = 1 THEN 0 ELSE 1 END
END
ELSE
0
END
AS QQQQ,
CASE
WHEN TR.CCCCC = 'T1M' THEN NVL (DTST.AZAZAZZA, 0)
WHEN TR.CCCCC = 'T2M' THEN NVL (DTST.GHGHGHGH, 0)
WHEN TR.CCCCC = 'T2Q' THEN NVL (DTST.TYTYTYTY, 0)
ELSE 0
END
AS RRRRR,
CASE
WHEN TR.CCCCC = 'T1M'
THEN
NVL (DTST.PLPLPLPLP, 0)
WHEN TR.CCCCC = 'T2M'
THEN
NVL (DTST.OUOIUOIUOI, 0)
WHEN TR.CCCCC = 'T2Q'
THEN
NVL (DTST.RIRORIRORI, 0)
ELSE
0
END
AS SSSSS,
OA.TTTTT,
CASE
WHEN OA.VVVVV <> 1
AND ( ( NVL (DTST.AZAZAZZA, 0)
+ NVL (DTST.GHGHGHGH, 0)
+ NVL (DTST.TYTYTYTY, 0)
+ NVL (DTST.PLPLPLPLP, 0)
+ NVL (DTST.OUOIUOIUOI, 0)
+ NVL (DTST.RIRORIRORI, 0)) > 0)
THEN
1
ELSE
0
END
AS UUUUU,
OA.VVVVV,
OA.WWWW,
OA.XXXXX,
OA.YYYYY,
OA.ZZZZZ,
OA.AQAQAQA,
OA.SWSWSWS,
CASE
WHEN TO_NUMBER (TO_CHAR (SYSDATE, 'yyyymmdd')) >
OA.SWSWSWS
THEN
'TRUE'
ELSE
'FALSE'
END
AS FRFRFRFFR
FROM crp_db_name.A_D_OBSRVD_AGNT_BY_DT_RF OA
JOIN crp_db_name.A_ADM_CCCCC TR
ON (IND_UNQ_MNTH = 1
AND MOD (OA.DT_RF, 100) = TR.MNTH_NMBR)
LEFT OUTER JOIN crp_db_name.A_ADM_RD_OA_DTST DTST
ON (OA.EA_ID = DTST.OAA_ID
AND OA.DT_RF = DTST.DT_RF);
The query is quite long and I'm not sure which exactly causes the error.

Related

How to replace the phrase "1234 Address" by an empty string for the aliased fields

When I run this code, the fields Old_Address and New_Address are containing 1234 Address. I would like the result to change 1234 Address to an empty string each time found. What is an easy way to do this?
INSERT
INTO OPT_INT.mgnNACHG_Results
(
BRANCH_CD,
ACCOUNT_CD,
RR_CD,
ALT_BRANCH_CD,
[OldAddress],
[NewAddress],
[AddressUpdate],
[OldName],
[NewName],
[NameUpdate]
)
SELECT A.BRANCH_CD,
A.ACCOUNT_CD,
A.RR_CD,
A.ALT_BRANCH_CD,
LTRIM(RTRIM(ISNULL(B.LINE4, '')))
+ LTRIM(RTRIM(ISNULL(B.LINE5, '')))
+ LTRIM(RTRIM(ISNULL(B.LINE6, ''))) AS 'Old_Address',
LTRIM(RTRIM(ISNULL(A.LINE4, '')))
+ LTRIM(RTRIM(ISNULL(A.LINE5, '')))
+LTRIM(RTRIM(ISNULL(A.LINE6, ''))) AS 'New_Address',
CASE
WHEN (LTRIM(RTRIM(ISNULL(B.LINE4, ''))) <> LTRIM(RTRIM(ISNULL(A.LINE4, ''))))
OR (LTRIM(RTRIM(ISNULL(B.LINE5,''))) <> LTRIM(RTRIM(ISNULL(A.LINE5, ''))))
OR (LTRIM(RTRIM(ISNULL(B.LINE6, ''))) <> LTRIM(RTRIM(ISNULL(A.LINE6, ''))))
THEN 'Address Change'
ELSE ''
END as 'Address_Update',
LTRIM(RTRIM(ISNULL(B.LINE1, '')))
+ LTRIM(RTRIM(ISNULL(B.LINE2, '')))
+ LTRIM(RTRIM(ISNULL(B.LINE3, ''))) AS 'Client_Old_Name',
LTRIM(RTRIM(ISNULL(A.LINE1, '')))
+ LTRIM(RTRIM(ISNULL(A.LINE2, '')))
+ LTRIM(RTRIM(ISNULL(A.LINE3, ''))) AS 'Client_New_Name',
CASE
WHEN (LTRIM(RTRIM(ISNULL(B.LINE1,''))) <> LTRIM(RTRIM(ISNULL(A.LINE1, ''))))
OR (LTRIM(RTRIM(ISNULL(B.LINE2, ''))) <> LTRIM(RTRIM(ISNULL(A.LINE2, ''))))
OR (LTRIM(RTRIM(ISNULL(B.LINE3, ''))) <> LTRIM(RTRIM(ISNULL(A.LINE3, ''))))
THEN 'Title Change'
ELSE ''
END AS 'Client_Name_Update'
FROM #NEW A
INNER JOIN #OLD B
ON A.BRANCH_CD = B.BRANCH_CD
AND A.ACCOUNT_CD = B.ACCOUNT_CD
WHERE (A.[LINE1] <> B.[LINE1])
OR (A.[LINE2] <> B.[LINE2])
OR (A.[LINE3] <> B.[LINE3])
OR (A.[LINE4] <> B.[LINE4])
OR (A.[LINE5] <> B.[LINE5])
OR (A.[LINE6] <> B.[LINE6])
See whether this helps
(Assumed that the requirement is [Old_Address] = [New_Address])
INSERT INTO OPT_INT.mgnNACHG_Results (BRANCH_CD,ACCOUNT_CD,RR_CD,ALT_BRANCH_CD,[OldAddress],[NewAddress],[AddressUpdate],[OldName],[NewName],[NameUpdate])
Select BRANCH_CD, ACCOUNT_CD, ACCOUNT_CD, ALT_BRANCH_CD,
Case When [Old_Address] = [New_Address] then ' blank or 1234 Address as you need ' Else [Old_Address] End as [Old_Address],
Case When [Old_Address] = [New_Address] then ' blank or 1234 Address as you need ' Else [New_Address] End as [New_Address],
[Address_Update], [Client_Old_Name], [Client_New_Name], [Client_Name_Update]
From
(
SELECT A.BRANCH_CD,A.ACCOUNT_CD,A.RR_CD,A.ALT_BRANCH_CD
,LTRIM(RTRIM(ISNULL(B.LINE4,'')))+LTRIM(RTRIM(ISNULL(B.LINE5,'')))+LTRIM(RTRIM(ISNULL(B.LINE6,''))) AS [Old_Address]
,LTRIM(RTRIM(ISNULL(A.LINE4,'')))+LTRIM(RTRIM(ISNULL(A.LINE5,'')))+LTRIM(RTRIM(ISNULL(A.LINE6,''))) AS [New_Address]
,CASE WHEN (LTRIM(RTRIM(ISNULL(B.LINE4,'')))<>LTRIM(RTRIM(ISNULL(A.LINE4,'')))) OR (LTRIM(RTRIM(ISNULL(B.LINE5,'')))<>LTRIM(RTRIM(ISNULL(A.LINE5,'')))) OR (LTRIM(RTRIM(ISNULL(B.LINE6,'')))<>LTRIM(RTRIM(ISNULL(A.LINE6,'')))) THEN 'Address Change' ELSE '' END as [Address_Update]
,LTRIM(RTRIM(ISNULL(B.LINE1,'')))+LTRIM(RTRIM(ISNULL(B.LINE2,'')))+LTRIM(RTRIM(ISNULL(B.LINE3,''))) AS [Client_Old_Name]
,LTRIM(RTRIM(ISNULL(A.LINE1,'')))+LTRIM(RTRIM(ISNULL(A.LINE2,'')))+LTRIM(RTRIM(ISNULL(A.LINE3,''))) AS [Client_New_Name]
,CASE WHEN (LTRIM(RTRIM(ISNULL(B.LINE1,'')))<>LTRIM(RTRIM(ISNULL(A.LINE1,'')))) OR (LTRIM(RTRIM(ISNULL(B.LINE2,'')))<>LTRIM(RTRIM(ISNULL(A.LINE2,'')))) OR (LTRIM(RTRIM(ISNULL(B.LINE3,'')))<>LTRIM(RTRIM(ISNULL(A.LINE3,'')))) THEN 'Title Change' ELSE '' END AS [Client_Name_Update]
FROM #NEW A
INNER JOIN #OLD B ON A.BRANCH_CD=B.BRANCH_CD AND A.ACCOUNT_CD=B.ACCOUNT_CD
WHERE (A.[LINE1]<>B.[LINE1]) OR (A.[LINE2]<>B.[LINE2]) OR (A.[LINE3]<>B.[LINE3]) OR (A.[LINE4]<>B.[LINE4]) OR (A.[LINE5]<>B.[LINE5]) OR (A.[LINE6]<>B.[LINE6])
) AS Q
If in case you need to check whether the 2 data fields containing a particular text, you may use
Case When [Old_Address] = [New_Address] and [Old_Address] = '1234 Address' then ' blank or 1234 Address as you need ' Else [Old_Address] End as [Old_Address],

Rolling multiple data rows into 1 row with 'NA' or 'None' displayed if NULL

I am using the below statement to retrieve values in one line.
I would like to display 'NA' or 'None' if there is no Value.
How do I include this in the below statement?
STUFF((SELECT CASE WHEN COUNT(pp.sPetType) > 0 and pp.sPetIsServiceAnimal = 'Yes' THEN
CASE WHEN COUNT(pp.sPetType) = 1 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '1 ' + pp.sPetType ELSE
CASE WHEN COUNT(pp.sPetType) = 2 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '2 ' + pp.sPetType + 's' ELSE
CASE WHEN COUNT(pp.sPetType) = 3 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '3 ' + pp.sPetType + 's'
END END END ELSE 'None' END
FROM person_pet pp
WHERE t.hMyPerson = pp.hMyPerson
GROUP BY pp.sPetType, pp.sPetIsServiceAnimal
FOR XML Path('')), 1, 1, '') [ServiceAnimal]
If I understood correctly :
Add another CASE in the outer case like this:
CASE WHEN (pp.sPetType IS NULL OR pp.sPetType = '') THEN 'NA'
STUFF((SELECT ISNULL(CASE WHEN COUNT(pp.sPetType) > 0 and pp.sPetIsServiceAnimal = 'Yes' THEN
CASE WHEN COUNT(pp.sPetType) = 1 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '1 ' + pp.sPetType ELSE
CASE WHEN COUNT(pp.sPetType) = 2 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '2 ' + pp.sPetType + 's' ELSE
CASE WHEN COUNT(pp.sPetType) = 3 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '3 ' + pp.sPetType + 's'
END END END ELSE 'None' END,'None')
FROM person_pet pp
WHERE t.hMyPerson = pp.hMyPerson
GROUP BY pp.sPetType, pp.sPetIsServiceAnimal
FOR XML Path('')), 1, 1, '') [ServiceAnimal]
I am assuming you are wanting the [ServiceAnimal] to have a result of 'NA' if it is null. This is a simple way to achieve that.
ISNULL(STUFF((SELECT CASE WHEN COUNT(pp.sPetType) > 0 and pp.sPetIsServiceAnimal = 'Yes' THEN
CASE WHEN COUNT(pp.sPetType) = 1 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '1 ' + pp.sPetType ELSE
CASE WHEN COUNT(pp.sPetType) = 2 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '2 ' + pp.sPetType + 's' ELSE
CASE WHEN COUNT(pp.sPetType) = 3 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '3 ' + pp.sPetType + 's'
END END END ELSE 'None' END
FROM person_pet pp
WHERE t.hMyPerson = pp.hMyPerson
GROUP BY pp.sPetType, pp.sPetIsServiceAnimal
FOR XML Path('')), 1, 1, ''), 'NA') [ServiceAnimal]

If true do something else skip SQL

Hello, I have data that looks like this:
I'm trying to come with the code that will calculate the 'MERGE' column.
Basically, I should check, if CLM_x >0 then take the value from SZ_x and concat with the value in CLM_x.
I'm trying to use case when, however I don't know how to skip merging if CLM_x =0:
CASE WHEN CLM_TBL1 > 0 THEN ('Size ' + SZ_1 + '-Qty '+CLM_1) else ... end ...
Please advise, Thank you!
Yuck. This is a bunch of string arithmetic:
select stuff( ((case when clm1_1 > 0 then concat(', Size ', sz_1, '-Qty ', clm1_1) else '' end) +
(case when clm1_2 > 0 then concat(', Size ', sz_2, '-Qty ', clm1_2) else '' end) +
(case when clm1_3 > 0 then concat(', Size ', sz_3, '-Qty ', clm1_3) else '' end)
), 1, 2, ''
) as merge_column
You just need to string the case statements together.
merge =
case
when CLM_1 > 0 then 'Size ' + SZ_1 + '-Qty '+ CLM_1 + ' '
else ''
end
+
case
when CLM_2 > 0 then 'Size ' + SZ_2 + '-Qty '+ CLM_2 + ' '
else ''
end
+
case
when CLM_3 > 0 then 'Size ' + SZ_3 + '-Qty '+ CLM_3 + ' '
else ''
end

RTRIM & LTRIM Function with Case Statement

How do i use the LTRIM & RTRIM with the following SQL? I need to LTRIM and RTRIM all these fields for leading spaces
UPDATE CORE.WeccoPartyAddress
SET AddressElements = CONTROL.TrimChar(
CASE when COALESCE(Address1,'') != '' THEN Address1 + ', ' ELSE '' END +
CASE when COALESCE(Address2,'') != '' THEN Address2 + ', ' ELSE '' END +
CASE when COALESCE(Address3,'') != '' THEN Address3 + ', ' ELSE '' END +
CASE when COALESCE(Town,'') != '' THEN Town + ', ' ELSE '' END +
CASE when COALESCE(County,'') != '' THEN County + ', ' ELSE '' END +
CASE when COALESCE(Postcode,'') != '' THEN Postcode ELSE '' END, ', '
)
Use like below nested:
UPDATE CORE.WeccoPartyAddress SET AddressElements = rtrim(ltrim(CASE when COALESCE(Address1,'') != '' THEN Address1 + ', ' ELSE '' END + CASE when COALESCE(Address2,'') != '' THEN Address2 + ', ' ELSE '' END + CASE when COALESCE(Address3,'') != '' THEN Address3 + ', ' ELSE '' END + CASE when COALESCE(Town,'') != '' THEN Town + ', ' ELSE '' END + CASE when COALESCE(County,'') != '' THEN County + ', ' ELSE '' END + CASE when COALESCE(Postcode,'') != '' THEN Postcode ELSE '' END))
You don't have to use CASE in your statement
UPDATE CORE.WeccoPartyAddress
SET AddressElements = ISNULL( STUFF (
COALESCE( ', ' + LTRIM( RTRIM(Address1) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Address2) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Address3) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Town) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1County) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Postcode) ) , '')
,1
,2
,''
), '')
If any of Address values is not null you will get string like this: ', Address', then using the function STUFF you replace ', ' at the beginning of the string to get 'Address' as the result.
If all values are null the STUFF function will return NULL which will be replaced with '' by ISNULL function.

T SQL Conditional String Concatenation

Have a 5 columns of address data. I need to concatenate these fields into a single address with spaces in between the values if they exist. If the column has a null value I should skip it and not enter any space.
select
case
when street_number != '' THEN (cast(street_number as int))
end as street_number,
case
when street_ext != '' then
case
when street_ext = 50 then '1/2'
end
end as street_ext,
case
when street_direct ! = '' then street_direct
end as street_direct,
case
when site_street ! = '' then site_street
end as site_street,
case
when site_address ! = '' then site_address
end as site_address
from parcel
what I'd like to do is have a variable and assign it to the value of the first column street_number, then when I move on to the next column, street_ext, if it isn't null I'd like to check to see if the variable is null and if not, append a space and the value...and so on down the road.
I'm rusty as hell and could use a push in the right direction.
Thanks everyone.
Use the "+" to concatenate strings in TSQL:
SELECT CASE
WHEN LEN(p.street_number) > 0 THEN p.street_number + ' '
ELSE ''
END +
CASE
WHEN p.street_ext = 50 THEN '1/2'
WHEN LEN(p.street_ext) > 0 THEN ''
ELSE p.street_ext
END + ' ' +
CASE
WHEN LEN(p.street_direct) > 0 THEN p.street_direct + ' '
ELSE ''
END +
CASE
WHEN LEN(p.site_street) > 0 THEN p.site_street + ' '
ELSE ''
END +
CASE
WHEN LEN(p.site_address) > 0 THEN p.site_address + ' '
ELSE ''
END AS full_address
FROM PARCEL p
The LEN function returns zero if the string value is NULL, or a zero length string.
Nested isnulls could do what you need. Something like:
SELECT
ISNULL(streetnumber + ' ', '')
+ ISNULL(streetext + ' ', '')
etc
relying on the fact that NULL + ' ' = NULL.
Something along the lines of:
select coalesce(street_number+' ','')+
coalesce(case when street_ext=50 then '1/2' else null end+' ','')+
coalesce(street_direct+' ','')+
coalesce(site_street+' ','')+
coalesce(site_address,'')
from parcel
I have assumed your data types are all varchar or similar for simplicity. If you are OK with removing any double spaces, how about:
rtrim(ltrim(replace(isnull(street_number) + ' '
+ isnull(street_ext) + ' '
+ isnull(street_direct) + ' '
+ isnull(site_street) + ' '
+ isnull(site_address), ' ', ' ')))
First I would declare the seperator as a variable, because customers are notorious for changing these.
I would do this as follows:
DECLARE #AddressSeperator NVARCHAR(5) = ' '
...and then for the column declation, I'd use the following:
, CONCAT
(
(CASE WHEN LEN(p.street_number) > 0 THEN p.street_number + #AddressSeperator ELSE '' END)
, (CASE WHEN p.street_ext = 50 THEN '1/2' + #AddressSeperator WHEN LEN(p.street_ext) > 0 THEN p.street_ext + #AddressSeperator ELSE '' END)
, (CASE WHEN LEN(p.street_direct) > 0 THEN p.street_direct + #AddressSeperator ELSE '' END)
, (CASE WHEN LEN(p.site_street) > 0 THEN p.site_street + #AddressSeperator ELSE '' END)
, ISNULL(p.site_address, '')
) AS [full_address]