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.
| Author |
Topic |
|
PingPing
Starting Member
1 Post |
Posted - 2010-04-08 : 06:52:01
|
| I have a table called 'q1' that looks like this:Marker Started Server StatusCRAB 03:47:20 PC3 StartedCRAB 05:38:51 PC3 StartedCRAB 06:08:23 PC2 StartedCRAB 06:19:18 PC2 CompletedI want to query to output like this:Marker Started Server CompletedCRAB 03:47:20 PC3 NULLCRAB 05:38:51 PC3 NULLCRAB 06:08:23 PC2 06:19:18I've been trying all morning with INNER JOINs and LEFT JOINs but I can't seem to crack it. Can anyone help please? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-04-08 : 07:00:51
|
| select Marker, Started, Server, case when status='Completed' then Started else null end as Completed from q1MadhivananFailing to plan is Planning to fail |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-08 : 07:32:49
|
quote: Originally posted by madhivanan select Marker, Started, Server, case when status='Completed' then Started else null end as Completed from q1
That's not quite what the OP is after.PingPing, can you supply a little more data, or a more indepth description of your logic? It's not quite clear exactly what you're after. eg, what would the output of the following look like?Marker Started Server StatusCRAB 03:47:20 PC3 StartedCRAB 05:38:51 PC3 StartedCRAB 06:09:18 PC3 CompletedCRAB 06:08:23 PC2 StartedCRAB 06:19:18 PC2 CompletedFrom what I can see, this could either be :CRAB 03:47:20 PC3 06:09:18CRAB 05:38:51 PC3 06:09:18CRAB 06:08:23 PC2 06:19:18or:CRAB 03:47:20 PC3 NULLCRAB 05:38:51 PC3 06:09:18CRAB 06:08:23 PC2 06:19:18There are 10 types of people in the world, those that understand binary, and those that don't. |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-04-08 : 07:42:52
|
Yes madhi's query is not giving required resultbut i think its first step towards solution as i also reached upto that and I think OP is trying to display server started and completedif it completed means for any perticular server he found status completed then it will take time of that statusif not then null as it is not completed yet.but this is unclear that if a server has status started twice and one status completed then which time will be taken from that two status more explanation is requiredVaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-08 : 08:18:50
|
may be this:-SELECT t.Marker,t.Started,t.Server,CASE WHEN t2.Started IS NULL THEN t1.Started ELSE NULL ENDFROM Table tOUTER APPLY (SELECT TOP 1 Started FROM Table WHERE Status='Completed' AND Server=t.Server AND Marker=t.Marker AND Started > t.Started ORDER BY Started ASC)t1OUTER APPLY (SELECT TOP 1 Started FROM Table WHERE Status='Started' AND Server=t.Server AND Marker=t.Marker AND Started > t.Started AND Started < t1.Started ORDER BY Started ASC)t2WHERE t.Status='Started' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-08 : 08:21:21
|
quote: Originally posted by visakh16 may be this:-SELECT t.Marker,t.Started,t.Server,CASE WHEN t2.Started IS NULL THEN t1.Started ELSE NULL ENDFROM Table tOUTER APPLY (SELECT TOP 1 Started FROM Table WHERE Status='Completed' AND Server=t.Server AND Marker=t.Marker AND Started > t.Started)t1OUTER APPLY (SELECT TOP 1 Started FROM Table WHERE Status='Started' AND Server=t.Server AND Marker=t.Marker AND Started > t.Started AND Started < t1.Started)t2WHERE t.Status='Started'
Should there be an ORDER BY clause in those sub queries?There are 10 types of people in the world, those that understand binary, and those that don't. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-08 : 08:23:58
|
quote: Originally posted by DBA in the making
quote: Originally posted by visakh16 may be this:-SELECT t.Marker,t.Started,t.Server,CASE WHEN t2.Started IS NULL THEN t1.Started ELSE NULL ENDFROM Table tOUTER APPLY (SELECT TOP 1 Started FROM Table WHERE Status='Completed' AND Server=t.Server AND Marker=t.Marker AND Started > t.Started)t1OUTER APPLY (SELECT TOP 1 Started FROM Table WHERE Status='Started' AND Server=t.Server AND Marker=t.Marker AND Started > t.Started AND Started < t1.Started)t2WHERE t.Status='Started'
Should there be an ORDER BY clause in those sub queries?There are 10 types of people in the world, those that understand binary, and those that don't.
Sorry I missed itHave edited my original responseThanks for the catch ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|