Why Pig can't accept casting integer to chararray - apache-pig

I have this pig script :
CRE_22002 = LOAD '$input' USING PigStorage(';') AS (GM_COMPTEUR:chararray,CIA_CD_CRV_CIA:chararray,CIA_DA_EM_CRV:chararray,CIA_CD_CTRL_BLCE:chararray,CIA_IDC_EXTR_RDJ:chararray,CIA_VLR_IDT_CRV_LOQ:chararray,CIA_VLR_REF_CRV:chararray,CIA_NO_SEQ_CRV:chararray,CIA_VLR_LG_ZON_RTG:chararray,CIA_HEU_CIA:chararray,CIA_TM_STP_CRE:chararray,CIA_CD_SI:chararray,CIA_VLR_1:chararray,CIA_DA_ARR_FIC:chararray,CIA_TY_ENR:chararray,CIA_CD_BTE:chararray,CIA_CD_PER:chararray,CIA_CD_EFS:chararray,CIA_CD_ETA_VAL_CRV:chararray,CIA_CD_EVE_CPR:chararray,CIA_CD_APLI_TDU:chararray,CIA_CD_STE_RTG:chararray,CIA_DA_TT_RTG:chararray,CIA_NO_ENR_RTG:chararray,CIA_DA_VAL_EVE:chararray,PSE_001:chararray,STR_002:chararray,STR_003:chararray,CPR_006_VLR:chararray,CPR_006_DCM:chararray,CPR_006_CD_DVS:chararray,CPR_008_VLR:chararray,CPR_008_DCM:chararray,CPR_008_CD_DVS:chararray,CPR_009_VLR:chararray, CPR_009_DCM:chararray,CPR_009_CD_DVS:chararray,CPR_059_VLR:chararray,CPR_059_DCM:chararray,CPR_059_CD_DVS:chararray,CPR_060_VLR:chararray,CPR_060_DCM:chararray,CPR_060_CD_DVS:chararray,RUB_205:chararray,RUB_216:chararray,DAT_015_X:chararray,NB_005_VLR:chararray,NB_005_DCM:chararray,NB_007_VLR:chararray,NB_007_DCM:chararray,NB_012_VLR:chararray,NB_012_DCM:chararray,EUR_061_VLR:chararray,EUR_061_DCM:chararray,EUR_061_CD_DVS:chararray,EUR_062_VLR:chararray,EUR_062_DCM:chararray,EUR_062_CD_DVS:chararray);
-- Generate columns
CRE_22002_DATA = FOREACH CRE_22002 GENERATE
(chararray) ( $0 ) AS MGM_COMPTEUR,
(chararray) ( $1 ) AS CIA_CD_CRV_CIA,
(chararray) ( $2 ) AS CIA_DA_EM_CRV,
(chararray) ( $3 ) AS CIA_CD_CTRL_BLCE,
(chararray) ( $4 ) AS CIA_IDC_EXTR_RDJ,
(chararray) ( $5 ) AS CIA_VLR_IDT_CRV_LOQ,
(chararray) ( $6 ) AS CIA_VLR_REF_CRV,
(chararray) ( $7 ) AS CIA_NO_SEQ_CRV,
(chararray) ( $8 ) AS CIA_VLR_LG_ZON_RTG,
(chararray) ( $9 ) AS CIA_HEU_CIA,
(chararray) ( $10 ) AS CIA_TM_STP_CRE,
(chararray) ( $11 ) AS CIA_CD_SI,
(chararray) ( $12 ) AS CIA_VLR_1,
(chararray) ( $13 ) AS CIA_DA_ARR_FIC,
(chararray) ( $14 ) AS CIA_TY_ENR,
(chararray) ( $15 ) AS CIA_CD_PER,
(chararray) ( $16 ) AS CIA_CD_EFS,
(chararray) ( $17 ) AS CIA_CD_ETA_VAL_CRV,
(chararray) ( $18 ) AS CIA_CD_EVE_CPR,
(chararray) ( $19 ) AS CIA_CD_APLI_TDU,
(chararray) ( $20 ) AS CIA_CD_STE_RTG,
(chararray) ( $21 ) AS CIA_DA_TT_RTG,
(chararray) ( $22 ) AS CIA_NO_ENR_RTG,
(chararray) ( $23 ) AS CIA_DA_VAL_EVE,
(chararray) ( $24 ) AS PSE_001,
(chararray) ( $25 ) AS STR_002,
(chararray) ( $26 ) AS STR_003,
(chararray) ( $27 ) AS CPR_006_VLR,
(chararray) ( $28 ) AS CPR_006_DCM,
(chararray) ( $29 ) AS CPR_006_CD_DVS,
(chararray) ( $30 ) AS CPR_008_VLR,
(chararray) ( $31 ) AS CPR_008_DCM,
(chararray) ( $32 ) AS CPR_008_CD_DVS,
(chararray) ( $33 ) AS CPR_009_VLR,
(chararray) ( $34 ) AS CPR_009_DCM,
(chararray) ( $35 ) AS CPR_009_CD_DVS,
(chararray) ( $36 ) AS CPR_059_VLR,
(chararray) ( $37 ) AS CPR_059_DCM,
(chararray) ( $38 ) AS CPR_059_CD_DVS,
(chararray) ( $39 ) AS CPR_060_VLR,
(chararray) ( $40 ) AS CPR_060_DCM,
(chararray) ( $41 ) AS CPR_060_CD_DVS,
(chararray) ( $42 ) AS RUB_205,
(chararray) ( $43 ) AS RUB_216,
(chararray) ($44) AS DAT_015_X,
(chararray) ( $45 ) AS NB_005_VLR,
(chararray) ( $46 ) AS NB_005_DCM,
(chararray) ( $47 ) AS NB_007_VLR,
(chararray) ( $48 ) AS NB_007_DCM,
(chararray) ( $49 ) AS NB_012_VLR,
(chararray) ( $50 ) AS NB_012_DCM,
(chararray) ( $51 ) AS EUR_061_VLR,
(chararray) ( $52 ) AS EUR_061_DCM,
(chararray) ( $53 ) AS EUR_061_CD_DVS,
(chararray) ( $54 ) AS EUR_062_VLR,
(chararray) ( $55 ) AS EUR_062_DCM,
(chararray) ( $56 ) AS EUR_062_CD_DVS;
-- Sauvegarder le resultat
CRE_22002_DATA_FILTER = FILTER CRE_22002_DATA BY (ToDate(DAT_015_X,'yyyyMMdd')> ToDate('20190101','yyyyMMdd'));
-- Store
STORE CRE_22002_DATA_FILTER INTO '/hdfs/data/adhoc/PR/02/RDO0/BB0/MGM22002-OUTPUT.csv' USING org.apache.pig.piggybank.storage.CSVExcelStorage(';','NO_MULTILINE', 'UNIX','WRITE_OUTPUT_HEADER');
I know that RUB_216 column contains integer but it's given by espace :
like :
;16 ;
;17 ;
..
So I decide to cast it as chararray.
When run the pig script :
ERROR 0: Exception while executing (Name: CRE_22002_DATA: New For
Each(false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false)[bag]
- scope-353 Operator Key: scope-353): org.apache.pig.backend.executionengine.ExecException: ERROR 0:
Exception while executing [POUserFunc (Name:
POUserFunc(org.apache.pig.builtin.ToDate2ARGS)[datetime] - scope-179
Operator Key: scope-179) children: null at []]:
java.lang.IllegalArgumentException: Invalid format: "RUB_216"
Any help please ?

Related

Return random value for each row from different table

I'm trying to get random name for each row, but it just shows same random value for every row. What am I missing?
SELECT TOP (1000) v.id,v.[Name], RandomName
FROM [V3_Priva].[dbo].[Vehicle] v
cross join
(Select top 1 ISNULL([Description_cs-CZ], [Description]) RandomName
from crm.Enumeration e
join crm.EnumerationType et on e.EnumerationType_FK = et.Id
where EnumerationType_FK = 12
order by NEWID()) RandomName
Result table
Try using something like the following to drive your lookup.
DECLARE #Rows AS TABLE ( I INT NOT NULL )
DECLARE #Vals AS TABLE ( ID INT IDENTITY, Val NVARCHAR(255) NOT NULL )
INSERT INTO #Rows
VALUES ( 0 )
, ( 1 )
, ( 2 )
, ( 3 )
, ( 4 )
, ( 5 )
, ( 6 )
, ( 7 )
, ( 8 )
, ( 9 )
INSERT INTO #Vals
VALUES ( 'Apple' )
, ( 'Banana' )
, ( 'Peach' )
, ( 'Plum' )
, ( 'Pear' )
WITH cte AS ( SELECT *, ABS(CHECKSUM(NEWID())) % 5 ID FROM #Rows )
SELECT cte.I
, cte.ID
, V.ID
, V.Val
FROM cte
JOIN #Vals V ON V.ID = cte.ID + 1
ORDER BY I
This way new ID is generated for each row, rather than once for the lookup.

SQL - Prepend a value if missing

Code/data:
DECLARE #T TABLE
(
[Col1] VARCHAR(20)
, [RowNum] INT
) ;
INSERT INTO #T
VALUES
( N'second', 1 )
, ( N'fifth', 4 )
, ( N'fourth', 3 )
--, ( N'zzz', 1 )
, ( N'third', 2 )
---- OR when "zzz" is part of this list
--VALUES
-- ( N'second', 2 )
-- , ( N'fifth', 5 )
-- , ( N'fourth', 4 )
-- , ( N'zzz', 1 )
-- , ( N'third', 3 )
SELECT STUFF ((
SELECT ',' + [SQ].[Col1]
FROM
(
SELECT N'zzz' AS [Col1]
, 1 AS [RowNum]
UNION
SELECT [Col1]
, [RowNum]
FROM #T
) AS [SQ]
FOR XML PATH ( '' ), TYPE
).[value] ( '.', 'varchar(MAX)' ), 1, 1, ''
) ;
Current output:
fifth,fourth,second,third,zzz
Goal:
Prepend "zzz," in the output string if missing in the 2nd part of the union AND the values should be in ASC ordered based on the values specified in [rownum] field defined in the 2nd part of the union. If "zzz" exists in the 2nd part of the input already (it will always be RowNum 1 in that case), it should return it only once as the first value.
Expected output:
zzz,second,third,fourth,fifth
UPDATED the requirement due to an error on my part when creating this post. Updated code/data represents more accurate scenario. Please note the RowNum seq in the 2nd part of the UNION, it also starts with 1, but this time, it might or might not be associated to "zzz" Basically, I want to prepend "zzz" in the comma-delimited & ordered output if it doesn't exist.
Hope the below one will help you.
SELECT ',' + [SQ].[Col1]
FROM
(
SELECT N'first' AS [Col1],1 AS [RowNum]
UNION
SELECT [ABC].[Col1],[ABC].[RowNum]
FROM
(
VALUES
( N'second', 2 )
, ( N'fifth', 5 )
, ( N'fourth', 4 )
--, ( N'first', 1 )
, ( N'third', 3 )
) AS [ABC] ( [Col1], [RowNum] )
) AS [SQ]
ORDER BY [RowNum]
FOR XML PATH ( '' ), TYPE
).[value] ( '.', 'varchar(MAX)' ), 1, 1, ''
) ;
Returns an output
first,second,third,fourth,fifth
Attached the Answer for the updated Scenario-
DECLARE #T TABLE
(
[Col1] VARCHAR(20)
, [RowNum] INT
) ;
INSERT INTO #T
VALUES
( N'second', 1 )
, ( N'fifth', 4 )
, ( N'fourth', 3 )
--, ( N'zzz', 1 )
, ( N'third', 2 )
---- OR when "zzz" is part of this list
--VALUES
-- ( N'second', 2 )
-- , ( N'fifth', 5 )
-- , ( N'fourth', 4 )
-- , ( N'zzz', 1 )
-- , ( N'third', 3 )
SELECT STUFF ((
SELECT ',' + [SQ].[Col1]
FROM
(
SELECT N'zzz' AS [Col1]
, 0 AS [RowNum]
UNION
SELECT [Col1]
, [RowNum]
FROM #T
) AS [SQ]
ORDER BY [RowNum]
FOR XML PATH ( '' ), TYPE
).[value] ( '.', 'varchar(MAX)' ), 1, 1, ''
) ;
Returns
zzz,second,third,fourth,fifth
Common Table Expressions (CTEs) provide a handy way of breaking queries down into simpler steps. Note that you can view the results of each step by switching out the last select statement.
with
Assortment as (
-- Start with the "input" rows.
select Col1, RowNum
from ( values ( N'second', 2 ), ( N'fifth', 5 ), ( N'fourth', 4 ),
-- ( N'first', 1 ),
( N'third', 3 ) ) ABC ( Col1, RowNum ) ),
ExtendedAssortment as (
-- Conditionally add "first".
select Col1, RowNum
from Assortment
union all -- Do not remove duplicate rows.
select N'first', 1
where not exists ( select 42 from Assortment where Col1 = N'first' ) )
-- Output the result.
-- Intermediate results may be seen by uncommenting one of the alternate select statements.
-- select * from Assortment;
-- select * from ExtendedAssortment;
select Stuff(
( select N',' + Col1 from ExtendedAssortment order by RowNum for XML path(N''), type).value( N'.[1]', 'NVarChar(max)' ),
1, 1, N'' ) as List;
The same logic can be performed using tables for input:
-- Rows to be included in the comma delimited string.
declare #Input as Table ( Col1 NVarChar(20), RowNum Int );
insert into #Input ( Col1, RowNum ) values
( N'second', 2 ), ( N'fifth', 5 ),
--( N'ZZZ', 17 ), -- Test row.
( N'fourth', 4 ), ( N'third', 3 );
select * from #Input;
-- Mandatory value that must appear in the result. One row only.
declare #Mandatory as Table ( Col1 NVarChar(20), RowNum Int );
-- By using the maximum negative value for an Int this value will be prepended
-- (unless other rows happen to have the same RowNum value).
insert into #Mandatory ( Col1, RowNum ) values ( N'ZZZ', -2147483648 );
select * from #Mandatory;
-- Process the data.
with
AllRows as (
select Col1, RowNum
from #Input
union all
select Col1, RowNum
from #Mandatory
where not exists ( select 42 from #Mandatory as M inner join #Input as I on M.Col1 = I.Col1 ) )
-- Output the result.
-- Intermediate results may be seen by uncommenting the alternate select statement.
--select * from AllRows;
select Stuff(
( select N',' + Col1 from AllRows order by RowNum for XML path(N''), type).value( N'.[1]', 'NVarChar(max)' ),
1, 1, N'' ) as List;

converting rows into columns in sql server

I wonder if anyone can help: I want to convert rows into columns. This is the original table:
I have tried using pivot but this case it is too complex for me.
declare #Table AS TABLE
(
TYPE VARCHAR(100) ,
SERIE VARCHAR(100) ,
CUR1 INT,
CUR2 INT
)
INSERT #Table
( TYPE, SERIE, CUR1, CUR2)
VALUES
( 'CORP', 'S1' ,2122,322 ),
( 'CORP', 'S2' ,321,546 ),
( 'SER', 'S1',543,788 ),
( 'SER', 'S2' ,655, 988 )
I expect the output to be like the attached table:
Please try this, a variant of this will help:-
;with cte as (
select SERIE, [CORP] as [CORP_CUR1], [SER] as [SER_CUR1] from (
select type , serie, cur1 from #Table)
as d
pivot
( max(cur1) for [type] in ( [CORP], [SER]) ) as p
),
ct as (
select SERIE, [CORP] as [CORP_CUR2], [SER] as [SER_CUR2] from (
select type , serie, cur2 from #Table)
as d
pivot
( max(cur2) for [type] in ( [CORP], [SER]) ) as p
)
select cte.SERIE, cte.[CORP_CUR1], cte.[SER_CUR1], ct.[CORP_CUR2], ct.[SER_CUR2] from cte inner join ct on cte.SERIE=ct.SERIE

Rigth way for this CTE in Postgresql

My goal is to pickup the id in the Soggetto entry and fill it into tipo and tipo2.I can't figure out how to write in the right way this CTE in Postgres via pg in nodejs. I'm little bit confused.
var query = client.query('WITH retid AS (INSERT INTO "Soggetto" (nome, cognome, "regSociale", "partIVA") VALUES ($1, $2, $3, $4) RETURNING id ), INSERT INTO "Tipologia" (privato, azienda) VALUES ($5, $6) SELECT id FROM retid, INSERT INTO "Tipologia2" (cliente, fornitore) VALUES ($7, $8) SELECT id FROM retid ',
[sog.nome, sog.cognome, sog.ragioneSociale, sog.partitaIva, sog.cliente, sog.fornitore, sog.privato, sog.azienda], function(err, result){
if(err){
console.log(err);
}else{
res.writeHead(200, {'content-type': 'text/plain'});
res.end('Insert ok');
}
});
The syntax is not correct. It is unclear exactly what you want, but each CTE needs its own name. So, something like this:
WITH retid AS (
INSERT INTO "Soggetto" (nome, cognome, "regSociale", "partIVA")
VALUES ($1, $2, $3, $4)
RETURNING id
),
t as (
INSERT INTO "Tipologia" (privato, azienda)
VALUES ($5, $6)
),
t2 as (
INSERT INTO "Tipologia2" (cliente, fornitore)
VALUES ($7, $8)
)
SELECT id
FROM retid;

How to add functionality for ComboBox <Select All> in Microsoft Access forms

So I posted a question the other day about an access form that needed to have a "Select All" option added to the 2 ComoBoxes. I was able to add in the option to the 2 of them using a union. However, the options do nothing as of yet. I found the Query that takes the form parameters from the ComboBox and this is where I need to add in the option to select all, except after staring at it for hours I've got no clue again.
The Database wasn't written by me, it's approximately 10 years old and was was given to me to add some new features. I have done that, and the owner complained that the "Select All" buttons have never worked. After research, the buttons have VB script that clears the ComboBox input to nullified value. I am planning on scrapping those now since I have added the option to the ComboBox itself.
The SQL query that reads combo input looks like this:
PARAMETERS [Forms]![ReportCentre]![cboTreatmentType] Short, [Forms]![ReportCentre]! [cboTreatmentDate] Short;
SELECT addresses.*,
[firstname] & "" & [lastname]
AS Name,
[street] & "," & [suburb] & "" & [stateorprovince] & "" & [postalcode]
AS
Address
FROM addresses
WHERE ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
[cbotreatmenttype].[Value] )
AND ( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
[cbotreatmentdate].[Value] )
AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
[txtbirthmonth].[Value]
& "*" ) )
OR ( ( ( addresses.treatmentid ) IS NULL )
AND
( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
[cbotreatmentdate].[Value] )
AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
[txtbirthmonth].[Value]
& "*" ) )
OR ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
[cbotreatmenttype].[Value] )
AND ( ( addresses.treatmentdate ) IS NULL )
AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
[txtbirthmonth].[Value]
& "*" ) )
OR ( ( ( addresses.treatmentid ) IS NULL )
AND ( ( addresses.treatmentdate ) IS NULL )
AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
[txtbirthmonth].[Value]
& "*" ) )
OR ( ( ( addresses.treatmentid ) IS NULL )
AND
( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
[cbotreatmentdate].[Value] )
AND ( ( addresses.birthmonth ) IS NULL ) )
OR ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
[cbotreatmenttype].[Value] )
AND ( ( addresses.treatmentdate ) IS NULL )
AND ( ( addresses.birthmonth ) IS NULL ) )
OR ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
[cbotreatmenttype].[Value] )
AND
( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
[cbotreatmentdate].[Value] )
AND ( ( addresses.birthmonth ) IS NULL ) );
I know it's messy and hard to understand, which is why im asking for help. How do I get that to validate a "Select All" option for both ComboBoxes?
One very easy way is to set the bound column of the combo to *:
SELECT "*" As ID, "Select All" As AText
FROM Table1
UNION SELECT Table1.ID, Table1.AText
FROM Table1;
Using your combo:
Select "*" As TreatmentID, "<<All Records>>" As Treatment
FROM Treatment
UNION
Select Treatment.TreatmentID, Treatment.Treatment
From Treatment;
You can then use LIKE:
SELECT Table1.ID
FROM Table1
WHERE Table1.ID Like [forms]![MainForm]![Combo]
Using your SQL:
... WHERE (((Addresses.TreatmentID)
Like [Forms]![ReportCentre]![cboTreatmentType]) AND ...
If you only have a single column, you can use:
SELECT Table1.Atext
FROM Table1
WHERE AText Like
IIf(Forms![MainForm]!Combo="Select All","*",Forms![MainForm]!Combo)