I have a code that was written for DB2 and now need to rewrite it for hive and I am unable to find equivalent code for MAX(DECODE in HIVE
My Current CODE
SELECT
E.CHARGE_ARRANGEMENT_NUMBER ,
E.MIG_MAIN_PROD_CODE ,
MAX(DECODE(UPPER(E.MIG_EXTRA_ACTION), 'KEEP', E.MIG_EXTRA_ACTION)) AS EXTRAS_KEEP ,
MAX(DECODE(UPPER(E.MIG_EXTRA_ACTION), 'DROP', E.EXTRAS_LIST)) AS EXTRAS_DROP
FROM
EXTRA_MAPPINGS_PRE E
GROUP BY
E.CHARGE_ARRANGEMENT_NUMBER ,
E.MIG_MAIN_PROD_CODE
so you need to use if or case when then end.
SELECT
E.CHARGE_ARRANGEMENT_NUMBER ,
E.MIG_MAIN_PROD_CODE ,
MAX(IF(UPPER(E.MIG_EXTRA_ACTION) ='KEEP', E.MIG_EXTRA_ACTION,null) ) AS EXTRAS_KEEP ,
MAX(IF(UPPER(E.MIG_EXTRA_ACTION) ='DROP', E.EXTRAS_LIST,null)) AS EXTRAS_DROP
FROM
EXTRA_MAPPINGS_PRE E
GROUP BY
E.CHARGE_ARRANGEMENT_NUMBER ,
E.MIG_MAIN_PROD_CODE
You can use case-when if you are not comfortable with if.
Related
I'm trying to execute the next query in MonetDB using "WITH":
with a as (select data_string from colombia.dim_tempo)
select
t.ano_mes
,f.sg_estado
,f.cod_produto
, sum(f.qtd_vendidas) as qtd_vendidas
, count(*) as fact_count
from colombia.fact_retail_market f, colombia.dim_tempo t
where f.cod_anomes = t.data_string
and t.data_string in (a.data_string)
group by
t.ano_mes
,f.sg_estado
,f.cod_produto ;
But always get this message:
What's wrong with the sentence?
The WHERE clause needs to be:
WHERE f.cod_anomes = t.data_string AND
t.data_string IN (SELECT data_string FROM a)
That is, IN needs to be followed by a subquery against the CTE.
I'm trying to simplify a column in BigQuery by using BigQuery extract on it but I am having a bit of an issue.
Here are two examples of the data I'm extracting from:
dc_pre=CLXk_aigyOMCFQb2dwod4dYCZw;gtm=2wg7f1;gcldc=;gclaw=;gac=UA-5815571-8:;auiddc=;u1=OVERDRFT;u2=undefined;u3=undefined;u4=undefined;u5=SSA;u6=undefined;u7=na;u8=undefined;u9=undefined;u10=undefined;u11=undefined;~oref=https://www.online.bank.co.za/onlineContent/ga_bridge.html
dc_pre=COztt4-tyOMCFcji7Qod440PCw;gtm=2wg7f1;gcldc=;gclaw=;gac=UA-5815571-8:;auiddc=;u1=DDA13;u2=undefined;u3=undefined;u4=undefined;u5=SSA;u6=undefined;u7=na;u8=undefined;u9=undefined;u10=undefined;u11=undefined;~oref=https://www.online.support.co.za/onlineContent/ga_bridge.html
I want to extract the portion between ;u1= and ;u2
Running the following legacy SQL Query
SELECT
Date(Event_Time),
Activity_ID,
REGEXP_EXTRACT(Other_Data, r'(?<=u1=)(.*\n?)(?=;u2)')
FROM
[sprt-data-transfer:dtftv2_sprt.p_activity_166401]
WHERE
Activity_ID in ('8179851')
AND Site_ID_DCM NOT IN ('2134603','2136502','2539719','2136304','2134604','2134602','2136701','2378406')
AND Event_Time BETWEEN 1563746400000000 AND 1563832799000000
I get the error...
Failed to parse regular expression "(?<=u1=)(.*\n?)(?=;u2)": invalid
perl operator: (?<
And this is where my talent runs out, is the error being caused because I'm using legacy SQL? Or is an unsupported format for REGEX?
Just tried this, and it worked, but with "Standart SQL" enabled.
select
other_data,
regexp_extract(other_data, ';u1=(.+?);u2') as some_part
from
unnest([
'dc_pre=CLXk_aigyOMCFQb2dwod4dYCZw;gtm=2wg7f1;gcldc=;gclaw=;gac=UA-5815571-8:;auiddc=;u1=OVERDRFT;u2=undefined;u3=undefined;u4=undefined;u5=SSA;u6=undefined;u7=na;u8=undefined;u9=undefined;u10=undefined;u11=undefined;~oref=https://www.online.bank.co.za/onlineContent/ga_bridge.html',
'dc_pre=COztt4-tyOMCFcji7Qod440PCw;gtm=2wg7f1;gcldc=;gclaw=;gac=UA-5815571-8:;auiddc=;u1=DDA13;u2=undefined;u3=undefined;u4=undefined;u5=SSA;u6=undefined;u7=na;u8=undefined;u9=undefined;u10=undefined;u11=undefined;~oref=https://www.online.support.co.za/onlineContent/ga_bridge.html'
]) as other_data
Not using regex but it still works...
with test as (
select 1 as id, 'dc_pre=CLXk_aigyOMCFQb2dwod4dYCZw;gtm=2wg7f1;gcldc=;gclaw=;gac=UA-5815571-8:;auiddc=;u1=OVERDRFT;u2=undefined;u3=undefined;u4=undefined;u5=SSA;u6=undefined;u7=na;u8=undefined;u9=undefined;u10=undefined;u11=undefined;~oref=https://www.online.bank.co.za/onlineContent/ga_bridge.html' as my_str UNION ALL
select 2 as id, 'dc_pre=COztt4-tyOMCFcji7Qod440PCw;gtm=2wg7f1;gcldc=;gclaw=;gac=UA-5815571-8:;auiddc=;u1=DDA13;u2=undefined;u3=undefined;u4=undefined;u5=SSA;u6=undefined;u7=na;u8=undefined;u9=undefined;u10=undefined;u11=undefined;~oref=https://www.online.support.co.za/onlineContent/ga_bridge.html'
),
temp as (
select
id,
split(my_str,';') as items
from test
),
flattened as (
select
id,
split(i,'=')[SAFE_OFFSET(0)] as left_side,
split(i,'=')[SAFE_OFFSET(1)] as right_side
from temp
left join unnest(items) i
)
select * from flattened
where left_side = 'u1'
I've written a query in SQL server that runs okay.
I need to run this query in Access as there is more data I need to pull into the query.
However when I copy the SQL into Access it doesn't work, I get a syntax error, Missing operator in query expression.
SELECT main.Tbl_ServiceOrder.ServiceOrder
, main.Tbl_Serviceorder.BusinessPartnerNumber
, main.Tbl_ServiceOrder.ExternalPointofDeliverynumber
, main.Tbl_ServiceOrder.ServiceProduct
, main.Tbl_ServiceOrder.SOCreatedOn
, main.Tbl_ServiceOrder.MainUserStatus
, main.Tbl_ServiceOrder.MainUserStatusDesc
, main.Tbl_ServiceOrder.ReasonCode
, main.Tbl_ServiceOrder.ReasonCodeDesc
, main.Tbl_ServiceOrder.SOActualFinishDate
, main.Tbl_ServiceOrder.BasicStartDate
,CAST(CASE WHEN reasoncode <> 'CMPL' THEN 'Pre Install'
WHEN reasoncode = 'CMPL' and SOActualFinishDate < (getdate()-182) THEN 'SMART BAU'
ELSE 'Post Install' END as Varchar) as SMRTPot
FROM main.Tbl_ServiceOrder
WHERE main.Tbl_ServiceOrder.ServiceProduct ='SMINSTALL'
OR main.Tbl_ServiceOrder.ServiceProduct ='SMEXCHANGE'
The WHEN on the first line of the CAST is highlighted by access as the error point.
What am i doing wrong?
I've tried changing the cast to the IIF below
, IIF (reasoncode <> 'CMPL','Pre Install',(IIF SOActualFinishDate < (getdate()-182),'SMART BAU','Post Install')) as SMRTPot
Above gives syntax error (comma) in query expression
IIF (reasoncode <> 'CMPL','Pre Install',(IIF SOActualFinishDate < (getdate()-182),'SMART BAU','Post Install')) as SMRTPot
above gives syntax error in query expression.
Okay so this is what I'm using now:
SELECT main.Tbl_ServiceOrder.ServiceOrder
, main.Tbl_Serviceorder.BusinessPartnerNumber
, main.Tbl_ServiceOrder.ExternalPointofDeliverynumber
, main.Tbl_ServiceOrder.ServiceProduct
, main.Tbl_ServiceOrder.SOCreatedOn
, main.Tbl_ServiceOrder.MainUserStatus
, main.Tbl_ServiceOrder.MainUserStatusDesc
, main.Tbl_ServiceOrder.ReasonCode
, main.Tbl_ServiceOrder.ReasonCodeDesc
, main.Tbl_ServiceOrder.SOActualFinishDate
, main.Tbl_ServiceOrder.BasicStartDate
,Switch(reasoncode <> 'CMPL', 'Pre Install',
reasoncode = 'CMPL' AND SOActualFinishDate < ( Getdate() - 182 ), 'SMART BAU',
True, 'Post Install') as SMRTPot
FROM main.Tbl_ServiceOrder
WHERE main.Tbl_ServiceOrder.ServiceProduct ='SMINSTALL' OR main.Tbl_ServiceOrder.ServiceProduct ='SMEXCHANGE'
Error message is now could not find file 'file path\main.mdb
Too many issues and too long for a comment.
You are using SQL Server syntax and not MS Access SQL syntax.
WHEN...CASE is not valid in MS ACcess SQL, you should use IIF or SWITCH instead
CAST is also not valid. You can convert to string using CStr
GetDate is also not valid, you should use DATE().
If you want to substract 182 days then you should do DATEADD('d', DATE() , -182)
But the most handy way to handle this is probably to make passthrough query that will be executed on SQL Server and not MS Access (thus not on linked tables), so you can keep your SQL Server syntax. Search that path.
In u-sql query dumping data from one csv file to another through Outputterss.Csv() function but header row with column names is appearing at the end of file instead of top.Please find my code below.Thanks for the help.
#telDataResult=
SELECT
"vin" AS vin,
"outsideTemperature" AS outsideTemperature,
"EventProcessedUtcTime" AS EventProcessedUtcTime,
"PartitionId" AS PartitionId,
"EventEnqueuedUtcTime" AS EventEnqueuedUtcTime,
"IoTHub" AS IoTHub
FROM #telData
UNION
SELECT
t.vin ,
Convert.ToString(outsideTemperature) AS outsideTemperature
EventProcessedUtcTime ,
PartitionId ,
EventEnqueuedUtcTime ,
IoTHub
FROM
#telData AS t
UNION
SELECT
t.vin ,
Convert.ToString(outsideTemperature) AS outsideTemperature
EventProcessedUtcTime ,
PartitionId ,
EventEnqueuedUtcTime ,
IoTHub
FROM
#telData1 AS t;
OUTPUT #telDataResult
TO
#"wasb://blobcontainer#blobstorage.blob.core.windows.net/cluster/logs/2016/outputofADLA.csv"
USING Outputters.Csv();
When you use a native outputter, the individual rows are written in a parallel fashion by multiple vertices, so there is no guarantee of order. We are currently working on supporting the output of header rows natively. In the meantime, you can use our custom outputter that writes header rows to output files. The custom outputter can be found in https://github.com/Azure/usql/tree/master/Examples/HeaderOutputter. Using the HeaderOutputter, your code will look like the following.
#telDataResult= SELECT
t.vin ,
Convert.ToString(outsideTemperature) AS outsideTemperature
EventProcessedUtcTime ,
PartitionId ,
EventEnqueuedUtcTime ,
IoTHub
FROM
#telData;
OUTPUT #telDataResult TO <OutputFile>
USING new new HeaderOutputter.HeaderOutputter(quoting:false);
I am having trouble with the final piece of my append query. I have the records generating just like I want with the exception of not triggering until the Expression Event Date is <=Date(). It is giving me a unmatched error when I place the <=Date() in the criteria field of the query builder. I tried it with DateSerial and a few other variations. I'm sure it has to do with the expression being that and not a hard date. Any assistance would be appreciated.
INSERT INTO SchedulingLog (
UserID
, LogDate
, EventDate
, Category
, CatDetail
, [Value]
)
SELECT Roster.UserID
, Date() AS LogDate
, DateSerial(Year(Date()),Month([WM DOH]),Day([WM DOH])) AS EventDate
, SchedulingLog.Category
, SchedulingLog.CatDetail
, Max(tblAccrual!WeeksAccrual*Roster!Schedule) AS [Value]
FROM tblAccrual
, [Schedule Type]
, Category
INNER JOIN CatDetail
ON Category.CategoryID = CatDetail.CategoryID
, SchedulingLog
INNER JOIN Roster
ON SchedulingLog.UserID = Roster.UserID
WHERE (((tblAccrual.Years)<=Round((Date()-[wm doh])/365,2)))
GROUP BY Roster.UserID
, Date()
, DateSerial(Year(Date()),Month([WM DOH]),Day([WM DOH]))
, SchedulingLog.Category
, SchedulingLog.CatDetail
HAVING (((SchedulingLog.Category) Like "Vac*")
AND ((SchedulingLog.CatDetail) Like "Ann*"));
I believe the issue is not explicitly converting the user input date with CDate. I suspect it's fine in most of the query because the [MW DOH] parameter is provided directly to functions which will convert it to date. However the WHERE clause would need an explicit conversion.
The following generates the error "This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to the variables."
SELECT Date()-[userinput] AS something;
Whereas the following code does not
SELECT Date()-CDate([userinput]) AS something;