Data conversion or data mapping error HY000 in multi row concatenation in sub select in view - sql

I have a sub-select as part of a view that generates a concatenated list of attributes from sub table. When doing a select from the view the data displays fine, but if it do a select in the concatenated list field I sometimes get the error Select or omit error on field.
AS400: V7R1M0
Main enitity:
MSGOCCID : CHAR 20
other fields.....
The detail entity fields (MSDPF):
MSGOCCID : CHAR 20
OCC : Integer 4
FIELDVAL : VARCHAR 128
I got the code for the sub select from StackOverflow and modified it for my situation. (I have also tried the recursive select but it very slow. The XMLSERIALIZE route is executable.)
To try and solve the problem it have added and removed REPLACE,COALESCE and TRIM of the value. I have added CCSID 1208 to the cast, changed the CHAR cast to VARCHAR... not change. (I have change CCSID 1208 to CCSID 37 at one stage to see if that will not solve it.)
If I run the select:
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP
where 1=1
If returns all the rows with not errors.
Sample output:
MSGOCCID: 2019020443165590
MSGDLIST: 14620, 1, C20180914023575582, 4179792C, C20180914023575582, 4179792C, WIPSTOCK, REMOVE, ROU07561
But if I use:
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP where 1=1 and DETLIST like '%4179792C%'
It fails with on the IBM backend:
Data conversion or data mapping error.
Select or omit error on field MapXmlData(Cast(Concat(',
',Strip(Replace(Cast(P6MSDPF_4.FIELDVAL AS VarChar(128) CCSID
1208),'-'), Both,' ')) AS VarChar(260) CCSID 1208)) member
COMMSWKBL1. Select/omit error on member COMMSWKBL1.
Short version of the view:
CREATE VIEW WBVIEW (
MSGOCCID ,
DETLIST )
AS
(SELECT MSG.MSGOCCID,
FROM P6DEVCDB00.P6MSGPF MSG
LEFT OUTER JOIN LATERAL
(SELECT XMLSERIALIZE(XMLAGG(XMLTEXT(CONCAT(', ',TRIM(REPLACE(REPLACE(REPLACE(FIELDVAL,',','-'),'>','-'),'<','-'))))) AS VARCHAR(4096) CCSID 1208) AS DETLIST
FROM MSDPF MSGDET
WHERE COALESCE(MSGDET.MSGOCCID,' ') != ' ' and
MSG.MSGOCCID = MSGDET.MSGOCCID AND
MSGDET.FIELDVAL != ''
GROUP BY MSGDET.MSGOCCID)MSGDLIST ON 1=1);
Character concatenation should not be a problem (except if it gets confused somewhere with the CCSIDs). I replace the <>, form the detail for in case that may interfere with xml functions... I have catered for Null and '' values ....
I have search lots of threads on lots of different sites and have run out of ideas.
Any suggestions will be appreciated.

I'm just guessing a quick comment answer here. Those left outer joins can introduce nulls into the mix you can simply coalesce them in the where clause ...
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP where
coalesce(DETLIST,' ') like '%4179792C%'
IRL I would use locate because it just feels faster when it runs.
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP where
locate('4179792C' ,coalesce(DETLIST,' ')) <> 0

HY000 is a CLI (ODBC) General error, the explaination in the IBM documentation is:
An error occurred for which there is no specific SQLSTATE and for which no implementation defined SQLSTATE is defined. The error message returned by SQLError in the argument szErrorMsg describes the error and its cause.
Since this is probably not something you are coding by hand, you probably do not have the opportunity to issue SQLError, but I suspect that the message you quoted in your question is the result of some internal process retrieving that error. so the problem should be:
Data conversion or data mapping error.
Select or omit error on field MapXmlData(Cast(Concat(',
',Strip(Replace(Cast(P6MSDPF_4.FIELDVAL AS VarChar(128) CCSID 1208),'-'), Both,' ')) AS VarChar(260) CCSID 1208)) member COMMSWKBL1. Select/omit error on member COMMSWKBL1.
I don't know your files, but I am guessing that there is bad data in the file and that COMMSWKBL1 is a select logical that is accessed to process the selection when you add the DETLIST like '%4179792C%' to your query. Your task is to find out which file is associated with member COMMSWKBL1, and see if you can determine why a Select or omit error would be occurring on that file.

It is fine. I have modified the view to select both the header and detail, and search on both entity entries (linked on the key MSGOCCID) and do a DISTINCT on the key MSGOCCID. It seems a little bit slower than the view the question is about but is working fine. Thank you for you time.

Related

Fetching attribute from JSON string with JSON_VAL cause "<attribute> is invalid in the used context" error

A proprietary third-party application stores JSON strings in it's database like this one:
{"state":"complete","timestamp":1614776473000}
I need the timestamp and found out that
DB2 offers JSON functions. Since it's stored as string in the PROF_VALUE column, I guess that converting with SYSTOOLS.JSON2BSON is required, before I can use JSON_VAL to fetch the timestamp:
SELECT SYSTOOLS.JSON_VAL(SYSTOOLS.JSON2BSON(PROF_VALUE), "timestamp", "f")
FROM EMPINST.PROFILE_EXTENSIONS ext
WHERE PROF_PROPERTY_ID = 'touchpointState'
This causes an error that timestamp is invalid in the used context ( SQLCODE=-206, SQLSTATE=42703, DRIVER=4.26.14). The same error is thown when I remove the JSON2BSON call like this
SELECT SYSTOOLS.JSON_VAL(PROF_VALUE, "timestamp", "f")
Also not working with the same error (different data-types):
SELECT SYSTOOLS.JSON_VAL(SYSTOOLS.JSON2BSON(PROF_VALUE), "state", "s:1000")
SELECT SYSTOOLS.JSON_VAL(PROF_VALUE) "state", "s:1000")
I don't understand this error. My syntax is like the documented JSON_VAL ( json-value , search-string , result-type) and it is the same like in the examples, where they show how to fetch the name field of an object.
I also played around a bit with JSON_TABLE to use raw input data for testing (instead of the database data), but it seems not suiteable for that.
SELECT *
FROM TABLE(SYSTOOLS.JSON_TABLE( SYSTOOLS.JSON2BSON('{"state":"complete","timestamp":1614776473000}'), 'state','s:32')) DATA
This gave me a table with one row: Type = 2 and Value = complete.
I had two problems in my query: First it seems that double quotes " are for object references. I wasn't aware that there is any difference, because in most databases I used yet, both single ' and double quotes " are equal.
The second problem is, that JSON_VAL needs to be called without SYSTOOLS, but the reference is still needed on SYSTOOLS.JSON2BSON(PROF_VALUE).
With those changes, the following query worked:
SELECT JSON_VAL(SYSTOOLS.JSON2BSON(PROF_VALUE), 'timestamp', 'f')
FROM EMPINST.PROFILE_EXTENSIONS ext
WHERE PROF_PROPERTY_ID = 'touchpointState'

Importing OLAP metadata in SQL Server via linked server results in out-of-range date

Currently, I am trying to extract metadata from an OLAP cube in SQL Server (via a linked server) using this simple query:
select *
into [dbo].[columns_metadata]
from openquery([LINKED_SERVER], '
select *
from $System.TMSCHEMA_COLUMNS
')
But in the result set, there is a column named RefreshedTime with values 31.12.1699 00:00:00.
Because of this value, the query gives this error message:
Msg 8114, Level 16, State 9, Line 1 Error converting data type (null)
to datetime.
The problem is that I need to run the query without specifying the columns in the SELECT statement.
Do you know a trick to avoid this error?
I know you wanted not to have to mention the columns explicitly, but in case nobody can suggest a way to have it handle the 1699-12-31 dates, then you can fallback to this:
select *
into [dbo].[columns_metadata]
from openquery([LINKED_SERVER], '
SELECT [ID]
,[TableID]
,[ExplicitName]
,[InferredName]
,[ExplicitDataType]
,[InferredDataType]
,[DataCategory]
,[Description]
,[IsHidden]
,[State]
,[IsUnique]
,[IsKey]
,[IsNullable]
,[Alignment]
,[TableDetailPosition]
,[IsDefaultLabel]
,[IsDefaultImage]
,[SummarizeBy]
,[ColumnStorageID]
,[Type]
,[SourceColumn]
,[ColumnOriginID]
,[Expression]
,[FormatString]
,[IsAvailableInMDX]
,[SortByColumnID]
,[AttributeHierarchyID]
,[ModifiedTime]
,[StructureModifiedTime]
,CStr([RefreshedTime]) as [RefreshedTime]
,[SystemFlags]
,[KeepUniqueRows]
,[DisplayOrdinal]
,[ErrorMessage]
,[SourceProviderType]
,[DisplayFolder]
from $System.TMSCHEMA_COLUMNS
')

MS Access query produces invalid procedure call

My query in access works fine if i only use the below query with a select statement. As soon as it becomes an append query it produces an "invalid procedure call" error.
I have narrowed down the offending columns as being "Publ" and "PublLong". Both are long text strings. If I remove these two columns the query updates without an error.
Here is a sample data point found in the [Bezeichung] Field:
publications.bank.com/publ-dl-ch/pdf/WhatsUp_20181113_en.pdf
I checked the table that it is being inserted to and the data types are the same nor did i see any other setting that would block the insertion.
How can i get it to work?
INSERT INTO tbl_MatomoRaw ( DownloadDate, IntExt, Publ, PublLong,
PublDate, [Language], Download_Visits, PublMonth )
SELECT
Date() AS DownloadDate,
Left([Bezeichnung],InStr([Bezeichnung],".")-1) AS IntExt,
Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"") AS Publ,
Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStrRev([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1) AS PublLong,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,8) AS PublDate,
Mid([Bezeichnung],Len([Bezeichnung])-5,2) AS [Language],
xlsx_Output.[Eindeutige Downloads] AS Download_Visits,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,6) AS PublMonth
FROM xlsx_Output
WHERE
(((Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"")) Not Like "#Func!"));
#Func! indicates one of your functions is causing an error.
Your query uses multiple functions that run into trouble when your input doesn't meet that format, pre-filter instead of filtering on an error, since you can't filter on an error when appending:
INSERT INTO tbl_MatomoRaw ( DownloadDate, IntExt, Publ, PublLong,
PublDate, [Language], Download_Visits, PublMonth )
SELECT
Date() AS DownloadDate,
Left([Bezeichnung],InStr([Bezeichnung],".")-1) AS IntExt,
Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"") AS Publ,
Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStrRev([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1) AS PublLong,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,8) AS PublDate,
Mid([Bezeichnung],Len([Bezeichnung])-5,2) AS [Language],
[Eindeutige Downloads] AS Download_Visits,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,6) AS PublMonth
FROM (SELECT * FROM xlsx_Output WHERE Len(Bezeichnung) > 5 AND Bezeichnung LIKE "*?.?*" AND Bezeichnung LIKE "*_????????*" AND Bezeichnung LIKE "*?\?*")
WHERE
(((Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"")) Not Like "#Func!"));
Since I don't know exactly where the errors occur, I can't write up a proper filter to identify them, but judging by your query they should include a slash and a symbol after that slash, an underscore and at least 8 symbols after that underscore, and a dot with at least one symbol before and after the dot.

U-sql error: Expected one of: AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')' ','

I have a following table:
EstimatedCurrentRevenue -- Revenue column value of yesterday
EstimatedPreviousRevenue --- Revenue column value of current day
crmId
OwnerId
PercentageChange.
I am querying two snapshots of the similarly structured data in Azure data lake and trying to query the percentage change in Revenue.
Following is my query i am trying to join on OpportunityId to get the difference between the revenue values:
#opportunityRevenueData = SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue) AS PercentageRevenueChange, optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue, opty.crmId, opty.OwnerId From #opportunityCurrentData AS opty JOIN #opportunityPreviousData AS optyPrevious on opty.OpportunityId == optyPrevious.OpportunityId;
But i get the following error:
E_CSC_USER_SYNTAXERROR: syntax error. Expected one of: AS EXCEPT FROM
GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')'
','
at token 'From', line 40
near the ###:
This expression is having the problem i know but not sure how to fix it.
(((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue)
Please help, i am completely new to U-sql
U-SQL is case-sensitive (as per here) with all SQL reserved words in UPPER CASE. So you should capitalise the FROM and ON keywords in your statement, like this:
#opportunityRevenueData =
SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue) * 100) / opty.EstimatedCurrentRevenue) AS PercentageRevenueChange,
optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue,
opty.crmId,
opty.OwnerId
FROM #opportunityCurrentData AS opty
JOIN
#opportunityPreviousData AS optyPrevious
ON opty.OpportunityId == optyPrevious.OpportunityId;
Also, if you are completely new to U-SQL, you should consider working through some tutorials to establish the basics of the language, including case-sensitivity. Start at http://usql.io/.
This same crazy sounding error message can occur for (almost?) any USQL syntax error. The answer above was clearly correct for the provided code.
However since many folks will probably get to this page from a search for 'AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE', I'd say the best advice to handle these is look closely at the snippet of your code that the error message has marked with '###'.
For example I got to this page upon getting a syntax error for a long query and it turned out I didn't have a casing issue, but just a malformed query with parens around the wrong thing. Once I looked more closely at where in the snippet the ### symbol was, the error became clear.

SQL Hive subquery error

I have the query below
set hive.cli.print.header=true;
set hive.query.max.partition=1000;
set hive.mapred.mode=unstrict;
SELECT
dim_lookup("accounts",name,"account_id") = '28016' as company,
dim_lookup("campaigns",name,"campaign_id") in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215) as campaign_name,
case when is_click_through=0 then "PV" else "PC" end as conv_type,
(SELECT COUNT(1) FROM impressions WHERE ad_info[2] in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215)) AS impressions
FROM actions
WHERE
data_date>='20170101'
AND data_date<='20171231'
AND conversion_action_id in (20769223,20769214,20769219,20764929,20764932,20764935,20769215,20769216,20764919,20769218,20769217,20769220,20769222)
GROUP BY conv_type
When I execute it I get an error
ERROR ql.Driver: FAILED: ParseException line 8:1 cannot recognize input near 'SELECT' 'COUNT' '(' in expression specification
I am trying to fetch each count of impression for a specified conversion_action_id. What could be the error in my query? Thanks for the help.
FYI: ad_info[2] and campaign_id are the same.
The problem is quite clear, you have a subquery inside your SELECT.
That is not how this works.
Unfortunately the exact solution is not that clear, as it I am not completely sure what you want, but here is some general advice:
Write your subquery, test it and make sure it is ok
Rather than putting it in your SELECT part, put it in your FROM part, and (as always) SELECt from the FROM
Just think of your subquery output as an other table that can be used in the from statement, and which needs to be combined (JOIN, UNION?) with other tables in the from statement.