So, I have this query that's just making me go crazy. For some crazy reason, the query runs and brings the desired output on PLSQL Developer, but on SQL Developer it doesn't! As far as I know, the exact same query should work on both, not just one or the other. Not everybody at my team has PLSQL Dev, so it has to work on both. This query also shows different behaviour on my BI Application. Has anyone seen this crazy behaviour before?
Here's the query:
with t1 as (
SELECT
case when CLG_STATUS.NM_STATUS in ('1 - ATIVO','5 - BLOQUEADO','7 - AGUARDANDO AUTENTICAÇÃO') then 'LG Club Member'
else 'Non Member'
end membership,
Round(
Round(SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0)), 1)
/
Count(distinct CLG_PARTICIPANTE.ID)
, 1) as average_sales ,
Round(
SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0))
, 1) as sellout,
Count(distinct CLG_PARTICIPANTE.ID)
as "qty members"
FROM
CLG_VENDA_PONTO,
CLG_PARTICIPANTE,
CLG_STATUS,
CLG_VENDA,
CLG_CARGO CLG_CARGO_VENDA,
CLG_CANAL CLG_CANAL_VENDA,
CLG_REDE CLG_REDE_VENDA,
CLG_PRODUTO,
CLG_TP_MOVIMENTO,
DIM_PERIOD_DAY PERIOD_VENDA_DATA,
CLG_LOJA CLG_LOJA_VENDA
WHERE
( CLG_VENDA_PONTO.ID_VENDA=CLG_VENDA.ID(+) )
AND ( CLG_VENDA_PONTO.ID_PROD=CLG_PRODUTO.ID(+) )
AND ( CLG_VENDA_PONTO.ID_CARGO=CLG_CARGO_VENDA.ID )
AND ( CLG_LOJA_VENDA.ID=CLG_VENDA_PONTO.ID_LOJA )
AND ( CLG_REDE_VENDA.ID=CLG_LOJA_VENDA.ID_REDE )
AND ( CLG_CANAL_VENDA.ID=CLG_VENDA_PONTO.ID_CANAL )
AND ( CLG_PARTICIPANTE.ID_LOJA=CLG_LOJA_VENDA.ID )
AND ( CLG_LOJA_VENDA.ID_REDE=CLG_REDE_VENDA.ID )
AND ( CLG_PARTICIPANTE.ID=CLG_VENDA_PONTO.ID_PARTCPTE )
AND ( CLG_VENDA_PONTO.ID_TP_MOVIMENTO=CLG_TP_MOVIMENTO.ID )
AND ( CLG_STATUS.ID=CLG_PARTICIPANTE.ID_STATUS )
AND ( CLG_VENDA_PONTO.DATA_VENDA=PERIOD_VENDA_DATA.YYYYMMDD )
AND ( CLG_CARGO_VENDA.NM_CARGO = '5 - VENDEDOR' OR CLG_VENDA.ID_CARGO = 8 )
AND
(
CLG_PRODUTO.NM_PRODUTO NOT IN ( 'M4338 - FRETE','R39745 - FRETE SOBRE VENDAS' )
AND
CLG_TP_MOVIMENTO.NM_MOVIMENTO IN ( '1 - VENDA','3 - DEVOLUCAO' )
AND
CLG_REDE_VENDA.NM_REDE = '21540901 - RABELO'
AND
PERIOD_VENDA_DATA.YYYYMMDD_DATE BETWEEN to_date('01-01-2013', 'DD-MM-YYYY') AND to_date('31-12-2013', 'DD-MM-YYYY')
AND
CLG_CANAL_VENDA.NM_CANAL IN ('1 - VAREJO')
AND
CLG_PRODUTO.FG_MAPEADO IN ( 'SIM' )
)
GROUP BY
case when CLG_STATUS.NM_STATUS in ('1 - ATIVO','5 - BLOQUEADO','7 - AGUARDANDO AUTENTICAÇÃO') then 'LG Club Member'
else 'Non Member'
end
), t2 as (
SELECT
case when CLG_STATUS.NM_STATUS in ('1 - ATIVO','5 - BLOQUEADO','7 - AGUARDANDO AUTENTICAÇÃO') then 'LG Club Member'
else 'Non Member'
end membership,
Round(
Round(SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0)), 1)
/
Count(distinct CLG_PARTICIPANTE.ID)
, 1) as average_sales ,
Round(
SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0))
, 1) as sellout,
Count(distinct CLG_PARTICIPANTE.ID)
as "qty members"
FROM
CLG_VENDA_PONTO,
CLG_PARTICIPANTE,
CLG_STATUS,
CLG_VENDA,
CLG_CARGO CLG_CARGO_VENDA,
CLG_CANAL CLG_CANAL_VENDA,
CLG_REDE CLG_REDE_VENDA,
CLG_PRODUTO,
CLG_TP_MOVIMENTO,
DIM_PERIOD_DAY PERIOD_VENDA_DATA,
CLG_LOJA CLG_LOJA_VENDA
WHERE
( CLG_VENDA_PONTO.ID_VENDA=CLG_VENDA.ID(+) )
AND ( CLG_VENDA_PONTO.ID_PROD=CLG_PRODUTO.ID(+) )
AND ( CLG_VENDA_PONTO.ID_CARGO=CLG_CARGO_VENDA.ID )
AND ( CLG_LOJA_VENDA.ID=CLG_VENDA_PONTO.ID_LOJA )
AND ( CLG_REDE_VENDA.ID=CLG_LOJA_VENDA.ID_REDE )
AND ( CLG_CANAL_VENDA.ID=CLG_VENDA_PONTO.ID_CANAL )
AND ( CLG_PARTICIPANTE.ID_LOJA=CLG_LOJA_VENDA.ID )
AND ( CLG_LOJA_VENDA.ID_REDE=CLG_REDE_VENDA.ID )
AND ( CLG_PARTICIPANTE.ID=CLG_VENDA_PONTO.ID_PARTCPTE )
AND ( CLG_VENDA_PONTO.ID_TP_MOVIMENTO=CLG_TP_MOVIMENTO.ID )
AND ( CLG_STATUS.ID=CLG_PARTICIPANTE.ID_STATUS )
AND ( CLG_VENDA_PONTO.DATA_VENDA=PERIOD_VENDA_DATA.YYYYMMDD )
AND ( CLG_CARGO_VENDA.NM_CARGO = '5 - VENDEDOR' OR CLG_VENDA.ID_CARGO = 8 )
AND
(
CLG_PRODUTO.NM_PRODUTO NOT IN ( 'M4338 - FRETE','R39745 - FRETE SOBRE VENDAS' )
AND
CLG_TP_MOVIMENTO.NM_MOVIMENTO IN ( '1 - VENDA','3 - DEVOLUCAO' )
AND
CLG_REDE_VENDA.NM_REDE = '21540901 - RABELO'
AND
PERIOD_VENDA_DATA.YYYYMMDD_DATE BETWEEN add_months(to_date('01-01-2013', 'DD-MM-YYYY'), -12) AND add_months(to_date('31-12-2013', 'DD-MM-YYYY'), -12)
AND
CLG_CANAL_VENDA.NM_CANAL IN ('1 - VAREJO')
AND
CLG_PRODUTO.FG_MAPEADO IN ( 'SIM' )
)
GROUP BY
case when CLG_STATUS.NM_STATUS in ('1 - ATIVO','5 - BLOQUEADO','7 - AGUARDANDO AUTENTICAÇÃO') then 'LG Club Member'
else 'Non Member'
end
)
select t1.*, coalesce((t1.average_sales - t2.average_sales) / t2.average_sales, 0) as variation
from t1 join t2 on t1.membership = t2.membership
On SQL Developer it displays this:
ORA-00928: missing SELECT keyword
00928. 00000 - "missing SELECT keyword"
I'm totally at a loss here. How can a query parse at one tool and not the other ? I tried searching around the net but to no avail.
Thanks in advance!
It might be the line,
/
SQL Plus uses / by itself to mark the end of a sql query, and SQL Developer most likely follows. However, the preceding whitespace may change the meaning, so this may not be the problem.
To test this hypothesis move the / to the preceding line. Change
Round(SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0)), 1)
/
Count(distinct CLG_PARTICIPANTE.ID)
To
Round(SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0)), 1) /
Count(distinct CLG_PARTICIPANTE.ID)
If that is the problem, then it is a matter of reformatting to something that works for you that doesn't leave / on a line by itself.
Related
I'm working on a ranking/scoring system and I'm missing the PERCENTRANK.INC function in powerBI. Instead I have worked out below formula which is the closest I can get.
Score =
DIVIDE (
RANKX (
FILTER ( 'Table', NOT ( ISBLANK ( [Sold amounts] ) ) ),
[Sold amounts],
,
ASC
) - 1,
COUNTROWS ( FILTER ( 'Table', NOT ( ISBLANK ( [Sold amounts] ) ) ) ) - 1
)
I really want to have the formula to take the type of "Fruit" into account in my scoring/ranking.
In short each fruit should be scored separately, with a range per fruit sold.
Could this somehow be done with a variable (VAR)?
Example of data:
This should work.
Score =
VAR fruit = 'Table'[Fruit]
VAR filteredTable = FILTER ( 'Table', NOT ( ISBLANK ( [Sold amount] ) ) && 'Table'[Fruit] = fruit)
RETURN
DIVIDE (
RANKX (
filteredTable,
[Sold amount],
,
ASC
) - 1,
COUNTROWS ( filteredTable ) - 1
)
I am trying to create a CTE to use the same in Merge statement in Oracle, but facing error so please have a look and help me out, earlier we were using subquery instead CTE but to enhance the response time of query I am trying CTE so please suggest me another approach to enhance the response time of query. Curious to know whether oracle supports CTE and Merge statement together as I did in below code.
With TRANS_HIST
As
(Select
NUMERO_DE_CUENTA,
TRANS_DATETIME,
Lag(TRANS_DATETIME, 1)
over
(
ORDER BY NUMERO_DE_CUENTA,TRANS_DATETIME) lag_trans_datetime
FROM db_fraud_bpd.tbl_event_new_transaction_h
)
MERGE
INTO DB_FRAUD_BPD.TBL_RT_FEATURES_TEMP t1
USING
(
SELECT
RTTEMP.ACCOUNT_NUMBER,
RTTEMP.TRANS_DATETIME,
CASE WHEN STDDEV(TRANS_HIST.TRANS_DATETIME - TRANS_HIST.lag_trans_datetime) = 0 THEN NULL
WHEN ROUND(( ( (RTTEMP.TRANS_DATETIME - Max(TRANS_HIST.TRANS_DATETIME)) - Avg(TRANS_HIST.TRANS_DATETIME - TRANS_HIST.lag_trans_datetime)) / STDDEV(TRANS_HIST.TRANS_DATETIME -TRANS_HIST.lag_trans_datetime)),3) >999999999999999 THEN 999999999999999
WHEN ROUND(( ( (RTTEMP.TRANS_DATETIME - Max(TRANS_HIST.TRANS_DATETIME)) - Avg(TRANS_HIST.TRANS_DATETIME - TRANS_HIST.lag_trans_datetime)) / STDDEV(TRANS_HIST.TRANS_DATETIME -TRANS_HIST.lag_trans_datetime)),3) <-99999999999999 THEN -99999999999999
ELSE ROUND(( ( (RTTEMP.TRANS_DATETIME - Max(TRANS_HIST.TRANS_DATETIME)) - Avg(TRANS_HIST.TRANS_DATETIME - TRANS_HIST.lag_trans_datetime)) / STDDEV(TRANS_HIST.TRANS_DATETIME -TRANS_HIST.lag_trans_datetime)),3)
END AS TIME_DELTA_ZSCORE_PAST_90_DAYS
FROM TRANS_HIST
right outer join
db_fraud_bpd.tbl_rt_features_temp RTTEMP
ON Cast(RTTEMP.account_number AS INTEGER) = Cast(TRANS_HIST.NUMERO_DE_CUENTA AS INTEGER)
WHERE
(
TRANS_HIST.TRANS_DATETIME < RTTEMP.TRANS_DATETIME
AND TRANS_HIST.TRANS_DATETIME >= (RTTEMP.TRANS_DATETIME-90)
)
or TRANS_HIST.TRANS_DATETIME IS NULL
GROUP BY
RTTEMP.account_number,
RTTEMP.TRANS_DATETIME
)TEMP
ON (t1.TRANS_DATETIME=TEMP.TRANS_DATETIME AND t1.ACCOUNT_NUMBER=TEMP.ACCOUNT_NUMBER)
WHEN MATCHED
THEN UPDATE
SET t1.TIME_DELTA_ZSCORE_PAST_90_DAYS = TEMP.TIME_DELTA_ZSCORE_PAST_90_DAYS
You may use CTE in the merge statement, but in the rigth position in the merge subquery.
See example below
merge into tab
using (with t as (
select 1 id, 'x' x from dual union all
select 2 id, 'y' x from dual)
select * from t) b
on (tab.id = b.id)
when matched then
update set tab.x = b.x
when not matched then
insert (id, x)
values (b.id, b.x);
Don't use TRANS_HIST as a CTE, but as a subquery.
MERGE INTO DB_FRAUD_BPD.TBL_RT_FEATURES_TEMP t1
USING ( SELECT RTTEMP.ACCOUNT_NUMBER,
RTTEMP.TRANS_DATETIME,
CASE
WHEN STDDEV (
TRANS_HIST.TRANS_DATETIME
- TRANS_HIST.lag_trans_datetime) =
0
THEN
NULL
WHEN ROUND (
( ( ( RTTEMP.TRANS_DATETIME
- MAX (TRANS_HIST.TRANS_DATETIME))
- AVG (
TRANS_HIST.TRANS_DATETIME
- TRANS_HIST.lag_trans_datetime))
/ STDDEV (
TRANS_HIST.TRANS_DATETIME
- TRANS_HIST.lag_trans_datetime)),
3) >
999999999999999
THEN
999999999999999
WHEN ROUND (
( ( ( RTTEMP.TRANS_DATETIME
- MAX (TRANS_HIST.TRANS_DATETIME))
- AVG (
TRANS_HIST.TRANS_DATETIME
- TRANS_HIST.lag_trans_datetime))
/ STDDEV (
TRANS_HIST.TRANS_DATETIME
- TRANS_HIST.lag_trans_datetime)),
3) <
-99999999999999
THEN
-99999999999999
ELSE
ROUND (
( ( ( RTTEMP.TRANS_DATETIME
- MAX (TRANS_HIST.TRANS_DATETIME))
- AVG (
TRANS_HIST.TRANS_DATETIME
- TRANS_HIST.lag_trans_datetime))
/ STDDEV (
TRANS_HIST.TRANS_DATETIME
- TRANS_HIST.lag_trans_datetime)),
3)
END AS TIME_DELTA_ZSCORE_PAST_90_DAYS
FROM (SELECT NUMERO_DE_CUENTA,
TRANS_DATETIME,
LAG (TRANS_DATETIME, 1)
OVER (
ORDER BY NUMERO_DE_CUENTA, TRANS_DATETIME) lag_trans_datetime
FROM db_fraud_bpd.tbl_event_new_transaction_h)
TRANS_HIST
RIGHT OUTER JOIN db_fraud_bpd.tbl_rt_features_temp RTTEMP
ON CAST (RTTEMP.account_number AS INTEGER) =
CAST (TRANS_HIST.NUMERO_DE_CUENTA AS INTEGER)
WHERE ( TRANS_HIST.TRANS_DATETIME < RTTEMP.TRANS_DATETIME
AND TRANS_HIST.TRANS_DATETIME >=
(RTTEMP.TRANS_DATETIME - 90))
OR TRANS_HIST.TRANS_DATETIME IS NULL
GROUP BY RTTEMP.account_number, RTTEMP.TRANS_DATETIME) TEMP
ON ( t1.TRANS_DATETIME = TEMP.TRANS_DATETIME
AND t1.ACCOUNT_NUMBER = TEMP.ACCOUNT_NUMBER)
WHEN MATCHED
THEN
UPDATE SET
t1.TIME_DELTA_ZSCORE_PAST_90_DAYS = TEMP.TIME_DELTA_ZSCORE_PAST_90_DAYS
I have two date columns and trying to measure days between the two dates excluding weekends. I'm getting a negative number and need help solving.
Table
CalendarDate DayNumber FirstAssgn FirstCnt DayNumber2 Id BusinessDays
5/21/2017 Sunday 5/21/17 5/21/17 Sunday 1 -1
Query:
TRUNC(TO_DATE(A.FIRST_CONTACT_DT, 'DD/MM/YYYY')) - TRUNC(TO_DATE(A.FIRST_ASSGN_DT, 'DD/MM/YYYY'))
- ((((TRUNC(A.FIRST_CONTACT_DT,'D'))-(TRUNC(A.FIRST_ASSGN_DT,'D')))/7)*2)
- (CASE WHEN TO_CHAR(A.FIRST_ASSGN_DT,'DY','nls_date_language=english') ='SUN' THEN 1 ELSE 0 END)
- (CASE WHEN TO_CHAR(A.FIRST_CONTACT_DT,'DY','nls_date_language=english')='SAT' THEN 1 ELSE 0 END)
- (SELECT COUNT(1) FROM HUM.CALENDAR CAL
WHERE 1=1
AND CAL.CALENDAR_DATE >= A.FIRST_ASSGN_DT
AND CAL.CALENDAR_DATE < A.FIRST_CONTACT_DT
--BETWEEN A.FIRST_ASSGN_DT AND A.FIRST_CONTACT_DT
AND CAL.GRH_HOLIDAY_IND = 'Y'
) AS Business_Days
Looks like below piece needs editing...
- (CASE WHEN TO_CHAR(A.FIRST_ASSGN_DT,'DY','nls_date_language=english')='SUN' THEN 1 ELSE 0 END)
Adapted from my answer here:
Get the number of days between the Mondays of both weeks (using TRUNC( datevalue, 'IW' ) as an NLS_LANGUAGE independent method of finding the Monday of the week) then add the day of the week (Monday = 1, Tuesday = 2, etc., to a maximum of 5 to ignore weekends) for the end date and subtract the day of the week for the start date. Like this:
SELECT ( TRUNC( end_date, 'IW' ) - TRUNC( start_date, 'IW' ) ) * 5 / 7
+ LEAST( end_date - TRUNC( end_date, 'IW' ) + 1, 5 )
- LEAST( start_date - TRUNC( start_date, 'IW' ) + 1, 5 )
AS WeekDaysDifference
FROM your_table
With RANGE_TEMP as (
SELECT
STARTPERIOD start_date,
ENDPERIOD end_date
FROM
TABLE_DATA -- YOUR TABLE WITH ALL DATA DATE
), DATE_TEMP AS (
SELECT
(start_date + LEVEL) DATE_ALL
FROM
RANGE_TEMP
CONNECT BY LEVEL <= (end_date - start_date)
), WORK_TMP as (
SELECT
COUNT(DATE_ALL) WORK_DATE
FROM
DATE_TEMP
WHERE
TO_CHAR(DATE_ALL,'D', 'NLS_DATE_LANGUAGE=ENGLISH') NOT IN ('1','7')
), BUSINESS_TMP as (
SELECT
COUNT(DATE_ALL) BUSINESS_DATE
FROM
DATE_TEMP
WHERE
TO_CHAR(DATE_ALL,'D', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('1','7')
)
SELECT
L.WORK_DATE,
H.BUSINESS_DATE
FROM
BUSINESS_TMP H,
WORK_TMP L
;
I have a following query and it takes 12 hours to execute in HUE. I would like to increase the performance of the query. Let me know what changes I can implement in the query to increase the performance in HUE environment
SELECT ordernum,
Min(distance) mindist,
Min(CASE
WHEN type_name = 'T'
OR ( type_name = 'I'
AND item LIKE '%D%' ) THEN distance
ELSE 9999999
END) min_t,
Min(CASE
WHEN type_name = 'A' THEN distance
ELSE 9999999
END) min_a
FROM (SELECT a.ordernum,
b.id,
b.type_name,
b.item,
Round(Least(Sqrt(Pow(b.sty-a.nrthng, 2)
+ Pow(b.stx-a.estng, 2)),
Sqrt(Pow(b.endy-a.nrthng, 2)
+ Pow(b.endx-a.estng, 2))))
distance
FROM temp_b a,
min_b1 b
WHERE ( ( b.stx BETWEEN ( a.estng - 1000 ) AND ( a.estng + 1000 )
AND b.sty BETWEEN ( a.nrthng - 1000 ) AND
( a.nthing + 1000 ) )
OR ( b.endx BETWEEN ( a.estng - 1000 ) AND ( a.esng + 1000 )
AND b.endy BETWEEN ( a.nrthng - 1000 ) AND
( a.nrthng + 1000 ) ) )) a
GROUP BY ordernum
My concers are about your query join condition.
As I see, you have tables a and b. Are there any key fields so tables could be matched? I mean, field f1 from the table a has the same meaning as field f2 from table b so they could be joined.
You could also create temporary table containing information from both tables to remove overhead for network communication and data transfer as I believe your hadoop cluster contains more than single node.
I have a sql query which is performing badly, It is causing table scans / IO spikes. Please check the below script and the execution plan and let me know if it requires any indexes or refactoring of the query itself. Thanks
--Query
SELECT TOP 20
CustomerPrimaryExtID,
Max(POSTimeStamp) AS TransactionDate,
ExtLocationCode,
0 AS RedemptionAmount,
0 AS RedemptionCount,
TerminalNum,
LogixTransNum,
POSTransNum AS TransNum,
0 AS DetailRecords,
CustomerTypeID,
PresentedCustomerID,
PresentedCardTypeID,
HHID,
Replayed,
0 AS TransContext,
ISNULL(TransTotal, 0) AS TransTotal
FROM TransHist AS TH WITH(nolock)
WHERE
(
(
( CustomerPrimaryExtID IN ( '' ) AND HHID IS NULL )
OR HHID = '0000000250000007320' AND CustomerTypeID <> 1
) OR ( CustomerPrimaryExtID = '0000000250000007320' AND CustomerTypeID = 1 )
)
AND NOT EXISTS
(
SELECT
LogixTransNum
FROM TransRedemptionView AS TR2
WHERE
(
( ( CustomerPrimaryExtID IN ( '' ) AND HHID IS NULL ) OR HHID = '0000000250000007320' AND CustomerTypeID <> 1 )
OR ( CustomerPrimaryExtID = '0000000250000007320' AND CustomerTypeID = 1 )
)
AND TH.LogixTransNum = TR2.LogixTransNum
)
GROUP BY
CustomerPrimaryExtID,
HHID,
CustomerTypeID,
PresentedCustomerID,
PresentedCardTypeID,
LogixTransNum,
POSTransNum,
TerminalNum,
ExtLocationCode,
Replayed,
TransTotal
ORDER BY TransactionDate DESC