سؤال

This should be fairly simple and thanks in advance. I have a unique ID column and a start column populated with integers. I want to create a new column populated with the minimum start date for each unique ID. example follows:

ID START

1 23

1 24

1 34

2 12

2 11

and what i want

ID START minStart

1 23 23

1 24 23

1 34 23

2 12 11

2 11 11

هل كانت مفيدة؟

المحلول

SAS proc sql has a facility called re-merging that allows you to do this in one step:

proc sql;
    select id, start, min(start)
    from t
    group by id;
run;

SAS recognizes a group by where not all the non-aggregated columns are included in the group by clause. In this case, it returns each row in the original data set, with the aggregation function min(start) aggregated according to id (because it is in the group by) clause.

نصائح أخرى

In T-SQL this would do it:

SELECT a.ID, a.START, b.MinStart
FROM Table AS a
JOIN (SELECT ID, MIN(START)'MinStart'
      FROM Table
      GROUP BY ID
     )AS b
ON a.ID = b.ID

But it looks like there's a better method in SAS per Gordon's answer.

TRY THIS

SELECT A.ID,A.START,B.START FROM TABLE_NAME A
 INNER JOIN
 (
      SELECT ID ,MIN(START) START FROM TABLE_NAME
      GROUP BY ID
 )B
ON A.ID=B.ID

Update me if not

Regards

Ashutosh Arya

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top