How can I use SUM() to sum my result array? - sql

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'

Related

LEFT JOIN question - Pervasive SQL - combining multiple records from one table into a single column

I am having some trouble with getting data into a column from a combination of two tables.
I'm using pervasive SQL and don't have access to pivot tables. I did a left join and tried union but am not getting the results I want.
I have a primary table - table A - That for a given contract has 10 summary 'colors'.
I am using a select statement and a case clause to string the 10 unique colors together with commas to properly determine the number of commas I am checking for values in those color fields.
This works well - I get a single row with all of the colors listed (10 fields into 1)
Table B contains records for the same contract and can have almost up to 2000 colors (detail colors).
If table B has colors then Table A doesn't.
Is there a way to select all of the colors in Table B into a single row and column and show all of the other column data in Table A?
What I want :
Contract +---------+ PHONENO +----------+ Color +------+------+
TEST 555-555-5555 BLUE,RED,GREEN,YELLOW
Instead of what I'm currently getting:
Contract+---------+ PHONENO +----------+ Color +------+------+
TEST 555-555-5555 BLUE
TEST 555-555-5555 RED
TEST 555-555-5555 GREEN
TEST 555-555-5555 YELLOW
SELECT TableA.ContractNo AS Contract,
TableA.PhoneNo,
CASE
WHEN TableA.Color10 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5 + ',' + TableA.Color6 + ',' + TableA.Color7 + ',' + TableA.Color8 + ',' + TableA.Color9 + ',' + TableA.Color10)
WHEN TableA.Color9 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5 + ',' + TableA.Color6 + ',' + TableA.Color7 + ',' + TableA.Color8 + ',' + TableA.Color9)
WHEN TableA.Color8 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5 + ',' + TableA.Color6 + ',' + TableA.Color7 + ',' + TableA.Color8)
WHEN TableA.Color7 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5 + ',' + TableA.Color6 + ',' + TableA.Color7)
WHEN TableA.Color6 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5 + ',' + TableA.Color6)
WHEN TableA.Color5 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5)
WHEN TableA.Color4 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4)
WHEN TableA.Color3 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3)
WHEN TableA.Color2 <> '' then (TableA.Color1 + ',' + TableA.Color2)
WHEN TableA.Color1 <> '' then (TableA.Color1)
WHEN TableB.Color <> '' then TableB.Color
ELSE 'n/a'
end as Color
FROM TableA
LEFT JOIN TableB ON (TableA.ContractNo = TableB.ContractNo)
ORDER BY TableA.ContractNo
Can anyone help out with this?

How to optimise below sql query

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.

SQL select, sum and group

I'm struggling with an Access database:
I have a large database with the headers TAG, ZST, KL and R1H00 to R1H23 (24 of them, each one stands for one hour of a day) and I need to get a specific dataset out of it:
SELECT TAG, ZST, (R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
WHERE KL = "SWISS7_PW"
So far so good, but the result contains many items with the same ID (ZST). I need to sum all entries with the same ZST, but I couldn't manage to do it so far. (I tried to use the GROUP BY statement, but that only results in errors)
Any experienced SQL people here that could help me with this?
use group by
SELECT TAG, ZST, sum(R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
WHERE KL = "SWISS7_PW"
GROUP BY TAG, ZST;
Use the Sum() function
SELECT TAG, ZST, Sum(R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
where ZST = '(Whatever it is)'
group by tag, zst

Eliminating multiple delimiters in single column in oracle

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;

IF THEN Statement in SQL to OpenEdge

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')