tags:

views:

65

answers:

3

Table1

ID Date Time Functionkey

001 23-02-2009, 08:00:00 A
001 23-02-2009, 12:00:00 A
001 23-02-2009, 13:00:00 B
001 23-02-2009, 14:00:00 B
001 23-02-2009, 16:00:00 C
001 23-02-2009, 17:00:00 C
001 23-02-2009, 19:00:00 D
001 23-02-2009, 21:00:00 D
001 24-02-2009 10:00:00 A
001 24-02-2009 18:00:00 D
001 25-02-2009 09:00:00 A
001 25-02-2009 12:00:00 B
001 25-02-2009 18:00:00 D
....,

I want to get min(time) where function key = 'A', max(time) where function key = 'B', min(time) where function key ='c'. max(time) where function key = 'D' Query:

   SELECT 
      ID, Date, 
      CASE WHEN function = 'A' THEN min(time) END as Intime1,
      CASE WHEN function = 'B' THEN max(time) END as Outtime1, 
      CASE WHEN function = 'C' THEN min(time) END as Intime2,  
      CASE WHEN function = 'D' THEN max(time) END as Outtime2 
   FROM dbo.table1 
   GROUP BY id, date, function

Getting Output

ID Date, Intime1, Outtime1, Intime2, Outtime2

001 23-02-2009 08:00:00, null, null, null
001 23-02-2009 null, 14:00:00, null, null
001 23-02-2009 null, null, 16:00:00, null
001 23-02-2009 null, null, null, 21:00:00

...,

I need output in one line, How to make a query

ID Date, Intime1, Outtime1, Intime2, Outtime2

001 23-02-2009 08:00:00, 14:00:00. 16:00:00, 21:00:00 
001 24-02-2009 09:00:00  null, null, 18:00:00
001 25-02-2009 09:00:00, 12:00:00, null, 18:00:00

...,

How to modify my query?

A: 

One thing you really should consider, is to store your date/time as a single column formatted as a unix timestamp, it makes handling date/time a lot easier.

googletorp
My time column is datetime only.
Gopal
A: 

Try the following:

SELECT  ID, 
        Date, 
        min(time) AS Intime, 
        max(time) AS Outtime 
FROM table1 
GROUP BY id, date
Maximilian Mayerl
I need Min(time) when function = a and max(time) when function = d ok, am not asking about the above your posted result.
Gopal
+2  A: 

Try something like:

SELECT T1.ID, T1.Date, MIN(T1.Time) As Intime, MAX(T2.Time) AS Outtime
FROM Table1 T1
JOIN Table1 T2 ON T1.ID=T2.ID AND T1.Date=T2.Date
WHERE T1.Function = 'A' AND T2.Function='D'
GROUP BY T1.ID, T1.Date

Edit: Regarding your comment you just need to change the condition for table T1 with the function for the minimum (see the MIN(T1.Time) above) and the condition for table T2 with the function for the maximum (see MAX(T2.Time) above), so it should be:

SELECT T1.ID, T1.Date, MIN(T1.Time) As Intime, MAX(T2.Time) AS Outtime
FROM Table1 T1
JOIN Table1 T2 ON T1.ID=T2.ID AND T1.Date=T2.Date
WHERE T1.Function = 'C' AND T2.Function='D'
GROUP BY T1.ID, T1.Date

I hope I understood correctly from the comment what you need.

Edit 2: OK, I got it now. You need to do the same self join two more times like this:

SELECT 
 T1.ID, T1.Date, 
 MIN(T1.Time) AS Intime1,
 MAX(T2.Time) AS Outtime1,
 MIN(T3.Time) AS Intime2,
 MAX(T4.Time) AS Outtime2
FROM Table1 T1
JOIN Table1 T2
 ON T1.ID=T2.ID AND T1.Date=T2.Date
JOIN Table1 T3
 ON T1.ID=T3.ID AND T1.Date=T3.Date
JOIN Table1 T4
 ON T1.ID=T4.ID AND T1.Date=T4.Date
WHERE 
 T1.Func = 'A' 
 AND T2.Func='B'
 AND T3.Func='C'
 AND T4.Func='D'
GROUP BY
 T1.ID, T1.Date

Edit 3: To account for possible missing values try the next query. The only value that needs to be present is that for the function 'A'. The rest can be absent:

SELECT 
 T1.ID, T1.Date, 
 MIN(T1.Time) AS Intime1,
 MAX(T2.Time) AS Outtime1,
 MIN(T3.Time) AS Intime2,
 MAX(T4.Time) AS Outtime2
FROM Table1 T1
LEFT JOIN Table1 T2
 ON T1.ID=T2.ID AND T1.Date=T2.Date AND T2.Func = 'B'
LEFT JOIN Table1 T3
 ON T1.ID=T3.ID AND T1.Date=T3.Date AND T3.Func = 'C'
LEFT JOIN Table1 T4
 ON T1.ID=T4.ID AND T1.Date=T4.Date AND T4.Func = 'D'
WHERE 
 T1.Func = 'A' 
GROUP BY
 T1.ID, T1.Date
rslite
Working, Suppose am having Function key b and c also, Here i want to take min(time) when function key = c and max(time) when function key = d on the same table. How to make a query
Gopal
No Suppose I want to take all the function key means - Min(time) where function = 'A', max(time) where function ='b', min(time) where function = 'c', max(time) where function = 'd' on the same day. How to make modify a query
Gopal
I modify my question, Can you check please.
Gopal
And Also If function key is not available, it should show the remaining function keys value. For Example - 001 24-02-2009 09:00:00 15:00:00, null, 22:00:00, Now Your query is giving output when all the function is available, some time some date a, b is available, b and d is available, That also should display. We have to modify a join only. How to change.
Gopal
Now Working, But It is Fitering like if there in null value in functionkey column, Is not displaying, It should display. Please This is the final answer am expecting. I modified my question and answer also.
Gopal