Home » SQL & PL/SQL » SQL & PL/SQL » Issue getting all data using WITH clause
Issue getting all data using WITH clause [message #622912] Mon, 01 September 2014 11:50 Go to next message
carswelljr
Messages: 7
Registered: March 2014
Junior Member
Good morning,
I have a query where I am trying to get results from many tables based on conditions from 2 values within one table. The values use the same field name (section_code). I cannot retrieve the values from the OGSMONTH section I get the error:
ORA-00904: "OGS"."Section_Code": invalid identifier

If I comment out all the OGS columns in the final select statement I can get the values for ADV but the section_code pulls in the value for only ADV and puts it in both section_code alias columns "ASSESSMENT" and "OGS MONTH". What am I doing wrong?

DATE         ASSESSMENT         ARCS ID      PHYSICIAN NAME        PHY ID          OGS MONTH
24-JUL-12	 ADVERSE EVENT    1380532      MURRAY COHEN	515494    ADVERSE EVENT


See code below:

with adverse as (SELECT das.section_code, 
DSE.TM_IDENT_CODE, 
DSE.THERAPY_TYPE, 
secview.section_code "Assessment"
          
 FROM ADM.DM_SESSIONS DSE, 
 ADM.DM_ADMIN_SECTIONS_V secview, 
 ADM.DM_SESSION_ASSESSMENTS DSA, 
 ADM.DM_ADMIN_CATEGORIES DAC, 
 ADM.DM_SESSION_ASSESSMENT_ITEMS DSAI, 
 ADM.DM_ADMIN_SECTIONS DAS, 
 ADM.DM_ADMIN_ITEMS DAI
      
 WHERE DSA.CATEGORY_ID = DAC.CATEGORY_ID
   AND DAC.CATEGORY_CODE = 'FORTEO OGS'
   AND DAC.CATEGORY_ID = DAS.CATEGORY_ID
   and secview.section_id=DAS.Section_id
   AND secview.section_code ='ADVERSE EVENT'
   AND DAS.SECTION_ID = DAI.SECTION_ID
   AND DAI.STOP_DATE IS NULL 
   AND DSA.SESSION_ASSESSMENT_ID = DSAI.SESSION_ASSESSMENT_ID
   AND DAI.ITEM_ID = DSAI.ITEM_ID
   AND DSAI.EXCLUDED = 'N'
   AND DSE.THERAPY_TYPE IN ('FORT','FORA')
   AND DSE.TM_IDENT_CODE BETWEEN TO_DATE('08/06/2010 00:00:00','MM/DD/YYYY HH24:MI:SS') AND (TRUNC(SYSDATE) - 1/86400)
   AND DSE.EXCLUDED = 'N' 
        ),
        
        
--below gets the section_code for OGS Month        
OGSMONTH AS (SELECT 
       
       DSE.TM_IDENT_CODE "DATE",
       DSE.PATIENT_ID "ARCS ID",
       DOC.FIRST ||' '|| DOC.LAST  "PHYSICIAN NAME",
       PHY.physician_id,
       DAS.SECTION_CODE "OGS MONTH"
      
 FROM ADM.DM_SESSIONS DSE
 INNER JOIN THOT.PATIENTS_TABLE PTA
         ON DSE.PATIENT_ID = PTA.ID
 INNER JOIN ADM.DM_SESSION_ASSESSMENTS DSA
         ON DSE.SESSION_ID = DSA.SESSION_ID
 INNER JOIN ADM.DM_ADMIN_CATEGORIES DAC
         ON DSA.CATEGORY_ID = DAC.CATEGORY_ID
        AND DAC.CATEGORY_CODE = 'FORTEO OGS'
 INNER JOIN ADM.DM_ADMIN_SECTIONS DAS
         ON DAC.CATEGORY_ID = DAS.CATEGORY_ID
 INNER JOIN THOT.PATIENT_PHYSICIAN PHY
         on pta.id=phy.patient_id
 INNER JOIN THOT.PHYSICIANS DOC
        on phy.physician_id=DOC.ID 
 INNER JOIN ADM.DM_ADMIN_ITEMS DAI
         ON DAS.SECTION_ID = DAI.SECTION_ID
        AND DAI.STOP_DATE IS NULL
        AND (
              (DAS.SECTION_CODE = 'NURSE WELCOME CALL' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = 'NURSE WELCOME CALL ATTEMPT' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = 'NURSE WELCOME CALL NEEDED' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '0 MONTH OGS' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR      
              (DAS.SECTION_CODE = '1 MONTH OGS' AND DAI.ITEM_CODE = 'CALLDIR') OR
              (DAS.SECTION_CODE = '1 MONTH OGS ATTEMPT' AND DAI.ITEM_CODE = 'CALLDIR') OR
              (DAS.SECTION_CODE = '2 MONTH OGS' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '2 MONTH OGS ATTEMPT' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '4 MONTH OGS' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '4 MONTH OGS ATTEMPT' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '6 MONTH OGS' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '6 MONTH OGS ATTEMPT' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '9 MONTH OGS' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '9 MONTH OGS ATTEMPT' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '12 MONTH OGS' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '12 MONTH OGS ATTEMPT' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '15 MONTH OGS' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '15 MONTH OGS ATTEMPT' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '17 MONTH OGS' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '17 MONTH OGS ATTEMPT' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '20 MONTH OGS' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '20 MONTH OGS ATTEMPT' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '24 MONTH OGS' AND DAI.ITEM_CODE = 'CALL_DIRECTION') OR
              (DAS.SECTION_CODE = '24 MONTH OGS ATTEMPT' AND DAI.ITEM_CODE = 'CALL_DIRECTION')
             
                          )
 INNER JOIN ADM.DM_SESSION_ASSESSMENT_ITEMS DSAI
        ON DSA.SESSION_ASSESSMENT_ID = DSAI.SESSION_ASSESSMENT_ID
        AND DAI.ITEM_ID = DSAI.ITEM_ID
        AND DSAI.EXCLUDED = 'N'
AND     DAI.item_code = 'CALL_DIRECTION' 
AND     DSAI.excluded = 'N'
AND     DAI.section_id = dsa.section_id 
AND     DAI.item_code = 'CALLDIR' 

 WHERE DSE.THERAPY_TYPE IN ('FORT','FORA')
   AND DSE.TM_IDENT_CODE BETWEEN TO_DATE('08/06/2000 00:00:00','MM/DD/YYYY HH24:MI:SS') AND (TRUNC(SYSDATE) - 1/86400)
   AND DSE.EXCLUDED = 'N' )
   
   
   SELECT 
   OGS.section_code "OGS MONTH", 
  -- ADV.section_code "ASSESSMENT",
   ADV.TM_IDENT_CODE, 
   ADV.THERAPY_TYPE,
   OGS.PATIENT_ID "ARCS ID",
   OGS.FIRST ||' '|| ogs.LAST  "PHYSICIAN NAME",
   OGS.physician_id,
   OGS.SECTION_CODE "OGS MONTH"
       
  FROM adverse ADV, ogsmonth OGS
 
--GROUP BY OGS.TM_IDENT_CODE, OGS.PATIENT_ID, OGS.FIRST ||' '|| OGS.LAST, OGS.physician_id, OGS.SECTION_CODE, ADV.SECTION_CODE        



Lalit : Added code tags as and where required

[Updated on: Mon, 01 September 2014 12:05] by Moderator

Report message to a moderator

Re: Issue getting all data using WITH clause [message #622914 is a reply to message #622912] Mon, 01 September 2014 12:10 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
It was already told to you :

BlackSwan wrote on Sat, 22 March 2014 01:33
welcome to this forum.


Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and please read http://www.orafaq.com/forum/t/174502/102589/


Michel Cadot wrote on Sat, 22 March 2014 12:11

Please, post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.




And you didn't even feedback to your previous post.
Re: Issue getting all data using WITH clause [message #622916 is a reply to message #622914] Mon, 01 September 2014 12:29 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
use the column alias as shown below
WITH calc AS
  (SELECT deptno,
          max(sal) max_sal
   FROM emp
   GROUP BY deptno)
SELECT avg(max_sal)
FROM calc;


I suggest that you avoid using double quote marks, since they often cause more problems than they solve.
Re: Issue getting all data using WITH clause [message #622921 is a reply to message #622916] Mon, 01 September 2014 15:23 Go to previous messageGo to next message
carswelljr
Messages: 7
Registered: March 2014
Junior Member
Thank you for the reply.
I removed the double quotes for the alias'. I now get the below error if I take out the quotes and make the commented columns active:

ORA-00923: FROM keyword not found where expected
00923. 00000 - "FROM keyword not found where expected"
*Cause:
*Action:
Error at Line: 33 Column: 24

--its this line: DOC.FIRST ||' '|| DOC.LAST PHYSICIAN NAME,
--I can get the code to work if I comment out some of the columns but it returns no data
I am creating the tables with some values now so you can look at exactly what I am doing if you do not mind.
Re: Issue getting all data using WITH clause [message #622922 is a reply to message #622921] Mon, 01 September 2014 15:44 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
I can not debug code I can not see.

column alias must be a single string ( can not contain any space character)
Re: Issue getting all data using WITH clause [message #622923 is a reply to message #622922] Tue, 02 September 2014 00:13 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
In your first message, you have this:
...
     OGSMONTH
        AS (SELECT ...
                   DAS.SECTION_CODE "OGS MONTH"  --> avoid double quotes
              FROM ADM.DM_SESSIONS DSE
              ...
           )
SELECT OGS.section_code "OGS MONTH",             --> use columns you have, not the ones you think you have
  ...
  FROM adverse ADV, ogsmonth OGS

As you've been told, avoid double quotes. Yes, they make labels somewhat prettier, but also make problems later. There's no much difference if you call a column OGS MONTH or OGS_MONTH. Underscore is usually accepted as a delimiter and makes names quite readable.

Furthermore, as OGSMONTH now contains a column whose name is (as far as the next SELECT is concerned) "OGS MONTH", you can't
SELECT OGS.SECTION_CODE
You *must* select columns you have in disposal, and in the OGSMONTH, it is "OGS MONTH", not SECTION_CODE.

So: try to rewrite that code as follows (apply the same principle to all other columns as well):
...
     OGSMONTH
        AS (SELECT ...
                   DAS.SECTION_CODE OGS_MONTH    --> remove double quotes, use underscore instead
              FROM ADM.DM_SESSIONS DSE
              ...
           )
SELECT OGS.OGS_MONTH,                            --> refer to OGS_MONTH instead of SECTION_CODE (which doesn't exist in OGSMONTH table)
  ...
  FROM adverse ADV, ogsmonth OGS

[Updated on: Tue, 02 September 2014 00:14]

Report message to a moderator

Re: Issue getting all data using WITH clause [message #622969 is a reply to message #622923] Tue, 02 September 2014 14:17 Go to previous messageGo to next message
carswelljr
Messages: 7
Registered: March 2014
Junior Member
Thank you both, this actually help and worked for me (removing the quotes for the alias and referencing the alias in the final select statement) However the issue I am having is I cannot put data. Is there something I need to do in order to pull 2 different values from the same columns and display them in the output? My logic may be whats not getting me the results I need.

example: Cats, White, Brown, Black are values in table animals, under column pets. I have an owners table that holds the value for animal_id. I want the final output to display like below, however Pet and Color(Alias) both pull from same Animal_ID field. This is my issue with my code.

Animal_ID Pet Color Owner_ID Owner_name Purchased_Date
01 Cats White aa1 Bill Smith 08/01/2013
02 Cats White bb2 Jan White 08/22/2014
03 Cats Brown cc3 Jill Brown 09/01/2014


CREATE TABLE animals
(
animal_id NUMBER(6),
pets VARCHAR2(20)
);


CREATE TABLE owner
(
owner_id NUMBER(6),
owner_name VARCHAR2(20),
animal_id NUMBER(6),
purchased_date DATE DEFAULT SYSDATE
);
Re: Issue getting all data using WITH clause [message #622970 is a reply to message #622969] Tue, 02 September 2014 14:21 Go to previous messageGo to next message
carswelljr
Messages: 7
Registered: March 2014
Junior Member
Animal_ID Pet Color Owner_ID Owner_name Purchased_Date
01 Cats White aa1 Bill Smith 08/01/2013
02 Cats White bb2 Jan White 08/22/2014 
03 Cats Brown cc3 Jill Brown 09/01/2014


CREATE TABLE animals 
( 
animal_id NUMBER(6), 
pets VARCHAR2(20) 
); 


CREATE TABLE owner 
( 
owner_id NUMBER(6), 
owner_name VARCHAR2(20), 
animal_id NUMBER(6), 
purchased_date DATE DEFAULT SYSDATE 
); 

[Updated on: Tue, 02 September 2014 14:22]

Report message to a moderator

Re: Issue getting all data using WITH clause [message #622971 is a reply to message #622970] Tue, 02 September 2014 14:37 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
SELECT aa.animal_id ,
       aa.pets,
       oo.owner_id ,
       oo.owner_name ,
       oo.purchased_date
FROM ANIMALS aa,
     OWNER oo
WHERE aa.animal_id = oo.animal_id;


[Updated on: Tue, 02 September 2014 14:38]

Report message to a moderator

Re: Issue getting all data using WITH clause [message #622972 is a reply to message #622971] Tue, 02 September 2014 14:57 Go to previous messageGo to next message
carswelljr
Messages: 7
Registered: March 2014
Junior Member
Ok. but what about when the Aminal_ID are different.
cats animal_id is 01, but white animal_id is 11, brown animal_id is 22.
This is how my database looks. the values are in the same column, have different IDs but I need them to be referenced and displayed on the same line when say an owner has a white cat, brown cat, etc...
Re: Issue getting all data using WITH clause [message #622973 is a reply to message #622972] Tue, 02 September 2014 15:06 Go to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/


If you expect to get useful answers from here you need to follow Posting Guideline & post test case data

>the values are in the same column, have different IDs
Then how in the world is anyone to know which animal is related to which owner?
Previous Topic: Calender from 1-Jan-0001 Till 31-Dec-9999
Next Topic: Oracle sql query to pick current and previous records
Goto Forum:
  


Current Time: Wed Apr 24 03:30:35 CDT 2024