I have a backend limitation that the Resultant column which is of decimal type(aban) can not have more than 1000 characters in it formula.
How to optimise this query?
I have tried using tempTable but results are not as expected.
aban =
CASE
WHEN (
sum(isnull(a.callshandled,0)) + sum(isnull(a.totalcallsaband,0)) + sum(isnull(a.incompletecalls,0)) + sum(isnull(a.returnbusy,0)) + sum(isnull(a.returnring,0)) + sum(isnull(a.icrdefaultrouted,0)) + sum(isnull(a.ndr,0)) + sum(isnull(a.overflowout,0)) + sum(isnull(a.callsrona,0)) + sum(isnull(a.returnrelease,0)) + sum(isnull(a.callsroutednonagent,0)) + sum(isnull(a.shortcalls,0)) + sum(isnull(a.agenterrorcount,0)) + sum(isnull(a.errorcount,0))
)
= 0 THEN
0
ELSE (sum(isnull(a.totalcallsaband,0)) * 1.0) / (sum(isnull(a.callshandled,0)) + sum(isnull(a.totalcallsaband,0)) + sum(isnull(a.incompletecalls,0)) + sum(isnull(a.returnbusy,0)) + sum(isnull(a.returnring,0)) + sum(isnull(a.icrdefaultrouted,0)) + sum(isnull(a.networkdefaultrouted,0)) + sum(isnull(a.overflowout,0)) + sum(isnull(a.callsrona,0)) + sum(isnull(a.returnrelease,0)) + sum(isnull(a.callsroutednonagent,0)) + sum(isnull(a.shortcalls,0)) + sum(isnull(a.agenterrorcount,0)) + sum(isnull(a.errorcount,0)))
END
,
Optimise the query in such a way that number of characters in formula is less then 1000 and resultant column should have same value before and after query optimisation.
Please try this way:
DECLARE #dividend int = 10;
DECLARE #divisor int = 0;
SELECT COALESCE(#dividend / NULLIF(#divisor,0), 0);
So you can avoid duplicate calculation for divisor.
My table has single column info
Info
+aname + + + + + + +ano+ + + + + + + + + +agender+ + + + + + + + + + + + +arace
I should get output like
aname+ano+agender+arace
I have to eliminate multiple delimiters and replace with single +
I tried with regexp_replace and trim and working as below
select trim(REGEXP_REPLACE('+aname + + + + + + +
+ano + + + + + + + +
+agender+ + + + + + + + + + +
+arace', '\ + + ', '+'),'+') from dual;
i get output as
aname+++++++ano+++++++agender++++++++++arace
This regexp does the trick: '\++'
select REGEXP_REPLACE('+aname++++++ano+++++++++agender++++++++++++arace', '\++', '+') from dual;
To get rid of the leading + (like in your example), use ltrim (or trim to remove trailing + too).
select ltrim(REGEXP_REPLACE('+aname++++++ano+++++++++agender++++++++++++arace', '\++', '+'),'+') from dual;
When I run the following query I get the above error. I realize this is because the subquery is returning more than 1 result but am not sure how to fix it.
update a
set a.covtypeplus = (SELECT distinct
REPLACE(REPLACE(ISNULL([BI],'x') + '+' + ISNULL([PD],'x') + '+' +
ISNULL([COM],'x') + '+' + ISNULL([COMGL],'x') + '+' + ISNULL([COL],'x') + '+' + ISNULL([COLDW],'x') + '+' +
ISNULL([MED],'x') + '+' + ISNULL([PIP],'x') + '+' + ISNULL([PIPNI],'x') + '+' + ISNULL([PIPNR],'x') + '+' +
ISNULL([UM],'x') + '+' + ISNULL([UMBI],'x') + '+' + ISNULL([UMBA],'x') + '+' + ISNULL([UMS],'x') + '+' +
ISNULL([UMPD],'x') + '+' + ISNULL([UMPA],'x') + '+' +
ISNULL([UIM],'x') + '+'+ ISNULL([UIMBI],'x') + '+' + ISNULL([UIMC],'x') + '+' + ISNULL([UIS],'x') + '+' +
ISNULL([UIMPD],'x') + '+' + ISNULL([MEX],'x') + '+' +
ISNULL([ACC],'x') + '+' + ISNULL([CPI],'x') + '+' + ISNULL([INC],'x') + '+' + ISNULL([FUN],'x') + '+' + ISNULL([XMD],'x')
, 'x+', ''), '+x', '') AS CovTypePlus
FROM (SELECT distinct POLICYID, VERSION, COVCODE FROM Staging.Coverage) p LEFT JOIN (
SELECT *
FROM (
SELECT POLICYID, VERSION, COVCODE
FROM Staging.Coverage
WHERE SUBSTRING(COVCODE, 1, 2) IN ('BI','PD','CO','ME','PI','UM','UI','AC','CP','IN','FU','XM','LD','RA','RE','RO','SP','TO','SG','WM')) p
PIVOT (MAX(COVCODE) FOR COVCODE IN (
[BI], [PD], [COM], [COMGL], [COL], [COLDW],
[MED], [PIP], [PIPNI], [PIPNR],
[UM], [UMBI], [UMBA], [UMS], [UMPD], [UMPA],
[UIM], [UIMBI], [UIMC], [UIS], [UIMPD],
[ACC], [CPI], [INC], [FUN], [XMD], [LD], [MEX], [RA], [REN], [ROADS], [SPE], [TOW], [SGC], [WMAR]
)) AS pvt) vc ON p.POLICYID = vc.POLICYID AND p.VERSION = vc.VERSION)
from results_vehicle a
left join staging.coverage c on a.polnum =c.policyid and a.polver = c.version and a.covcode = c.COVCODE
When I run the following original query I created (not an update) it works fine so it must be something I am doing wrong in my update syntax:
SELECT DISTINCT p.POLICYID AS PolNum, p.VERSION As PolVer,
REPLACE(REPLACE(ISNULL([BI],'x') + '+' + ISNULL([PD],'x') + '+' +
ISNULL([COM],'x') + '+' + ISNULL([COMGL],'x') + '+' + ISNULL([COL],'x') + '+' + ISNULL([COLDW],'x') + '+' +
ISNULL([MED],'x') + '+' + ISNULL([PIP],'x') + '+' + ISNULL([PIPNI],'x') + '+' + ISNULL([PIPNR],'x') + '+' +
ISNULL([UM],'x') + '+' + ISNULL([UMBI],'x') + '+' + ISNULL([UMBA],'x') + '+' + ISNULL([UMS],'x') + '+' +
ISNULL([UMPD],'x') + '+' + ISNULL([UMPA],'x') + '+' +
ISNULL([UIM],'x') + '+'+ ISNULL([UIMBI],'x') + '+' + ISNULL([UIMC],'x') + '+' + ISNULL([UIS],'x') + '+' +
ISNULL([UIMPD],'x') + '+' + ISNULL([MEX],'x') + '+' +
ISNULL([ACC],'x') + '+' + ISNULL([CPI],'x') + '+' + ISNULL([INC],'x') + '+' + ISNULL([FUN],'x') + '+' + ISNULL([XMD],'x')
, 'x+', ''), '+x', '') AS CovTypePlus
FROM (SELECT DISTINCT POLICYID, VERSION, COVCODE FROM Staging.Coverage) p LEFT JOIN (
SELECT *
FROM (
SELECT POLICYID, VERSION, COVCODE
FROM Staging.Coverage
WHERE SUBSTRING(COVCODE, 1, 2) IN
('BI','PD','CO','ME','PI','UM','UI','AC','CP','IN','FU','XM','LD','RA','RE','RO','SP','TO','SG','WM')) p
PIVOT (MAX(COVCODE) FOR COVCODE IN (
[BI], [PD], [COM], [COMGL], [COL], [COLDW],
[MED], [PIP], [PIPNI], [PIPNR],
[UM], [UMBI], [UMBA], [UMS], [UMPD], [UMPA],
[UIM], [UIMBI], [UIMC], [UIS], [UIMPD],
[ACC], [CPI], [INC], [FUN], [XMD], [LD], [MEX], [RA], [REN], [ROADS], [SPE], [TOW], [SGC], [WMAR]
)) AS pvt) vc ON p.POLICYID = vc.POLICYID AND p.VERSION = vc.VERSION
Any help is greatly appreciated, I've tried playing around with the query and yield the same results. I also tried adding a SELECT TOP 1 and got the same results for all policies.
You are doing:
update a
set a.covtypeplus = (SELECT distinct . . .
This suggests that you are expecting more than one result from that subquery. Not allowed.
Perhaps this will fix it, but it may not be what you want:
update a
set a.covtypeplus = (SELECT top 1 . . .
It appears to be this part that is messing you up:
from results_vehicle a
left join staging.coverage c on a.polnum =c.policyid
and a.polver = c.version and a.covcode = c.COVCODE
This must be generating more than one result for some values of a.covcode.
I'm struggeling with an if statement towards an OpenEdge database via ODBC. The query I have works like a charm, but Progress wants me to define the if statement different and I have no clue. Is there anyone who can define the correct query for me?
IF
{Crediteur code|type=string} = 'HOEFASTE'
BEGIN
(SELECT
inok."vest-kd-arvo"+RTRIM('-') + RIGHT(CAST((inok."inok-nr" + 1000000) AS varchar(7)),6) AS inokvalue
,RTRIM("leso-kd")+RTRIM('-')+inok."vest-kd-arvo"+RTRIM('-')+CAST("inok-nr"AS varchar(6)) AS inokdisplay
,RIGHT(CAST((inok."inok-nr") AS varchar(7)),6) + '-' + inok."vest-kd-arvo" + '-' + RTRIM("leso-kd") AS inoknr
,RTRIM("leso-kd") AS soortfact
FROM
PUB.inok
WHERE
inok."vcdc-nr" = {Administratie|type=int32}
AND inok."inss-kd" NOT IN ('H','G')
)
ELSE
(SELECT
inok."vest-kd-arvo"+RTRIM('-') + RIGHT(CAST((inok."inok-nr" + 1000000) AS varchar(7)),6) AS inokvalue
,RTRIM("leso-kd")+RTRIM('-')+inok."vest-kd-arvo"+RTRIM('-')+CAST("inok-nr"AS varchar(6)) AS inokdisplay
,RIGHT(CAST((inok."inok-nr") AS varchar(7)),6) + '-' + inok."vest-kd-arvo" + '-' + RTRIM("leso-kd") AS inoknr
,RTRIM("leso-kd") AS soortfact
FROM
PUB.inok
WHERE
inok."rela-kd-kre" = {Crediteur code|type=string}
AND inok."vcdc-nr" = {Administratie|type=int32}
AND inok."inss-kd" NOT IN ('H','G')
)
END
If I understand correctly, only your WHERE-clause is different, based on "Crediteur code".
You could move the IF inside of the WHERE-clause. That will leave you with a single SELECT-statement. Does this alternative work for you?
SELECT
inok."vest-kd-arvo"+RTRIM('-') + RIGHT(CAST((inok."inok-nr" + 1000000) AS varchar(7)),6) AS inokvalue
,RTRIM("leso-kd")+RTRIM('-')+inok."vest-kd-arvo"+RTRIM('-')+CAST("inok-nr"AS varchar(6)) AS inokdisplay
,RIGHT(CAST((inok."inok-nr") AS varchar(7)),6) + '-' + inok."vest-kd-arvo" + '-' + RTRIM("leso-kd") AS inoknr
,RTRIM("leso-kd") AS soortfact
FROM
PUB.inok
WHERE
inok."rela-kd-kre" = (CASE WHEN {Crediteur code|type=string} = 'HOEFASTE' THEN inok."rela-kd-kre" ELSE {Crediteur code|type=string} END)
AND inok."vcdc-nr" = {Administratie|type=int32}
AND inok."inss-kd" NOT IN ('H','G')
My current method to add the rows together is like so:
$totalxp = $row['Attackxp'] + $row['Defencexp'] + $row['Strengthxp'] + $row['Hitpointsxp'] + $row['Rangedxp'] + $row['Prayerxp'] + $row['Magicxp'] + $row['Cookingxp'] + $row['Woodcuttingxp'] + $row['Fletchingxp'] + $row['Fishingxp'] + $row['Firemakingxp'] + $row['Craftingxp'] + $row['Smithingxp'] + $row['Miningxp'] + $row['Herblorexp'] + $row['Agilityxp'] + $row['Thievingxp'] + $row['Slayerxp'] + $row['Farmingxp'] + $row['Runecraftxp'] + $row['Constructionxp'];
But then I saw SUM() and I tried this:
SELECT SUM(xp) FROM skills WHERE playerName='Undercover'
It works but I needed all the values of xp, so I tried adding %xp but it wont work.
How could I use the Sum() function to add all the rows up instead of straining PHP?
Aggregate functions (IE: SUM, MIN, MAX, COUNT, etc) don't work across columns--they work on the values for the specific column, based on the grouping (GROUP BY) and filteration (JOIN and/or WHERE clause).
To add up values across columns, you need to add them like you would for normal mathematical equations:
SELECT Attackxp + Defencexp + Strengthxp + Hitpointsxp + Rangedxp + Prayerxp + Magicxp + Cookingxp+ Woodcuttingxp + Fletchingxp + Fishingxp + Firemakingxp + Craftingxp + Smithingxp + Miningxp + Herblorexp + Agilityxp + Thievingxp + Slayerxp + Farmingxp + Runecraftxp + Constructionxp AS total_xp
FROM skills
WHERE playerName = 'Undercover'
If you have more than one record associated to a playername, then you can use an aggregate function:
SELECT SUM(Attackxp + Defencexp + Strengthxp + Hitpointsxp + Rangedxp + Prayerxp + Magicxp + Cookingxp+ Woodcuttingxp + Fletchingxp + Fishingxp + Firemakingxp + Craftingxp + Smithingxp + Miningxp + Herblorexp + Agilityxp + Thievingxp + Slayerxp + Farmingxp + Runecraftxp + Constructionxp) AS total_xp
FROM skills
WHERE playerName = 'Undercover'
That depend of the table data if each player is one entity (row) then is enaught to add columns:
SELECT Attackxp + Defencexp + Strengthxp + Hitpointsxp +Rangedxp + Prayerxp + Magicxp + Cookingxp + Woodcuttingxp + Fletchingxp + Fishingxp + Firemakingxp + Craftingxp + Smithingxp + Miningxp + Herblorexp + Agilityxp + Thievingxp + Slayerxp + Farmingxp + Runecraftxp + Constructionxp
As totalSkills FROM skills WHERE playerName = 'Undercover'
But is there more rows per player then You will need to sum also the rows
SELECT SUM(Attackxp + Defencexp + Strengthxp + Hitpointsxp +Rangedxp + Prayerxp + Magicxp + Cookingxp + Woodcuttingxp + Fletchingxp + Fishingxp + Firemakingxp + Craftingxp + Smithingxp + Miningxp + Herblorexp + Agilityxp + Thievingxp + Slayerxp + Farmingxp + Runecraftxp + Constructionxp)
As totalSkills FROM skills WHERE playerName = 'Undercover'
SELECT SUM(`Attackxp`) + SUM(`Defencexp`) + ... AS `total_sum`
FROM skills
WHERE playerName='Undercover'