我对如何完成此查询感到困惑。
如何在不影响性能的情况下实现此查询(如果表包含数千行)?
请指教!
MyTable
pktimestmp | sw_id | url_id | open_date | end_date
-------------------------------------------------------
xxx0 | 101 | com | 2013-01-01 | 2013-01-30
xxx1 | 202 | net | 2013-01-01 | 2013-01-30
xxx2 | 202 | net | 2013-01-15 | 2013-02-28 *
xxx3 | 303 | com | 2013-01-01 | 2013-01-30
xxx4 | 303 | com | 2013-02-01 | 2013-02-15 *
xxx5 | 303 | com | 2013-03-01 | 2013-03-15 *
xxx6 | 404 | org | 2013-01-01 | 2013-01-30
xxx7 | 404 | org | 2013-02-01 | 2013-02-15 *
xxx8 | 404 | gov | 2013-02-01 | 2013-02-15
xxx9 | 404 | gov | 2013-02-01 | 2013-02-15
...
更新查询:
软件和网址相同的日期(即sw_id和url_id)。
我们将opendate和enddate设置为less(min)出现次数。
xxx2 | 202 | net | 2013-01-15 | 2013-02-28
will be
xxx2 | 202 | net | 2013-01-01 | 2013-01-30
here we have updated
open_date to min(open_date)
and
end_date to min(end_date)
from same sw and url.
(如果我们只更新有差别的地方会很好, 例如,无需更新xxx8和xxx9,因为它们是“相同的”)
MyTable (* =更新的行)
pktimestmp | sw_id | url_id | open_date | end_date
-------------------------------------------------------
xxx0 | 101 | com | 2013-01-01 | 2013-01-30
xxx1 | 202 | net | 2013-01-01 | 2013-01-30
xxx2 | 202 | net | 2013-01-01 | 2013-01-30 *
xxx3 | 303 | com | 2013-01-01 | 2013-01-30
xxx4 | 303 | com | 2013-01-01 | 2013-01-30 *
xxx5 | 303 | com | 2013-01-01 | 2013-01-30 *
xxx6 | 404 | org | 2013-01-01 | 2013-01-30
xxx7 | 404 | org | 2013-01-01 | 2013-01-30 *
xxx8 | 404 | gov | 2013-02-01 | 2013-02-15
xxx9 | 404 | gov | 2013-02-01 | 2013-02-15
提前致谢! (并且我无权重新对表格进行建模):)
感谢任何帮助,网址,提示,电子书,网站......
答案 0 :(得分:1)
以下内容应该有效(几乎任何RDBMS):
UPDATE MyTable as a SET (open_date, close_date)
= (SELECT MIN(open_date), MIN(close_date)
FROM MyTable as b
WHERE b.sw_id = a.sw_id
AND b.url_id = a.url_id)
WHERE EXISTS (SELECT *
FROM MyTable as b
WHERE b.sw_id = a.sw_id
AND b.url_id = a.url_id
AND (b.open_date < a.open_date OR b.close_date < a.close_date))
生成预期的,只更新了4行(针对我的iSeries实例进行了测试):
PKTIMESTAMP SW_ID URL_ID OPEN_DATE CLOSE_DATE
xxx0 101 com 2013-01-01 2013-01-30
xxx1 202 net 2013-01-01 2013-01-30
xxx2 202 net 2013-01-01 2013-01-30
xxx3 303 com 2013-01-01 2013-01-30
xxx4 303 com 2013-01-01 2013-01-30
xxx5 303 com 2013-01-01 2013-01-30
xxx6 404 org 2013-01-01 2013-01-30
xxx7 404 org 2013-01-01 2013-01-30
xxx8 404 gov 2013-02-01 2013-02-15
xxx9 404 gov 2013-02-01 2013-02-15