473,386 Members | 1,842 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,386 software developers and data experts.

How do I code this complex select statement

Hello,

I have two tables I'm working on:

In tableA each row consists of an object, its required status and its
current status e.g.

Object Req Status Cur Status

MYOBJECT1 OK NOTOK
MYOBJECT2 *MYSTS PROBLEM

In the above the Req Status column can be a single value (OK) or point to a
list of values (*MYSTS) held in tableB.

If the Req Status column is a list (denoted by the * in the 1st char
position) then I need to refer to tableB for the list of statuses e.g.

tableB

Spec Stat Valid Stat

*MYSTS OK
*MYSTS MAYBE
*MYSTS UNCERTAIN
*JSSTS NOTOK
*JSSTS UNCERTAIN
What I need to do is:

Select all objects from tableA where the Cur Status is < > to the Req Status

That's easy enough. In pseudo programming terms I need to say:

If tableA.Req Status field is a special status (* in first char position)
then compare tableA.Cur Status against the tableB.Valid Status entries
where tableB.Spec Stat = tableA.Req Status

else

just compare the Cur Status to the Req Status column

endif

Can anyone help me on this?

Thanks
Glenn

--
Glenn
Nov 12 '05 #1
1 1340
An example:
CREATE TABLE GTableA
(Object CHAR(10) NOT NULL
,Req_Status CHAR(10) NOT NULL
,Cur_Status CHAR(10) NOT NULL
);

INSERT INTO GTableA
VALUES
('MYOBJECT1', 'OK', 'NOTOK')
,('MYOBJECT2', '*MYSTS', 'PROBLEM')
;

CREATE TABLE GTableB
(Spec_Stat CHAR(10) NOT NULL
,Valid_Stat CHAR(10) NOT NULL
);

INSERT INTO GTableB
VALUES
('*MYSTS', 'OK')
,('*MYSTS', 'MAYBE')
,('*MYSTS', 'UNCERTAIN')
,('*JSSTS', 'NOTOK')
,('*JSSTS', 'UNCERTAIN')
;

------------------------- Commands Entered --------------------------
SELECT Object
, COALESCE('(V)'||b.Valid_Stat, '(R)'||a.Req_Status) Req_or_Valid
, Cur_Status
FROM GTableA a
LEFT OUTER JOIN
GTableB b
ON SUBSTR(a.Req_Status,1,1) = '*'
AND b.Spec_Stat = a.Req_Status
WHERE a.Req_Status <> a.Cur_Status
ORDER BY
Object;
--------------------------------------------------------------------

OBJECT REQ_OR_VALID CUR_STATUS
---------- ------------- ----------
MYOBJECT1 (R)OK NOTOK
MYOBJECT2 (V)OK PROBLEM
MYOBJECT2 (V)MAYBE PROBLEM
MYOBJECT2 (V)UNCERTAIN PROBLEM

4 record(s) selected.
If all the values of b.Spec_Stat in TableB begin with '*',
ON condition SUBSTR(a.Req_Status,1,1) = '*' would be not necessary.
SELECT Object
, COALESCE('(V)'||b.Valid_Stat, '(R)'||a.Req_Status) Req_or_Valid
, Cur_Status
FROM GTableA a
LEFT OUTER JOIN
GTableB b
ON b.Spec_Stat = a.Req_Status
WHERE a.Req_Status <> a.Cur_Status
ORDER BY
Object;

Nov 12 '05 #2

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

Similar topics

1
by: brett | last post by:
Here is my SQL string: "SELECT to_ordnum, to_orddate," _ & "(SELECT SUM((DDPROD.pr_stanmat * DDPROD.pr_prfact) * (DOBOM2.b2_quant * DDORD.or_quant)) FROM DDPROD INNER JOIN DOBOM2 ON...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
6
by: Hardy | last post by:
One of my customers have a sql statement totaled more than 400 lines, about 40KB. when excuted, error arrised saying "SQL0101N The statement is too long or too complex". I tried one of his...
3
by: Susie Swint | last post by:
I have the following IIf statement which worked in Access 95 but will not work in Access 2002. The error message I get is that the expression is typed incorrectly, or is too complex to be...
2
by: Ben de Vette | last post by:
Hi, I'm using the querybuilder when updating a record in a table (Access). However, I get a "Query is too complex" message. The Primary key is autonumbered. Why is it making such a complex...
4
by: William | last post by:
After much frustration I was able to update my data store via code only. Using the data adapter was the only way I was able to set up all the objects written in my code. Basically, I cheated by...
22
by: pbd22 | last post by:
hi. I am having probelms with an update statement. every time i run it, "every" row updates, not just the one(s) intended. so, here is what i have. i have tried this with both AND and OR and...
14
by: Ben Voigt | last post by:
Under certain circumstances I get: First-chance exception at 0x7c812a5b (kernel32.dll) in LTMGUI.exe: 0xC0020001: The string binding is invalid. First-chance exception at 0x7c812a5b (kernel32.dll)...
6
by: Jon Bilbao | last post by:
I´m trying a select clause in two steps because it´s too complex. First: SELECT Reference, Results.idEnsayo, Results.Num_taladro, min(Results.dTime) + 500 AS tIni, max(Results.dTime) - 500 AS...
8
by: wizardRahl | last post by:
Hi, I'm trying to display my query results onto an Access subform. I have written a very simple SQL statement to preform a query depending on a user's choice of a drop down box in an attempt to...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.