Oracle, multiple table query problems

I AM HAVING PROBLEMS TRYING TO RELATE THREE TABLES AND RETURN REQUIRED RESULTS:

TABLES WITH KEYCOLUMNS:

ORDER
---ORDER.ORDERNUM

ORDERLINES
---ORDERLINES.ORDERNUM
---ORDERLINES.WORKNUMBER

WORK
--WORK.WORKNUMBER
--WORK.WORKAVAILABLE


1 - ORDER HAS MANY ORDERLINES

2 - ORDERLINES CAN HAVE A WORKNUMBER BUT CAN ALSO HAVE NULL

3 - WORK IS ONLY ASSOCIATED TO INDIVIDUAL ORDERLINES - THE WORKNUMBER CAN SHOW UP IN MANY ORDERLINES AND VARYING ASSOCIATED ORDERLINE.ORDERNUMs

SETUP:
WOULD LIKE TO RETURN EACH ORDER AS IT LOOKS AT EACH ORDERLINE FOR WORKNUMBER AND MEETS CRITERIA BASED ON THE 3 SCENARIOS BELOW


THE FOLLOWING START QUERY RETURNS A RECORD FOR EACH ORDER AND SHOWS THE SUM OF ORDERLINES ALONG WITH A NULL IN ANOTHER COLUMN (WORKAVAILABLE).  THIS COLUMN SHOULD BE REFLECTIVE OF THE RELATIONSHIP DEFINED WITHIN THE 3 SCENARIOS BELOW WITHIN THE GOAL SECTION.  THIS IS GOOD BUT DOES NOT HAVE THE RELATIONSHIP TO WORK TABLE.

SELECT ORDER.ORDERNUM, SUM(ORDERLINES.COST) AS COST, NULL AS WORKAVAILABLE
FROM ORDER, ORDERLINES
WHERE ORDER.ORDERNUM = ORDERLINES.ORDERNUM
GROUP BY ORDER.ORDERNUM

GOAL:
I AM TRYING TO ALSO LOOK AT THE WORK TABLE BASED ON WORKNUMBER WITHIN THE ORDERLINES TABLE AND RETURN THE FOLLOWING RESULTS BASED ON THE FOLLOWING CRITERIA:

1 - IF THE ORDERLINES DO NOT HAVE ANY WORKNUMBERS I WOULD LIKE TO SHOW WORKAVAILABLE AS 0 SINCE NO WORK WAS FOUND

2 - IF THE ORDERLINES HAVE SOME WITH WORKNUMBERS AND ALL ASSOCIATED WORK.WORKAVAILABLE (WORK TABLE) SHOWS 0 I WOULD LIKE TO SHOW WORKAVAILABLE AS 0 SINCE WORK WAS FOUND BUT ALL SHOWED WORK.WORKAVAILABLE AS 0

2 - IF THE ORDERLINES HAVE SOME WITH WORKNUMBERS AND AT LEAST ONE ASSOCIATED WORK.WORKAVAILABLE (WORK TABLE) SHOWS 1 I WOULD LIKE TO SHOW WORKAVAILABLE AS 1 SINCE AT LEAST ONE REACORD SHOWS WORK.WORKAVAILABLE AS 1

Thanks
vandy02Asked:
Who is Participating?
 
lwadwellCommented:
To join in to your query, keeping in mind that to join in the WORK table to your original query would alter the outcome of the SUM(), my query need to be used as an in-line view - try:

SELECT ORDER.ORDERNUM, SUM(ORDERLINES.COST) AS COST, MAX(WORKAVAILABLE) as WORKAVAILABLE
FROM ORDER
JOIN ORDERLINES
ON ORDER.ORDERNUM = ORDERLINES.ORDERNUM
JOIN (SELECT ORDERLINES.ORDERNUM, MAX(NVL(WORK.WORKAVAILABLE,0)) as WORKAVAILABLE
      FROM ORDERLINES
      LEFT JOIN WORK ON ORDERLINES.WORKNUMBER = WORK.WORKNUMBER
      GROUP BY ORDERLINES.ORDERNUM) v
ON ORDERLINES.ORDERNUM = v.ORDERNUM
GROUP BY ORDER.ORDERNUM

Open in new window

or, to simplify the joins
SELECT ORDER.ORDERNUM, SUM(v.COST) AS COST, MAX(WORKAVAILABLE) as WORKAVAILABLE
FROM ORDER
JOIN (SELECT ORDERLINES.ORDERNUM, ORDERLINES.COST, MAX(NVL(WORK.WORKAVAILABLE,0)) as WORKAVAILABLE
      FROM ORDERLINES
      LEFT JOIN WORK ON ORDERLINES.WORKNUMBER = WORK.WORKNUMBER
      GROUP BY ORDERLINES.ORDERNUM, ORDERLINES.COST) v
ON ORDER.ORDERNUM = v.ORDERNUM
GROUP BY ORDER.ORDERNUM

Open in new window

0
 
lwadwellCommented:
something like:
SELECT ORDERLINES.ORDERNUM, MAX(NVL(WORK.WORKAVAILABLE,0))
FROM ORDERLINES
LEFT JOIN WORK ON ORDERLINES.WORKNUMBER = WORK.WORKNUMBER
GROUP BY ORDERLINES.ORDERNUM

Open in new window

0
 
vandy02Author Commented:
Unfortunately I am thinking this will not work  because there also other columns I will need to show from the orders table. For example, I will have a status field from orders.

Thanks
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
lwadwellCommented:
My query is not joined to the ORDER table.  Your 3 goals didn't state that was needed ... I assumed you would know how to do that.
0
 
vandy02Author Commented:
I keep getting an invalid argument when I have this within the select.

MAX(NVL(WORK.WORKAVAILABLE,0)) as WORKAVAILABLE

Thanks,
0
 
vandy02Author Commented:
Sorry...invalid number of arguments
0
 
lwadwellCommented:
Just try without the NVL() ...
  ie. MAX(WORK.WORKAVAILABLE) as WORKAVAILABLE
but that may result in nulls - which I was using the NVL to avoid.
The other option is to reverse them
  i.e.NVL(MAX(WORK.WORKAVAILABLE),0) as WORKAVAILABLE
0
 
vandy02Author Commented:
I am testing the previous now....I made a couple changes to it and I think it worked.  I had the last 'ON' as ORDERLINE instead ORDER.  

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.