Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help

Author  Topic 

CT666
Starting Member

1 Post

Posted - 2011-02-07 : 15:31:14
I've a query, which returns different records for each transaction processed. For each transaction there are 3 diff. status. My query returns all 3 status along with all the other fields seperately.


CASE when tl.Status = 'LogIn' then (CONVERT(VARCHAR(10), (tl.LogDate),101)) else Null end as LogInDate,
CASE when tl.Status = 'LogOut' then (CONVERT(VARCHAR(10), (tl.LogDate),101)) else NULL end as LogoutDate,
CASE when tl.Status = 'Complete' then (CONVERT(VARCHAR(10), (tl.LogDate),101)) else NULL end as CompleteDate

Can any one plz tell me how to display a record with all of these dates returning in single row?

BEST

T C

malpashaa
Constraint Violating Yak Guru

264 Posts

Posted - 2011-02-07 : 16:06:30
Try Something like this:

DECLARE @Transactions TABLE
(
TransactionId INT,
LogDate DATETIME,
Status VARCHAR(8)
);

INSERT INTO @Transactions(TransactionId, LogDate, Status)
VALUES(1, '20110207', 'LogIn'),
(1, '20110208', 'LogOut'),
(1, '20110209', 'Complete'),
(2, '20110207', 'LogIn');

SELECT TransactionId,
MAX(CASE WHEN Status = 'LogIn' THEN CONVERT(VARCHAR(10), LogDate, 101) END) AS LogInDate,
MAX(CASE WHEN Status = 'LogOut' THEN CONVERT(VARCHAR(10), LogDate, 101) END) AS LogOutDate,
MAX(CASE WHEN Status = 'Complete' THEN CONVERT(VARCHAR(10), LogDate, 101) END) AS CompleteDate
FROM @Transactions AS T
GROUP BY TransactionId
Go to Top of Page
   

- Advertisement -