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 |
|
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 #622923 is a reply to message #622922] |
Tue, 02 September 2014 00:13 |
|
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 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 |
|
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 |
|
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
|
|
|
|
|
|
Goto Forum:
Current Time: Wed Apr 24 03:30:35 CDT 2024
|