Solved

Oracle SQL Sub Query

Posted on 2016-08-15
5
45 Views
Last Modified: 2016-08-15
I have a query that I am running, code below, which is not giving the desired output. I have changed the joins etc to try to get the desired result with no success.

With my limited knowledge, I have come to the conclusion that a sub query may be required but am unsure how to structure it.

The result that I am looking for is all contracts that are 'ACTIVE' in C_ID'3' and the latest revision Y, no problem. The views are joined on the only option of customer account code.

The issue arises when I join the consultants view and need to retrieve the consultant details where the AC.EA_ACCOUNT_TYPE = 'C' for customer, the only other option is 'P' for prospect. In this view it is possible that  there will be some customers may have two entries, one as a 'C' and one as a 'P' I just need to retrieve the 'C's linked by account code.  

I hope that I have explained this well enough for you all to decipher! if not please ask.

Many thanks in advance,

Keith


SELECT C.REGISTRATION_NUMBER,
       C.ACCOUNT_CODE,
       C.NAME,
       AC.CLIENT_SUPPORT,
       AC.SALESPERSON,
       AC.ACCOUNT_MANAGER
  FROM STD_BI.RL2_CONTRACTS_VW C
       LEFT JOIN STD_BI.RL_ACCOUNT_CONSULTANTS_VW AC
          ON (AC.EA_ACCOUNT_CODE = C.ACCOUNT_CODE)
 WHERE     ( (C.CONTRACT_STATUS = 'ACTIVE') AND (C.C_ID = 3))
       AND (C.LATEST_REVISION = 'Y') AND (AC.EA_ACCOUNT_TYPE = 'C')
 GROUP BY C.REGISTRATION_NUMBER,
       C.ACCOUNT_CODE,
       C.NAME,
       AC.CLIENT_SUPPORT,
       AC.SALESPERSON,
       AC.ACCOUNT_MANAGER
0
Comment
Question by:kalees
  • 2
  • 2
5 Comments
 
LVL 34

Expert Comment

by:johnsone
ID: 41756183
I'm not sure I completely understand what you are asking, but it sounds like you are getting rows where there isn't a 'C' in account type.  If that is the case, my guess is that you don't want the left join.
SELECT C.registration_number, 
       C.account_code, 
       C.NAME, 
       AC.client_support, 
       AC.salesperson, 
       AC.account_manager 
FROM   std_bi.rl2_contracts_vw C 
       JOIN std_bi.rl_account_consultants_vw AC 
         ON ( AC.ea_account_code = C.account_code ) 
WHERE  C.contract_status = 'ACTIVE' 
       AND C.c_id = 3 
       AND C.latest_revision = 'Y' 
       AND AC.ea_account_type = 'C' 
GROUP  BY C.registration_number, 
          C.account_code, 
          C.NAME, 
          AC.client_support, 
          AC.salesperson, 
          AC.account_manager 

Open in new window

There is no need for the parenthesis in your where clause.  They way they were structured it could prevent usage of an index.  It probably didn't, but it is possible.  They are all the same operator, AND, so you don't need them.
0
 
LVL 22

Expert Comment

by:Helena Marková
ID: 41756185
In a brief I think that your select is OK. Why do you think it is not ?
0
 

Author Comment

by:kalees
ID: 41756237
Hi, many thanks for the responses, I maybe missed a little in the explanation.

There may not be a matching record in the consultants view so I am expecting to see some blanks for consultants in the returned data and there is not.

I have just done a test commenting out anything to do with consultants and have the correct number of records returned proving that the consultants is restricting the data set. This is why I was thinking about sub query, but as I said, this is somewhat out of my knowledge range!

Keith
0
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
ID: 41756287
Then this may be what you are looking for:
SELECT C.registration_number, 
       C.account_code, 
       C.name, 
       AC.client_support, 
       AC.salesperson, 
       AC.account_manager 
FROM   std_bi.rl2_contracts_vw C 
       left join std_bi.rl_account_consultants_vw AC 
              ON ( AC.ea_account_code = C.account_code 
                   AND AC.ea_account_type = 'C' ) 
WHERE  C.contract_status = 'ACTIVE' 
       AND C.c_id = 3 
       AND C.latest_revision = 'Y' 
GROUP  BY C.registration_number, 
          C.account_code, 
          C.name, 
          AC.client_support, 
          AC.salesperson, 
          AC.account_manager 

Open in new window

If that isn't it, please provide sample data and expected results.
1
 

Author Closing Comment

by:kalees
ID: 41756296
Perfect!

Many thanks Johnsone. I just need to understand having a where clause in the join now
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

758 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

19 Experts available now in Live!

Get 1:1 Help Now