Solved

Help with correlated sub query in select list

Posted on 2006-07-21
7
390 Views
Last Modified: 2012-08-13
I am struggling to make this query below work and am looking for help.

The query is running against a Pervasive 2000i database.

SELECT
  B.BKIC_PROD_CODE Part_Num,
  B.BKIC_PROD_DESC Description,
  W.MTWO_WIP_COMQTY COMQTY,
  W.MTWO_WIP_SQTY SQTY,
  W.MTWO_WIP_COMQTY / W.MTWO_WIP_SQTY YIELD,
  W.MTWO_WIP_ELABOR ELABOR,
  W.MTWO_WIP_ALABOR ALABOR, W.MTWO_WIP_ALABOR / W.MTWO_WIP_SQTY LAB_Per_Pc,
  (SELECT SUM(WO.MTWORO_ESTHRS)
    FROM
    WORKOUTOLD WO
    WHERE WO.MTWORO_WOPRE = W.MTWO_WIP_WOPRE AND WO.MTWORO_WOSUF = W.MTWO_WIP_WOSUF),
  W.MTWO_WIP_AFIN AFINISH,
  W.MTWO_WIP_WOPRE WOPRE
FROM  
  BKICMSTR B INNER JOIN WORKORD W ON B.BKIC_PROD_CODE=W.MTWO_WIP_CODE
WHERE
  W.MTWO_WIP_STATUS='C' AND
  W.MTWO_WIP_AFIN>='2003-03-01' AND
  W.MTWO_WIP_AFIN<='2006-03-29' AND
  B.BKIC_PROD_CODE LIKE '10275%'
ORDER BY
  B.BKIC_PROD_CLASS,
  B.BKIC_PROD_CODE

The  query runs fine when I remove the subquery in the select list.  

Any thoughts as to what I have wrong?
0
Comment
Question by:pdbernier
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
>The  query runs fine when I remove the subquery in the select list.  
what's the error message, resp problem result?
0
 

Author Comment

by:pdbernier
Comment Utility
The message I get from the Pervasive SQL Editor is

Some error(s) encountered while executing SQL statement(script).

Not very helpful.
0
 
LVL 7

Expert Comment

by:FDzjuba
Comment Utility
you can't have SELECT statement in the select vaules


use the following.

SELECT
  B.BKIC_PROD_CODE Part_Num,
  B.BKIC_PROD_DESC Description,
  W.MTWO_WIP_COMQTY COMQTY,
  W.MTWO_WIP_SQTY SQTY,
  W.MTWO_WIP_COMQTY / W.MTWO_WIP_SQTY YIELD,
  W.MTWO_WIP_ELABOR ELABOR,
  W.MTWO_WIP_ALABOR ALABOR, W.MTWO_WIP_ALABOR / W.MTWO_WIP_SQTY LAB_Per_Pc,
  SUM(WO.MTWORO_ESTHRS)
    FROM
    WORKOUTOLD WO
    WHERE WO.MTWORO_WOPRE = W.MTWO_WIP_WOPRE AND WO.MTWORO_WOSUF = W.MTWO_WIP_WOSUF),
  W.MTWO_WIP_AFIN AFINISH,
  W.MTWO_WIP_WOPRE WOPRE
FROM  
  BKICMSTR B INNER JOIN WORKORD W ON B.BKIC_PROD_CODE=W.MTWO_WIP_CODE
WHERE
  W.MTWO_WIP_STATUS='C' AND
  W.MTWO_WIP_AFIN>='2003-03-01' AND
  W.MTWO_WIP_AFIN<='2006-03-29' AND
  B.BKIC_PROD_CODE LIKE '10275%'
ORDER BY
  B.BKIC_PROD_CLASS,
  B.BKIC_PROD_CODE
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 19

Expert Comment

by:folderol
Comment Utility
you can re-write it to

SELECT
  B.BKIC_PROD_CODE Part_Num,
  B.BKIC_PROD_DESC Description,
  W.MTWO_WIP_COMQTY COMQTY,
  W.MTWO_WIP_SQTY SQTY,
  W.MTWO_WIP_COMQTY / W.MTWO_WIP_SQTY YIELD,
  W.MTWO_WIP_ELABOR ELABOR,
  W.MTWO_WIP_ALABOR ALABOR, W.MTWO_WIP_ALABOR / W.MTWO_WIP_SQTY LAB_Per_Pc,
  WO.tot_estimated_hours,
  W.MTWO_WIP_AFIN AFINISH,
  W.MTWO_WIP_WOPRE WOPRE
FROM  
  BKICMSTR B INNER JOIN WORKORD W ON B.BKIC_PROD_CODE=W.MTWO_WIP_CODE
left outer join
(
   SELECT SUM(MTWORO_ESTHRS) as tot_estimated_hours, MTWORO_WOPRE, MTWORO_WOSUF
   FROM WORKOUTOLD
   group by MTWORO_WOPRE, MTWORO_WOSUF
) as WO on
WO.MTWORO_WOPRE = W.MTWO_WIP_WOPRE AND WO.MTWORO_WOSUF = W.MTWO_WIP_WOSUF
WHERE
  W.MTWO_WIP_STATUS='C' AND
  W.MTWO_WIP_AFIN>='2003-03-01' AND
  W.MTWO_WIP_AFIN<='2006-03-29' AND
  B.BKIC_PROD_CODE LIKE '10275%'
ORDER BY
  B.BKIC_PROD_CLASS,
  B.BKIC_PROD_CODE


OR, you could just try assigning a column name to the subquery, which your editor may require, your query looks like it would run in MS SQL Query Analyser.  I don't know the datatype of MTWORO_ESTHRS but I assume you tested the sub-query separately and it is bringing back the correct summed value.

'tot_estimated_hours' = (SELECT SUM(WO.MTWORO_ESTHRS)
    FROM
    WORKOUTOLD WO
    WHERE WO.MTWORO_WOPRE = W.MTWO_WIP_WOPRE AND WO.MTWORO_WOSUF = W.MTWO_WIP_WOSUF),


Tom
0
 

Author Comment

by:pdbernier
Comment Utility
Tom,

I've tried both of your suggestions, but still no luck.  I am wondering if Pervasive 2000i cannot handle a subquery like this.
0
 
LVL 19

Accepted Solution

by:
folderol earned 500 total points
Comment Utility
Can't help you there, I only use MS products.  We already know the the query runs without the sub query. Will this query run as well?

   SELECT SUM(MTWORO_ESTHRS) as tot_estimated_hours, MTWORO_WOPRE, MTWORO_WOSUF
   FROM WORKOUTOLD
   group by MTWORO_WOPRE, MTWORO_WOSUF

If it does, I am out of ideas.  The query as a whole looks pretty conventional so it should be acceptable.

Tom
0
 

Author Comment

by:pdbernier
Comment Utility
That query does run, but the entire query does not.

Thanks for the help.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now