2014-03-06 44 views
-1

當我運行4個不同的查詢時,我收到了一些無意義的結果。SQL查詢中的無意義結果

最主要的一條是:

SELECT distinct p.id 
FROM praxis px,basis_medikation bm,therapie_zyklus_medikation tzm,therapie_zyklus tz,therapie t,patient p, behandlung b 
WHERE 1=1 
AND EXISTS (select * from diagnose d, diagnose_mamma dm, tumor_status ts where d.patient_id = p.id and d.diagnose_mamma_id = dm.id and d.deleted = 0 
    and ts.tumorstatus_m in ('M1') 
    and d.datum < t.datum 
    and (ts.id = dm.tumorstatus_id or ts.id = dm.tumorstatus_rechts_id)) 
AND px.ID = p.praxis_ID AND px.testpraxis = 0 AND tz.deleted = 0 and tzm.deleted = 0 and t.deleted = 0 AND p.ID = t.patient_ID AND t.ID = tz.therapie_id AND tz.ID = tzm.therapiezyklus_id AND tzm.basis_medikation_id = bm.ID 
AND t.datum >= to_date('2009-10-01', 'yyyy-MM-dd') AND t.datum < to_date('2013-10-01', 'yyyy-MM-dd') 
AND t.behandlung_id = b.id 
AND b.deleted = 0 
AND ( 
     ( 
     bm.atcaname in ('Medizin1','Medizin2','Medizin3','Medizin4') 
     AND bm.product_name <> 'Medizin0' 
     AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5') 
     AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6') 
     ) 
     OR ( 
     bm.atcaname in ('Medizin1','Medizin2','Medizin3','Medizin4') 
     AND bm.product_name <> 'Medizin0' 
     AND EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5') 
     AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6') 
     ) 
     OR ( 
     bm.atcaname in ('Medizin1','Medizin2','Medizin3','Medizin4') 
     AND bm.product_name <> 'Medizin0' 
     AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5') 
     AND EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6') 
     ) 
     OR ( 
     bm.atcaname not in ('Medizin1','Medizin2','Medizin3','Medizin4') 
     AND bm.product_name = 'Medizin0' 
     AND EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5') 
     AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6') 
     ) 
     OR ( 
     bm.atcaname not in ('Medizin1','Medizin2','Medizin3','Medizin4') 
     AND bm.product_name = 'Medizin0' 
     AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5') 
     AND EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6') 
     ) 
     OR ( 
     bm.atcaname not in ('Medizin1','Medizin2','Medizin3','Medizin4') 
     AND bm.product_name = 'Medizin0' 
     AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5') 
     AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6') 
     ) 
    ) ; 

它選擇的乳腺癌患者已被處理,要麼Medizin1或Medizin2或Medizin3或僅Medizin4,和那些誰合併任何與Medizin5或Medizin6這些藥物的和那些誰一直在與Medizin0和Medizin5治療,和那些誰一直在與Medizin0和Medizin6和那些誰一直只與Medizin0治療治療...

對於第二個查詢我只添加條件

AND b.line = 1 

對於第三查詢我補充一下:

AND b.line = 2 

而對於第四:

AND b.line > 2 

而且廢話來當結果第一次查詢的數量比的總和低其他三個查詢的結果...應該等於或少於...我認爲...

但的確我缺少一些東西...

+2

每當您編寫單個SQL查詢並且它不再適合一個屏幕時,您應該停下來思考如果無法以更優雅的方式解決問題。 – Philipp

+0

您是否檢查某些返回的行是否重複?對所有三個查詢進行聯合查找。 – dnoeth

+0

如果沒有真正看到數據庫中的數據,真的很難猜測解決方案對於這個問題的影響。我會仔細看看你的數據,以確保'b.line'具有你期望的值。 – woemler

回答

1

而且廢話來的時候成績爲第一個查詢 的數量比其他三個查詢的結果的總和...... 應該是等於或小於......我想下..

我認爲你的邏輯有缺陷,但它取決於你的數據。

如果你有同樣的patient.id有記錄,其中behandlung.line爲1,一個地方是2,和一個地方是3,則ID將上市正好一次你查詢的所有四個(因爲distinct)。這並不意味着它是錯誤的,但篩選查詢的計數總和將是3,而未經篩選的查詢仍將只有一行。

count(distinct id)是不一樣的count (id),並沒有可比性:

count(distinct case when line = 1 then id end) 
    + count(distinct case when line = 2 then id end) 
    + count(distinct case when line > 2 then id end) 

沒有distinct你的邏輯也適用,雖然exists門檻變得複雜了一點。

也許一個例子會有所幫助,簡化爲忽略所有連接等。:

create table t42 (id number, line number); 
insert into t42 (id, line) values (1, 1); 
insert into t42 (id, line) values (1, 2); 
insert into t42 (id, line) values (2, 1); 
insert into t42 (id, line) values (2, 2); 
insert into t42 (id, line) values (2, 3); 
insert into t42 (id, line) values (3, 2); 

你的第一個查詢發現3行:

select distinct id from t42; 

     ID 
---------- 
     1 
     2 
     3 

3 rows selected 

你第二個查詢發現兩行,但兩者的ID是(當然)包含在第一查詢結果太:

select distinct id from t42 where line = 1; 

     ID 
---------- 
     1 
     2 

2 rows selected 

第三個查詢找到三行,這次完全複製第一個查詢的結果集(在這個非常設計的情況下),那些來自sec OND查詢:

select distinct id from t42 where line = 2; 

     ID 
---------- 
     1 
     2 
     3 

3 rows selected 

而且你的第三個查詢只找到一排,這是在以前所有的結果集:

select distinct id from t42 where line > 2; 

     ID 
---------- 
     2 

1 rows selected 

所以第一個查詢查找三人行,這比之少其他3 - 2 + 3 + 1發現的行數是6。所有結果集都是正確的,但不能比較總行數,因爲相同的ID被包含在多個集合中。 ID 1兩次計數,2三次計數,並且3計爲一次。

結果集中的ID總數相同 - 畢竟這三個IDS是相同的 - 並且在原始未過濾的查詢中未出現三個過濾查詢中奇蹟般出現的ID。但是你必須消除重複以進行任何形式的比較。例如,合併三個已過濾的查詢並計算所有三個不同ID的數量將與原始查詢匹配。 (在這種情況下,你說你的行數爲line < 1,所以在你真正的查詢中總數會低於你的預期)。

+0

謝謝:)我對這件事太耿耿於懷了,並不那麼難! – diminuta