BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sarahzhou
Quartz | Level 8

 

Hi Instructors,

I created table as below:

create table WORK.RelationTable as select * from connection to ServerName
(select distinct A.*
           ,INDV_KEY
           ,TO_KEY
           ,RLTN_TX
           ,RLTN_STRT_DT
           ,max(RLTN_END_DT) as RLTN_END_DT from &TableSourceOne. A
INNER join ##TableSourceTwo B
on (A.INDV_KEY=B.TO_INDV_KEY)
);

the error occurs at max(RLTN_END_DT) as RLTN_END_DT, when I change back to RLTN_END_DT, then it runs ok.

 

I tried add a filter where  RLTN_END_DT=max(RLTN_END_DT), it doesn't work

 

What shoud I do?

Thank you!

 

RLTN_END_DT: relation end datetime

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

First of all, you must tell SQL from where each individual column has to be taken, so all of your column names should either have an A. or B. as prefix. Even if the procedure does not complain (because a certain column is contained in only one table), working without prefixes is sloppy programming.

Finding a single record in a group is done like this in SQL:

select a.*
from table a
group by a.indv_key
having rltn_end_dt = max(a.rltn_end_dt)

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User
  • Please post the complete log from that step into a window opened with this button: Bildschirmfoto 2020-04-07 um 08.32.59.jpg
  • Since this happens in explicit pass-through, you should consult sources that deal with that specific DBMS
ChrisNZ
Tourmaline | Level 20

1. Is max a valid function name in your database?

2. Do you really want to use max() and have no group by clause?

sarahzhou
Quartz | Level 8

I added group by clause

 

create table WORK.RELATION as select * from connection to SQLSERVER
		    (select distinct A.*, INDV_KEY, TO_INDV_KEY, RLTN_TX, RLTN_STRT_DT, MAX(RLTN_END_DT) as RLTN_END_DT from &TABLENAMEONE. A
				INNER join ##TABLEDATATWO B
					on (A.INDV_KEY=B.TO_INDV_KEY)				
			)GROUP BY INDV_KEY;

ERROR: CLI describe error: [SAS][ODBC SQL Server Wire Protocol driver][Microsoft SQL Server]Column 'TABLENAMEONE.INDV_KEY' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. :
[SAS][ODBC SQL Server Wire Protocol driver][Microsoft SQL Server]The batch could not be analyzed because of compile errors.

what is wrong with my "MAX(RLTN_END_DT)".

Please help. Thank you!

ChrisNZ
Tourmaline | Level 20

The message couldn't be clearer:

  Column 'TABLENAMEONE.INDV_KEY' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

 

> what is wrong with my "MAX(RLTN_END_DT)".

 Nothing. As the message mentions, the issue is with INDV_KEY

What result do you expect from this query? What total time do you expect ?

 

Also adding group by outside of the parentheses makes no sense.

sarahzhou
Quartz | Level 8

In previous code which run correct and get the table below

 

INDV_KEY TO_INDV_KEY RLTN_TX RLTN_STRT_DT RLTN_END_DT
1F6A57115A 1F6A57115B member 14-Feb-18 30-Apr-18
1F6A57115A 1F6A57115B member 24-Feb-18 .

 

I only want the first row to be returned.

 

Thank you

Kurt_Bremser
Super User

First of all, you must tell SQL from where each individual column has to be taken, so all of your column names should either have an A. or B. as prefix. Even if the procedure does not complain (because a certain column is contained in only one table), working without prefixes is sloppy programming.

Finding a single record in a group is done like this in SQL:

select a.*
from table a
group by a.indv_key
having rltn_end_dt = max(a.rltn_end_dt)
sarahzhou
Quartz | Level 8
Thank you! The code was fixed. 🙂
Thank you very much!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1299 views
  • 0 likes
  • 3 in conversation