473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SQL Query from iSeries Data Transfer

I am trying to run a SQL query as follows:

SELECT INVMST.INUMBR, INVMST.IDESCR,
CASE WHEN SUM(INVCHS.CHQT01) = 0 THEN SUM(INVCHS1.CHQT01) ELSE
SUM(INVCHS.CHQT01) END as QTY1,
CASE WHEN SUM(INVCHS.CHQT02) = 0 THEN SUM(INVCHS1.CHQT02) ELSE
SUM(INVCHS.CHQT02) END as QTY2,
SUM(QTY1+QTY2) as SUMQTY FROM ............etc

The actual query uses QTY3 through QTY12 with CASE statements as
well, I just chopped it down a bit for readability. My question is how
do I add a field to the query resutls that is the SUM of QTY+QTY2+...
+QTY12? The QTYX values are dependent on case statements for their
assigned values. The SUM(QTY1+QTY2+QTY3+etc.) statement gives an
error:

SQL0206 - Column QTY1 not in specified tables.

Cause ...... : QTY1 is not a column of table *N in *N, QTY1 is not a
column of any table or view that can be referenced. (etc.)

What is the correct SQL query way to return a field that is the SUM
of QTY1 through QTY12?

Thanks for any help!!

ASM

Mar 12 '07 #1
3 10120
ah***@chebucto.ns.ca wrote:
I am trying to run a SQL query as follows:

SELECT INVMST.INUMBR, INVMST.IDESCR,
CASE WHEN SUM(INVCHS.CHQT01) = 0 THEN SUM(INVCHS1.CHQT01) ELSE
SUM(INVCHS.CHQT01) END as QTY1,
CASE WHEN SUM(INVCHS.CHQT02) = 0 THEN SUM(INVCHS1.CHQT02) ELSE
SUM(INVCHS.CHQT02) END as QTY2,
SUM(QTY1+QTY2) as SUMQTY FROM ............etc

The actual query uses QTY3 through QTY12 with CASE statements as
well, I just chopped it down a bit for readability. My question is how
do I add a field to the query resutls that is the SUM of QTY+QTY2+...
+QTY12? The QTYX values are dependent on case statements for their
assigned values. The SUM(QTY1+QTY2+QTY3+etc.) statement gives an
error:

SQL0206 - Column QTY1 not in specified tables.

Cause ...... : QTY1 is not a column of table *N in *N, QTY1 is not a
column of any table or view that can be referenced. (etc.)

What is the correct SQL query way to return a field that is the SUM
of QTY1 through QTY12?
Could you SELECT QTY1..QTY12 into host variables and then SUM the host
variables?

-Philip
Mar 12 '07 #2
ah***@chebucto.ns.ca wrote:
I am trying to run a SQL query as follows:

SELECT INVMST.INUMBR, INVMST.IDESCR,
CASE WHEN SUM(INVCHS.CHQT01) = 0 THEN SUM(INVCHS1.CHQT01) ELSE
SUM(INVCHS.CHQT01) END as QTY1,
CASE WHEN SUM(INVCHS.CHQT02) = 0 THEN SUM(INVCHS1.CHQT02) ELSE
SUM(INVCHS.CHQT02) END as QTY2,
SUM(QTY1+QTY2) as SUMQTY FROM ............etc

The actual query uses QTY3 through QTY12 with CASE statements as
well, I just chopped it down a bit for readability. My question is how
do I add a field to the query resutls that is the SUM of QTY+QTY2+...
+QTY12? The QTYX values are dependent on case statements for their
assigned values. The SUM(QTY1+QTY2+QTY3+etc.) statement gives an
error:

SQL0206 - Column QTY1 not in specified tables.

Cause ...... : QTY1 is not a column of table *N in *N, QTY1 is not a
column of any table or view that can be referenced. (etc.)

What is the correct SQL query way to return a field that is the SUM
of QTY1 through QTY12?
I dont know db2 for iseries so the following might not apply. Reason you
cant add QTY1, QTY2 etc is that the "rename" operation takes part after
the calculation, so QTY1 are not known by then.

Either sum using the case stmts:

SELECT INVMST.INUMBR, INVMST.IDESCR,
CASE WHEN SUM(INVCHS.CHQT01) = 0 THEN SUM(INVCHS1.CHQT01) ELSE
SUM(INVCHS.CHQT01) END as QTY1,
CASE WHEN SUM(INVCHS.CHQT02) = 0 THEN SUM(INVCHS1.CHQT02) ELSE
SUM(INVCHS.CHQT02) END as QTY2,
SUM(CASE WHEN SUM(INVCHS.CHQT01) = 0 THEN SUM(INVCHS1.CHQT01) ELSE
SUM(INVCHS.CHQT01) END + ... FROM

or encapsulate it in an inline view

SELECT INUMBR, IDESCR, QTY1, ..., QTY1+QTY2+... FROM (
<your query goes here>
) X

or in a CTE

WITH TMP (INUMBR, IDESCR, QTY1,...) as (
<your query goes here>
)
select TMP.*, QTE1+QTE2+... FROM TMP
HTH
/Lennart

Mar 12 '07 #3
On Mar 12, 10:57 am, Lennart <erik.lennart.jons...@gmail.comwrote:
<... snip ...>
or in a CTE

WITH TMP (INUMBR, IDESCR, QTY1,...) as (
<your query goes here>
)
select TMP.*, QTE1+QTE2+... FROM TMP

HTH
/Lennart
Thanks a lot, this worked perfectly!

ASM

---

Mar 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thad Rizzi | last post by:
My webserver is an iSeries and I want to run some PHP scripts on it using SQL to access data on the iSeries. Does anyone have any exapmles or resources on the most effective way to do this? I...
8
by: VPaul | last post by:
Is there a web page out there like this one ... http://www.sql-tutor.com/sql_tutor/database.asp that would allow me to test some SQL to a DB2? In particular to an AS/400. I am learning and find...
4
by: erkki | last post by:
Hi, I have to move iSeries library to new DB2 database. Is there any easier way to do this moving operation than exporting all tables in iSeries into text files and then import them into DB2...
1
by: Yanik Reid | last post by:
When I run the following vb.net code, I'm not able to acces the my database AMFLIB. I use to be able to access AMFLIB with the old ODBC driver from IBM iSeries Access but now I want to use the...
6
by: MadMan2004 | last post by:
Hello all! I'm having a problem with a project I'm working on and I'd like to ask for anyone's input that might be helpful. I'm building a rather large front-end application connecting to an...
3
by: roberto | last post by:
I have a table (ORDERS) with this kind structure (this is not the really table): OrderId Product Amount Agent1 Agent2 Agent3 ________ _______ ______ ______ ______ ______ 000001 ...
1
by: kellyj00 | last post by:
We have clients who have not been exposed to SQL and have built a few thousand queries using the iSeries Query tool via their 5250 sessions. Is there any way to gather the code behind these...
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
1
by: sqldood | last post by:
Hi, We have currently made an application on db2 express c but now our boss wants to transfer it on Power6/Iseries after the recent purchase. We want to migrate all the tables with the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.