Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have requirement as follows .
Id     createddt createdby   modifideby modifydt

1      21/2/2015    2         NULL       NULL
1      NULL         NULL       4        22/2/2015
2      22/2/2015     3         NULL      NULL
2      NULL         NUll       3         23/2/2015
1      NULL         NULL       5         27/2/2015

I need As follows :
Id     createddt createdby   modifideby modifydt

1      21/2/2015    2          5         27/2/2015
2      22/2/2015     3         3         23/2/2015

I need created date and last modified date in one row ......
Posted
Updated 3-Jun-15 4:41am
v2
Comments
JJMatthews 3-Jun-15 10:27am    
I am far from a database expert but I had the same problem to solve last year (if I understand correctly). I ended up using a "left outer join" to get the correct results.
CHill60 3-Jun-15 10:44am    
What have you tried?

In it's simplest terms you can just do
SQL
select Id, max(createddt), max(createdby), max(modifideby), max(modifydt)
from tablename
group by Id
which will give you the results you expect. Note that MAX[^] ignores NULL values.

I suspect there is more to your requirement however - is your course work to do with PIVOT? If so then read this article Simple Way To Use Pivot In SQL Query[^]
 
Share this answer
 
Comments
Maciej Los 3-Jun-15 11:05am    
+5!
Use max and min should do the trick:
SQL
select id, min(createddt), max(createdby), max(modifideby), max(modifydt) from tablename
group by id
 
Share this answer
 
Comments
CHill60 3-Jun-15 10:59am    
Apologies for the overlap - system is running slow
Peter Leow 3-Jun-15 11:03am    
Yes, it is happening intermittently.
Maciej Los 3-Jun-15 11:05am    
+5!
Try this:

SQL
SELECT t1.Id, t1.createddt, t1.createdby, t2.modifiedby, t2.modifydt
FROM TableName AS t1 INNER JOIN 
     (
         SELECT Id, MAX(modifydt) AS modifydt
         FROM TableName 
         GROUP BY Id
     ) AS t2 ON t1.Id = t2.Id AND t1.modifydt = t2.modifydt
 
Share this answer
 
Comments
CHill60 3-Jun-15 10:59am    
Apologies for the overlap - system is running slow
Maciej Los 3-Jun-15 11:05am    
No problem ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900