• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

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
0
vandy02
Asked:
vandy02
  • 4
  • 4
1 Solution
 
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
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now