0

I have two tables:

TotalTimeTable
(date date,time time)

FinalListA
(date date, time time, A int, B int)

I am running the following query:

SELECT t1.date,
       t1.time,
       max(t2.time)
FROM TotalTimeTable t1,
     FinalListA t2
WHERE t2.date=t1.date
  AND t2.time <= t1.time
GROUP BY t1.date,
         t1.time
ORDER BY t1.date,
         t1.time

Is there a way to pull out A and B from FinalListA where time = "max(t2.time)" in the same query?

TotalTimeTable:

date        time    
2006-01-01  9:30:01     
2006-01-01  9:30:02 
2006-01-01  9:30:03
2006-01-01  9:30:04
2006-01-01  9:30:05 
2006-01-01  9:30:06

FinalListA:

date        time     A B
2006-01-01  9:28:01  1 4
2006-01-01  9:30:02  2 3
2006-01-01  9:30:04  4 7
2006-01-01  9:30:07  6 4
2006-01-01  9:30:10  8 9
2006-01-01  9:30:11  1 2

The expected result is:

date        time     A B
2006-01-01  9:30:01  1 4
2006-01-01  9:30:02  2 3
2006-01-01  9:30:03  2 3
2006-01-01  9:30:04  4 7
2006-01-01  9:30:05  4 7
2006-01-01  9:30:06  4 7

The time is from TotalTimeTable FinalListA.time don't have to be in the result.

1
  • 2
    Please, provide sample rows. Commented Apr 17, 2013 at 9:54

3 Answers 3

1

Please try:

SELECT t1.date,
       t1.time,
       (select top 1 t2.A from FinalListA t2 where t2.date=t1.date and t2.Time<=t1.Time order by t2.Time desc) A,
       (select top 1 t2.B from FinalListA t2 where t2.date=t1.date and t2.Time<=t1.Time order by t2.Time desc) B
FROM TotalTimeTable t1

or using left join

select 
    x.date,
    x.Time,
    x.A,
    x.B
from(
    SELECT t1.*,
           t2.A,
           t2.B,
           ROW_NUMBER() over (PARTITION BY t1.date, t1.time order by t2.time desc) RNum
    FROM TotalTimeTable t1 left join
         FinalListA t2 on t1.date=t2.date and t2.Time<=t1.Time
)x where RNum=1

or using CROSS APPLY

SELECT t1.date,
       t1.time, 
       x.*
FROM TotalTimeTable t1 cross apply (
SELECT TOP 1 t2.A, t2.B from FinalListA t2 where t2.date=t1.date and t2.Time<=t1.Time order by t2.Time desc)x
Sign up to request clarification or add additional context in comments.

7 Comments

Please add sample data, and expected output.
I've added sample data just now, i'll add expected output in a minute
Please check the edited answer using sub-query and left join.
Is there a way to do 2 top 1 select together? Besides, first query is the quickest and the results are correct.
Please check the result using CROSS APPLY.
|
0

Using cross apply:

SELECT t1.date
   , t1.time
   , t2.time
   , t2.A
   , t2.B
FROM TotalTimeTable t1
CROSS APPLY (
 SELECT TOP(1)
  T2.time
 , T2.A
 , T2.B
FROM FinalListA t2
WHERE T2.date = T1.date
AND T2.time <= t1.time
ORDER BY T2.time DESC
) AS T2
ORDER BY t1.date,
     t1.time

2 Comments

Sorry, didn't had time to test it yet
The result is correct but it is a little bit slower than of @techdo
0

You can use your original query as a constraining subquery (without the ORDER BY)

SELECT t1.Date, t1.Time, t2.Time, t2.A, t2.B
FROM TotalTimeTable t1
INNER JOIN 
(
    SELECT t1.date,
           t1.time,
           maxt2Time = max(t2.time)
    FROM TotalTimeTable t1,
         FinalListA t2
    WHERE t2.date=t1.date
      AND t2.time <= t1.time
    GROUP BY t1.date,
             t1.time
) tmax
ON t1.Date = tmax.date AND t1.Time = tmax.Time
INNER JOIN FinalListA t2 ON t2.Date = tmax.Date AND t2.Time = tmax.maxt2Time

3 Comments

@Serge's cross-apply below is about 50% faster
Yes, this is how I'm planning to do it, but I want to check if there is simpler/faster query than this.
Could reduce the the subquery to INNER JOIN (SELECT date, time = max(time) from FinalListA GROUP BY date) tmax

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.