Hello, I would like to get some help to optimize a query. Problem is mostly due to slow speed it go. Since it s put into a view, i dont want to put a where clause for the "datetime" and lets the user put it when he call my view. But without this where clause, my query run real slow. With the same where clause such "where datetime > getdate() -1" if it s put at first in my view, it run like 2-3x faster than if i specify it when i call my view.
Here is what I have :
Table
Here is what i want :
It s like turn my table from vertical to horizontal, and match some record. Like for a specific date with kind = 1, i want the lastess event before that date of kind 2 and 3 with their value
Here is the query i ended with (it s quite more long in my real one since i have like 7-8 diff date, but using the same template, making 7-8 Left join)
is there any other way to make this better?
Here is what I have :
Table
Code:
key datetime kind value
1 2006-05-30 1 a
1 2006-05-29 2 b
1 2006-05-28 2 c
1 2006-05-27 2 d
1 2006-05-24 3 b
1 2006-05-23 3 c
1 2006-05-22 3 d
2 2006-05-28 1 a
2 2006-05-29 2 b
2 2006-05-28 2 c
2 2006-05-27 2 d
2 2006-05-24 3 b
2 2006-05-23 3 c
2 2006-05-22 3 d
3 2006-05-23 1 a
3 2006-05-29 2 b
3 2006-05-28 2 c
3 2006-05-27 2 d
3 2006-05-24 3 b
3 2006-05-23 3 c
3 2006-05-22 3 d
Here is what i want :
Code:
key datetime date2 value2 date3 value3
1 2006-05-30 2006-05-29 b 2006-05-24 b
2 2006-05-28 2006-05-27 d 2006-05-24 b
3 2006-05-23 NULL NULL 2006-05-22 d
It s like turn my table from vertical to horizontal, and match some record. Like for a specific date with kind = 1, i want the lastess event before that date of kind 2 and 3 with their value
Here is the query i ended with (it s quite more long in my real one since i have like 7-8 diff date, but using the same template, making 7-8 Left join)
Code:
select block1.key, block1.date1 datetime, block2.date2, block2_value.value value2, block3.date3, block3_value.value value3
from
( (
select table.key, table.datetime date1
from realtable table
where table.kind = 1
--and table.datetime > 'XXXX-XX-XX'
) block1
left join
(
(
select table.key, table.datetime date1, max(table2.datetime) date2
from realtable table, realtable table2
where table.datetime > table2.datetime, table.key = table2.key, table2.kind = 2
) block2_date
inner join
realtable block2_value
on block2_date.date2 = block2_value.datetime and block2_date.key = block2_value.key
and block2_date.kind = block2_value.kind
) block2
on block1.key = block2_date.key and block1.date1 = block2.date1
)
left join
(
(
select table.key, table.datetime date1, max(table3.datetime) date3
from realtable table, realtable table3
where table.datetime > table3.datetime, table.key = table3.key, table3.kind = 3
) block3_date
inner join
realtable block3_value
on block3_date.date3 = block3_value.datetime and block3_date.key = block3_value.key
and block3_date.kind = block3_value.kind
) block3
on block1.key = block3.key and block1.date1 = block3.date1
is there any other way to make this better?