select max

I have following query
SELECT ppa.project_type,ppa.segment1 project_no, ppa.project_id, pt.task_id,pt.WBS_LEVEL,pt.READY_TO_DISTRIBUTE_FLAG,pt.RECORD_VERSION_NUMBER,
            tASK_NUMBER,
             pt.task_name,
             pt.billable_flag,
             pt.chargeable_flag,
             pt.PARENT_TASK_ID
        FROM apps.pa_tasks pt, apps.pa_projects_all ppa
       WHERE     ppa.project_id = pt.project_id
            AND ppa.segment1 IN ('172018')
           -- and pt.READY_TO_DISTRIBUTE_FLAG = 'N'
    AND pt.WBS_LEVEL in (select max(to_number(b.wbs_level)) from pa_tasks b where pt.task_id=b.task_id)

I want to select the max of wbs_level but the query is not working
pardeshirahulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pardeshirahulAuthor Commented:
if wbs_level is 1 2 and 3 then select 2 and 3
0
pardeshirahulAuthor Commented:
if it is 1 only select 1
0
PortletPaulfreelancerCommented:
The following snippet  is the equivalent of wbs_level = wbs_level
FROM apps.pa_tasks pt
...
WHERE ...
AND pt.WBS_LEVEL IN (
      SELECT
            MAX(to_number(b.wbs_level))
      FROM pa_tasks b
      WHERE pt.task_id = b.task_id
)

Open in new window

(when pt.task_id = b.task_id there is a single wbs_level value in pt that is exactly the same single wbs_level value in b)

"I want to select the max of wbs_level"
This may be very difficult, or it might be easy, the problem is we know nothing about your data, so it is very hard to offer a suggestion. However the following might help. The logic is to calculate the maximum wbs_level in each project, then only return that level of the WBS (per project)
SELECT
      *
FROM (

      SELECT
            ppa.project_type
          , ppa.segment1        project_no
          , ppa.project_id
          , pt.task_id
          , pt.WBS_LEVEL
          , pt.READY_TO_DISTRIBUTE_FLAG
          , pt.RECORD_VERSION_NUMBER
          , tASK_NUMBER
          , pt.task_name
          , pt.billable_flag
          , pt.chargeable_flag
          , pt.PARENT_TASK_ID
          , MAX(to_number(pt.wbs_level)) OVER (PARTITION BY pt.project_id) maxwbs_lvl
      FROM apps.pa_tasks pt
      INNER JOIN apps.pa_projects_all ppa ON ppa.project_id = pt.project_id
      WHERE ppa.segment1 IN ('172018')
-- and pt.READY_TO_DISTRIBUTE_FLAG = 'N'
) d
WHERE wbs_level = maxwbs_lvl

Open in new window

If thi does not help can you provide some samples of the data in your task table (scrub any private stuff)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Anthony ParraCommented:
Try this:

SELECT ppa.project_type,ppa.segment1 project_no, ppa.project_id, pt.task_id,pt.WBS_LEVEL,pt.READY_TO_DISTRIBUTE_FLAG,pt.RECORD_VERSION_NUMBER,
            tASK_NUMBER,
             pt.task_name,
             pt.billable_flag,
             pt.chargeable_flag,
             pt.PARENT_TASK_ID
        FROM apps.pa_tasks pt, apps.pa_projects_all ppa
       WHERE     ppa.project_id = pt.project_id
            AND ppa.segment1 IN ('172018')
           -- and pt.READY_TO_DISTRIBUTE_FLAG = 'N'
    AND pt.WBS_LEVEL in (select max(to_number(b.wbs_level)) from pa_tasks b)
0
slightwv (䄆 Netminder) Commented:
What you have asked can mean many different things.  You might also be able to get what you want with RANK, DENSE_RANK or ROW_NUMBER.

As Paul as asked:
Please post some simple sample data and expected results.
0
Pawan KumarDatabase ExpertCommented:
Please try like this .

/*First get the maximum level for each task_id  and  then join it with the other query. */
SELECT MAX(to_number(b.wbs_level)) lvl , task_id  FROM pa_tasks b  GROUP BY task_id

Please use the updated code given below and let us know in case of any issues.

SELECT
  ppa.project_type,
  ppa.segment1 project_no,
  ppa.project_id,
  pt.task_id,
  pt.WBS_LEVEL,
  pt.READY_TO_DISTRIBUTE_FLAG,
  pt.RECORD_VERSION_NUMBER,
  tASK_NUMBER,
  pt.task_name,
  pt.billable_flag,
  pt.chargeable_flag,
  pt.PARENT_TASK_ID
FROM 
apps.pa_tasks pt
INNER JOIN apps.pa_projects_all ppa ON ppa.project_id = pt.project_id
INNER JOIN (SELECT MAX(to_number(b.wbs_level)) lvl , task_id 
  FROM pa_tasks b
  GROUP BY task_id)y ON y.task_id = pt.task_id AND y.lvl = pt.WBS_LEVEL
WHERE ppa.segment1 = ('172018')

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Commented:
SELECT
  ppa.project_type,
  ppa.segment1 project_no,
  ppa.project_id,
  pt.task_id,
  pt.WBS_LEVEL,
  pt.READY_TO_DISTRIBUTE_FLAG,
  pt.RECORD_VERSION_NUMBER,
  tASK_NUMBER,
  pt.task_name,
  pt.billable_flag,
  pt.chargeable_flag,
  pt.PARENT_TASK_ID
FROM
apps.pa_tasks pt
INNER JOIN apps.pa_projects_all ppa ON ppa.project_id = pt.project_id
INNER JOIN (SELECT wbs_level, task_id ,
row_number() over (order by task_id order by wbs_level DESC) rown
  FROM pa_tasks) x
ON x.task_id = pt.task_id
WHERE ppa.segment1 = ('172018')
AND x.rown = 1;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

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.