Have multiple criteria and I need to have results appear on one line

I need to run a query in Teradata where I'm pulling claims with a range of Codes (99201 thru 99216), but also need to pull claims with Codes (10000 thru 69999), but I need the results to appear on the same line.

How can I accomplish this?  Can I do a union query using the same table twice?

Here is the query I'm currently using, but getting the error "SELECT failed, 936: ORA-00936: missing expression

select
FACETS.CMC_CLCL_CLAIM.CLCL_ID,
FACETS.CMC_CDML_CL_LINE.CDML_SEQ_NO,
FACETS.CMC_CDML_CL_LINE.IPCD_ID,
FROM           
FACETS.CMC_CLCL_CLAIM
LEFT OUTER JOIN
FACETS.CMC_CDML_CL_LINE ON FACETS.CMC_CLCL_CLAIM.CLCL_ID = FACETS.CMC_CDML_CL_LINE.CLCL_ID
where FACETS.CMC_CLCL_CLAIM.CLCL_ID = '123450565300'
and FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '99201' AND '99216'
UNION
select
FACETS.CMC_CLCL_CLAIM.CLCL_ID,
FACETS.CMC_CDML_CL_LINE.CDML_SEQ_NO,
FACETS.CMC_CDML_CL_LINE.IPCD_ID,
FROM           
FACETS.CMC_CLCL_CLAIM s
LEFT OUTER JOIN
FACETS.CMC_CDML_CL_LINE ON FACETS.CMC_CLCL_CLAIM.CLCL_ID = FACETS.CMC_CDML_CL_LINE.CLCL_ID
where FACETS.CMC_CLCL_CLAIM.CLCL_ID = '123450565300'
and  s.FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '10000' AND '69999'

Open in new window


Thanks,

gdunn59
LVL 1
gdunn59Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Haris DulicIT ArchitectCommented:
HI,

you can do it also like this:

select
FACETS.CMC_CLCL_CLAIM.CLCL_ID,
FACETS.CMC_CDML_CL_LINE.CDML_SEQ_NO,
FACETS.CMC_CDML_CL_LINE.IPCD_ID,
FROM           
FACETS.CMC_CLCL_CLAIM
LEFT OUTER JOIN
FACETS.CMC_CDML_CL_LINE ON FACETS.CMC_CLCL_CLAIM.CLCL_ID = FACETS.CMC_CDML_CL_LINE.CLCL_ID
where FACETS.CMC_CLCL_CLAIM.CLCL_ID = '123450565300'
and (FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '99201' AND '99216' or 
FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '10000' AND '69999')

Open in new window

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
pcelbaCommented:
The local alias "s" is used incorrectly in your query but removing the part after UNION solved it.
I would also remove the comma before FROM clause. The expression is expected after the comma as the error message says.
select
FACETS.CMC_CLCL_CLAIM.CLCL_ID,
FACETS.CMC_CDML_CL_LINE.CDML_SEQ_NO,
FACETS.CMC_CDML_CL_LINE.IPCD_ID
FROM           
FACETS.CMC_CLCL_CLAIM
LEFT OUTER JOIN
FACETS.CMC_CDML_CL_LINE ON FACETS.CMC_CLCL_CLAIM.CLCL_ID = FACETS.CMC_CDML_CL_LINE.CLCL_ID
where FACETS.CMC_CLCL_CLAIM.CLCL_ID = '123450565300'
and (FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '99201' AND '99216'
     OR FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '10000' AND '69999')

Open in new window

gdunn59Author Commented:
I tried both resolutions, and neither of them are yielding what I need.

Here are the results I'm getting:

                CLCL_ID                CDML_SEQ_NO                IPCD_ID
1              123450565300                 1                           1172059
2              123450565300                 2                           1105659
3              123450565300                 3                           1171959
4              123450565300                 4                           9921225

I need to have the results laid out as follows:

                CLCL_ID                CDML_SEQ_NO                IPCD_ID
1              123450565300                1                            1172059, 1105659, 1171959, 9921225
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

pcelbaCommented:
We've been solving just the error message, so the ID concatenation can be done e.g. this way:
SELECT
   FACETS.CMC_CLCL_CLAIM.CLCL_ID "Id",
   MIN(FACETS.CMC_CDML_CL_LINE.CDML_SEQ_NO) "Seq_No",
   LISTTAG(FACETS.CMC_CDML_CL_LINE.IPCD_ID, ',') WITHIN GROUP (ORDER BY FACETS.CMC_CDML_CL_LINE.IPCD_ID) "IPCD_Id"
FROM FACETS.CMC_CLCL_CLAIM
LEFT OUTER JOIN
     FACETS.CMC_CDML_CL_LINE ON FACETS.CMC_CLCL_CLAIM.CLCL_ID = FACETS.CMC_CDML_CL_LINE.CLCL_ID
WHERE FACETS.CMC_CLCL_CLAIM.CLCL_ID = '123450565300'
  AND (FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '99201' AND '99216'
       OR FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '10000' AND '69999')
GROUP BY FACETS.CMC_CLCL_CLAIM.CLCL_ID

Open in new window

More info: https://docs.oracle.com/database/121/SQLRF/functions100.htm#SQLRF30030
gdunn59Author Commented:
pcelba:

I tried your last posting, and I'm getting the error:

   "FROM keyword not found where expected"

Thanks,

gdunn59
pcelbaCommented:
Interesting... Did you use it "As Is"?  
You could try to remove column names (in double quotes).
pcelbaCommented:
UPDATE: the aggregate function should have been LISTAGG  not  LISTTAG ...

And this function is available in Oracle 11.2. What is your version?

select banner from v$version;
gdunn59Author Commented:
I'm using Teradata
pcelbaCommented:
Teradata does not recognize LISTAGG but you could use XMLAGG if Teradata's XML services are installed:
SELECT
   FACETS.CMC_CLCL_CLAIM.CLCL_ID "Id",
   MIN(FACETS.CMC_CDML_CL_LINE.CDML_SEQ_NO) "Seq_No",
   TRIM(TRAILING ',' FROM (XMLAGG(TRIM(FACETS.CMC_CDML_CL_LINE.IPCD_ID)|| ',' ORDER BY FACETS.CMC_CDML_CL_LINE.IPCD_ID) (VARCHAR(8000))))
FROM FACETS.CMC_CLCL_CLAIM
LEFT OUTER JOIN
     FACETS.CMC_CDML_CL_LINE ON FACETS.CMC_CLCL_CLAIM.CLCL_ID = FACETS.CMC_CDML_CL_LINE.CLCL_ID
WHERE FACETS.CMC_CLCL_CLAIM.CLCL_ID = '123450565300'
  AND (FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '99201' AND '99216'
       OR FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '10000' AND '69999')
GROUP BY FACETS.CMC_CLCL_CLAIM.CLCL_ID

Open in new window


If your Teradata version does not support XML services then you'll need to use recursion as described e.g. here: https://forums.teradata.com/forum/general/equivalent-of-listagg-in-teradata
gdunn59Author Commented:
pcelba:

I tried your last posting, and I'm still getting the error:

   "FROM keyword not found where expected"

Thanks,

gdunn59
pcelbaCommented:
What is your Teradata version? Are XML services installed? XMLAGG is not supported everywhere.

And if XML services are not available then you have to use the recursion.
gdunn59Author Commented:
I can't get the XML Services installed, so I was wondering how I could do a subquery in Teradata?

Basically, I need to select where
FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '99201' AND '99216'

and then I need to do a select where
       OR FACETS.CMC_CDML_CL_LINE.IPCD_ID BETWEEN '10000' AND '69999'

Thanks,

gdunn59
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
Query Syntax

From novice to tech pro — start learning today.