L
Lisa R
Can I find the sum of values of consecutive columns in a row to be displayed
at the end of the row?
at the end of the row?
Ken Sheridan said:Add the values in a computed column in a query, calling the NZ function to
cater for any which are NULL e.g.
SELECT Column1, Column2, Column3
NZ(Column1,0) + NZ(Column2,0) + NZ(Column3,0)
AS SumOfColumns
FROM YourTable;
Whether the columns are 'consecutive' or not is irrelevant.
Ken Sheridan
Stafford, England
I'm attempting to take your advice on this, but keep getting syntax errors
(missing operator) in qtdearnded03.
I copied the query you had below and got the same identical error. When you
mentioned the extra comma, there was in fact one that I believe doesn't
belong:
select qtdearnded00, qtdearnded01, qtdearnded02, qtdearnded03
NZ(qtdearnded00,0) + NZ(qtdearnded01,0) + NZ(qtdearnded02,0) +
NZ(qtdearnded03,0)
AS sumofqts FROM Census2007
When I tried copying and pasting your query and ran it, I got the same
error, then I noticed the comma that doesn't belong right after NZ(
qtdearnded01,0), and took it out, I'm still getting the same error it reads
with more verbeage:
Syntax error (missing operator) in query expression 'qtdearnded03
NZ(qtdearnded00,0) + NZ(qtdearnded01,0) +
NZ(qtdearnded02,0) + NZ(qtdearnded03,0)
Actually I was able to do it with out the NZ function,
SELECT Census2007.name,
(qtdearnded00)+(qtdearnded01)+(qtdearnded02)+(qtdearnded03) AS sumofqts INTO
[Sum of 401K (ordered by name)]
FROM Census2007
ORDER BY Census2007.name;
I'm going to give it a lookthrough, but it appears to be working.
I wouldn't EVER peacemeal this the way I am, but I've been given a task that
I really don't have a better way to do right now because I don't have the
luxury of time to learn a reporting program. I've always been the DBA
writing the queries to extract the data, then pass it off to the developers
who make all this magic happen.
This was a task handed to me with a tight deadline, moving forward, if this
is something that's going to be expected of me on an anual basis, I've got to
make it a priority to do this in a more elegant way. Do you have any
suggestions for how I can go about doing this? Initially this came out of a
SQL Server database, surely there's a better way to do this, maybe through
SQL Server reporting services or something?
The challange is the program doesn't total up the earnings and deductions,
each item has it's own code, and the data is seperated by quarters. In order
to get all the types of earnings and deductions the queries bring back
several rows per employee. It's a real mess to contend with. This program
should have a canned report to take care of messes like this, but it doesn't.
Somehow I've got to educate myself to be able to take data like this and
write a report that does all the calculations... Whether it be in SQL Server
or Access, I don't care, but it's got to be less labor intensive than how I'm
doing it now due to lack of knowing how to do it "The right way".
Penny said:Actually I was able to do it with out the NZ function,
SELECT Census2007.name,
(qtdearnded00)+(qtdearnded01)+(qtdearnded02)+(qtdearnded03) AS sumofqts INTO
[Sum of 401K (ordered by name)]
FROM Census2007
ORDER BY Census2007.name;
I'm going to give it a lookthrough, but it appears to be working.
I wouldn't EVER peacemeal this the way I am, but I've been given a task that
I really don't have a better way to do right now because I don't have the
luxury of time to learn a reporting program. I've always been the DBA
writing the queries to extract the data, then pass it off to the developers
who make all this magic happen.
This was a task handed to me with a tight deadline, moving forward, if this
is something that's going to be expected of me on an anual basis, I've got to
make it a priority to do this in a more elegant way. Do you have any
suggestions for how I can go about doing this? Initially this came out of a
SQL Server database, surely there's a better way to do this, maybe through
SQL Server reporting services or something?
The challange is the program doesn't total up the earnings and deductions,
each item has it's own code, and the data is seperated by quarters. In order
to get all the types of earnings and deductions the queries bring back
several rows per employee. It's a real mess to contend with. This program
should have a canned report to take care of messes like this, but it doesn't.
Somehow I've got to educate myself to be able to take data like this and
write a report that does all the calculations... Whether it be in SQL Server
or Access, I don't care, but it's got to be less labor intensive than how I'm
doing it now due to lack of knowing how to do it "The right way".
John W. Vinson said:Apologies... it wasn't an extra comma it was a misplaced comma. The error
message does in fact explain: there is no operator between the fieldname
qtdearnded03 and the calculated expression. There needs to be a comma:
select qtdearnded00, qtdearnded01, qtdearnded02, qtdearnded03,
NZ(qtdearnded00,0) + NZ(qtdearnded01,0) + NZ(qtdearnded02,0) +
NZ(qtdearnded03,0)
AS sumofqts FROM Census2007
maybe I'm making this more complicated than it needs to be. Is there a way
to group all the different values for each employee and roll it up into just
one row?
In all honesty I'm not sure what about the structure you're asking for.
We're only dealing with 2 tables here, the employee and earnings table.
Unfortunately the system doesn't store the earnings/deductions in a column
that's already summed up, it only stores the data on a quarterly basis. The
idea is to get a single row back per employee.
To make this even more aggravating is the fact that I'm also dealing with
several variations of deductions that are stored as codes which brings back
several rows per employee based on 401K, 401K1, 401KD, 401KDR, 125D, 125I,
and 125M.
Although I can select the data, I don't know how to sum and roll all these
different deductions up into a single row per employee.
Does this give you enough info?