2

I have a table Person, date is in yyyymmDD format and time is is hhmmssSSS format

ID    NAME      CREATEDDATE     CREATEDTIME
1      A          20170205       135744300
2      B          20160205       113514368
3      C          20090205       163054942
4      A          20150205       135744301

Now I want to get count of each person and maximum createddate and CORRESPONDING createdTIME in a single query

I tried

SELECT NAME, COUNT (ID) AS COUNT, MAX(CREATEDDATE), MAX(CREATEDTIME)
FROM Person

but this gives me maximum date and maximum time from each column, it does not give me CREATEDTIME corresponding to MAX(CREATEDDATE)

e.g.

The query results is

NAME   COUNT    MAXCREATEDDATE       CORRESPONDINGCREATEDTIME

A        2        20170205             135744301

The CORRESPONDINGCREATEDTIME should be 135744300

Please help me do it

2
  • what data types are the final two columns? date/time or varchar or int? Commented Jun 24, 2016 at 9:18
  • They are INT. The date and time are in reserve format. Commented Jun 24, 2016 at 9:22

6 Answers 6

2

You could do this simply with a subquery that finds the max date for each Name and perform an inner join.

Test Data

CREATE TABLE #Person (ID int, Name varchar(1), CreatedDate int, CreatedTime int)
INSERT INTO #Person (ID, Name, CreatedDate, CreatedTime)
VALUES
 (1,'A',20170205,135744300)
,(2,'B',20160205,113514368)
,(3,'C',20090205,163054942)
,(4,'A',20150205,135744301)

Query

SELECT
a.Name
,b.CtName CountName
,a.CreatedDate
,a.CreatedTime
FROM #Person a
JOIN    (
            SELECT 
            Name
            ,COUNT(Name) CtName
            ,MAX(CreatedDate) MaxDate 
            FROM #Person 
            GROUP BY Name
        ) b
ON a.Name = b.Name
AND a.CreatedDate = b.MaxDate

Result

Name    CountName   CreatedDate CreatedTime
A       2           20170205    135744300
B       1           20160205    113514368
C       1           20090205    163054942
Sign up to request clarification or add additional context in comments.

Comments

1

One more, no joins or subquery

SELECT NAME, COUNT (ID) AS COUNT
  , MaxCREATEDDATE = MAX(cast(CREATEDDATE as bigint)*1000000000 + CREATEDTIME) /1000000000
  , CorrespTime = MAX(cast(CREATEDDATE as bigint)*1000000000 + CREATEDTIME) %1000000000
FROM Person
GROUP by NAME

2 Comments

WOW that is a genius! Can you please explain the logic ? I think you have added both and then took a part of date and time
Exactly. First CREATEDDATE and CREATEDTIME are wrapped into single bigint. When max is found integer divisions ( / % ) are applied to get parts back.
1

Try this answer,

SELECT NAME, COUNT(ID) AS COUNT, max(CREATEDDATE), max(CREATEDTIME)
FROM Person
where CREATEDDATE = (select MAX(CREATEDDATE) from Person)
group by NAME

3 Comments

This is not the result OP wants. Your query will output only one person having created date equals to the maximum created date of all records.
Yes that's right, I want each persons MAX DATE and corresponding time
@DineshDB I thought that, but his result does give the example expected results that the OP posted.
1

Try this also

    SELECT DISTINCT
       R.NAME,
       COUNT(1) OVER (PARTITION BY NAME),
       MAX(R.CREATEDDATE) OVER (PARTITION BY NAME ) ,
       MAX(P.CREATEDTIME) OVER (PARTITION BY NAME )
    FROM
        Person

2 Comments

I think Now better
It is still max time. Exactly what OP said was a problem. See my answer.
0

You can calculate count and max(CREATEDDATE) in inner query and then join back to your table to get time according to max date for each person:

select
   R.NAME,
   R.COUNT,
   R.CREATEDDATE,
   P.CREATEDTIME
from
  (
    SELECT NAME, COUNT (ID) AS COUNT, MAX(CREATEDDATE) as CREATEDDATE
    FROM Person
    group by NAME
  ) as R
  left outer join Person as P on P.NAME = R.NAME and P.CREATEDDATE = R.CREATEDDATE

Comments

0
select * from 
(
SELECT *,
       COUNT(1)     OVER (PARTITION BY NAME) as count,
       row_number() OVER (PARTITION BY NAME order by CREATEDDATE desc) as rn
 FROM  Person
) tt
where tt.rn = 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.