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
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
)
it says "not a groupby function". when I add D.unitpricef to groupby it doesn't show any error,but in result it shows same itmcode multiple time. one item code should diplay only one time
SELECT
ItemCode,
case when H.InvoType = 1 then concat(ItemCode,' ( SLT Equipment )' ) else concat(ItemCode,' (
CONBES Eqipment )' ) end as EquName,
case when TRIM(SUM(QTY)) is null then '' else TRIM(TO_CHAR(SUM(QTY),'999999')) end as QTY,
CAST( (SUM(QTY) * D.unitpricef) AS NUMBER(38,2)) AS Amount
FROM Invoicedetails D
INNER JOIN
invoiceheader H ON D.InvoiceNo = H.InvoiceNo
INNER JOIN Equipment E ON E.EquCode = ItemCode
WHERE
H.CancelStat= 0
AND H.ReceiptStat = 1
AND H.BCCODE = 'xxx'
GROUP BY ItemCode,H.InvoType ORDER BY ItemCode ASC;
You need to group by using the same expressions associated to the aggregate function
SELECT
ItemCode,
case when H.InvoType = 1 then concat(ItemCode,' ( SLT Equipment )' ) else concat(ItemCode,' ( CONBES Eqipment )' ) end as EquName,
case when TRIM(SUM(QTY)) is null then '' else TRIM(TO_CHAR(SUM(QTY),'999999')) end as QTY,
CAST( (SUM(QTY) * D.unitpricef) AS NUMBER(38,2)) AS Amount
FROM Invoicedetails D
INNER JOIN
invoiceheader H ON D.InvoiceNo = H.InvoiceNo
INNER JOIN Equipment E ON E.EquCode = ItemCode
WHERE
H.CancelStat= 0
AND H.ReceiptStat = 1
AND H.BCCODE = 'xxx'
GROUP BY
ItemCode
case when H.InvoType = 1 then concat(ItemCode,' ( SLT Equipment )' ) else concat(ItemCode,' ( CONBES Eqipment )' ) end
ORDER BY ItemCode ASC;
I want to have a column in power bi showing the growth rate of sales. I have a table like
year
count
1395
123
1396
232
1397
23
1398
908
1399
678
1400
34
the growth rate is (this year - previous year)/previous year
could you please guide me how I can do this?
When I use the growth the data is like below
You can add a calculated column like this:
growth =
VAR _currentcount = 'Table'[count]
VAR _currentyear = 'Table'[year]
VAR _previouscount =
CALCULATE (
SELECTEDVALUE ( 'Table'[count] ) ,
ALL ( 'Table' ) ,
'Table'[year] = _currentyear - 1
)
RETURN
IF (
NOT ISBLANK ( _previouscount ) ,
DIVIDE ( _currentcount , _previouscount ) - 1
)
or a measure like this, to be used with your year dimension:
growth_measure =
VAR _currentcount = SELECTEDVALUE ( 'Table'[count] )
VAR _currentyear = SELECTEDVALUE ( 'Table'[year] )
VAR _previouscount =
CALCULATE (
SELECTEDVALUE ( 'Table'[count] ) ,
ALL ( 'Table' ) ,
'Table'[year] = _currentyear - 1
)
RETURN
IF (
NOT ISBLANK ( _previouscount ) ,
DIVIDE ( _currentcount , _previouscount ) - 1
)
Giving this result:
All depending on your needs.
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.
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.