2010-01-09 89 views
1

我有很複雜的查詢,我想創建一個視圖。查詢如下:從複雜的選擇創建視圖

select s.avg as c3, fs.bayes, fs.sure, fs.visu, fs.fstd from 
(
SELECT AVG(q.c3), COUNT(q.c3), q.std 
FROM (
     SELECT std, c3, ROW_NUMBER() OVER (PARTITION BY std ORDER BY id) AS rn 
     FROM ssims 
     WHERE obraz = 'peppers2' and noisetype ='L' and data>'2009-12-23' and maska = 9 
     ) q 
WHERE rn <= 15 
GROUP BY 
     std 
     ) s 
,(
SELECT AVG(f.bayes) as bayes, AVG(f.sure) as sure, AVG(f.visu) as visu, COUNT(f.bayes) as fcount, f.std as fstd 
FROM (
     SELECT std, bayes, sure, visu, ROW_NUMBER() OVER (PARTITION BY std ORDER BY id) AS rn 
     FROM falki_ssim 
     WHERE obraz = 'peppers2' and noisetype ='L' 
     ) f 
WHERE rn <= 15 
GROUP BY 
     std 
     ) fs 
where s.std = fs.fstd 

它選擇我用指定參數測試的平均結果。此查詢的輸出返回10行(標準值從5到50與步驟5,所以5,10,15 ...)和5列:c3,bayes,當然,可視,std

但我想以便能夠更改兩個子查詢的obraznoisetype字段。因此,在創建這樣的說法後,我希望能夠以這種方式選擇的結果:

select * from my_view where obraz='peppers2' and noisetype = 'L' 

怎麼辦呢?

回答

1

我將不得不測試它以獲得確切的查詢權限,但基本想法是在您的選擇查詢和您的group by子句中包含obraz和noisetype。類似這樣的:

select s.obraz, s.noisetype, s.avg as c3, fs.bayes, fs.sure, fs.visu, fs.fstd from 
(
SELECT obraz, noisetype, AVG(q.c3), COUNT(q.c3), q.std 
FROM (
     SELECT obraz, noisetype, std, c3, ROW_NUMBER() OVER (PARTITION BY obraz, noisetype, std ORDER BY id) AS rn 
     FROM ssims 
     WHERE data>'2009-12-23' and maska = 9 
     ) q 
WHERE rn <= 15 
GROUP BY 
     obraz, noisetype, std 
     ) s 
,(
SELECT obraz, noisetype, AVG(f.bayes) as bayes, AVG(f.sure) as sure, AVG(f.visu) as visu, COUNT(f.bayes) as fcount, f.std as fstd 
FROM (
     SELECT obraz, noisetype, std, bayes, sure, visu, ROW_NUMBER() OVER (PARTITION BY obraz, noisetype, std ORDER BY id) AS rn 
     FROM falki_ssim 
     ) f 
WHERE rn <= 15 
GROUP BY 
     obraz, noisetype, std 
     ) fs 
where s.std = fs.fstd AND s.obraz = fs.obraz AND s.noisetype = fs.noisetype 

您還應該使用JOIN關鍵字來連接表。

+0

首先,obraz和noisetype必須出現在選擇的每個級別。實際上有三種選擇。即使糾正後,查詢不幸運行,因爲我期望(它只選擇obraz/noisetype的所有可用 – Gacek 2010-01-09 14:01:02

+0

我發現一個錯誤的組合:row_number中的分區還需要包括obraz和noisetype。是固定在兩個地方 – 2010-01-09 14:11:17

+0

是的,現在它完美的工作,謝謝! – Gacek 2010-01-09 15:00:19