2014-06-05 31 views
0

我試圖構建如下下面的鏈接解釋一個2x2列聯表:2×2列聯表中的SQL構建

Ad hoc 2x2 contingency tables SQL Server 2008 (試圖理解的代碼,但不能換我的頭周圍)

一個循環用於構造C1,C1 C1,C2 C2,C1 C2,C2中的對。 (笛卡爾積)

這些對是作爲sql代碼的參數給出的。對於這個例子,我已經給出了一對sql代碼 - > C1,C1當爲不同的對構造它們時,它們是正確的,如C1,C2 C2,C1(經過下面的一些修改後)。當成對C1,C1或C2,C2時,它會構造一個錯誤的應變表。

例如(表名是alpha_occurence):

id concept_uri document_uri 

1  C1  D1 

2  C2  D1 

在對C1,C1應該從上面給定的表得到的2×2列聯表:

 C1  not C1 
    C1 1  0 
not C1 0  - 

但是,相反,得到(後一些修改):

 C1 not C1 
    C1 0 1 
not C1 1 - 

注意我已經把一個 - 的價值不是C1,不是C1。因爲要計算使用其他方法。

這個SQL代碼用來檢索值:

SELECT count(*) AS total FROM 
(SELECT document_uri,count(DISTINCT concept_uri) AS count_conc FROM mydb.alpha_occurence 
WHERE concept_uri IN ('C1','C1') 
GROUP BY document_uri 
HAVING count_conc >=2) 
AS amount_of_concept_co_occurence #value of both X and Y 

UNION ALL 

SELECT count(*) AS total FROM 
(SELECT concept_uri,document_uri FROM mydb.alpha_occurence 
WHERE concept_uri IN ('C1')) 
AS only_concept_A #value of Only X not Y 

UNION ALL 

SELECT count(*) AS total FROM 
(SELECT concept_uri,document_uri FROM mydb.alpha_occurence 
WHERE concept_uri IN ('C1')) 
AS only_concept_B #value of Not X only Y 

檢索一個小腳本運行在這些值予以糾正值後。 以下完成:

To get Only X and not Y   = only_concept_A - amount_of_concept_co_occurence 
To get Not X and Only Y   = Only_concept_B - amount_of_concept_co_occurence 
To get the value of neither X or Y = total # of documents (which is not given here as the sample data only has data of which concept occurce in which document) - (amount_of_concept_co_occurence + Only X and not Y + Not X and Only Y) 
+0

請編輯您的問題,誰的數據樣本。你有一些非感性的結構,比如'not in('c1','c1')'並且提及「A」和「B」,但是在查詢中沒有這樣的引用。 –

+0

我已經編輯了我的問題,並希望我對此有所瞭解 – user3707396

+0

如果您只是展示了一些示例數據和結果,您的問題就會更清晰。我知道什麼是應急表。我*不知道*你的數據是什麼樣的。你也可以考慮關閉這個問題,並詢問另一個更好地傳達你正在試圖用你的數據完成的事情。 –

回答

1

我用這個腳本

select concept_uri, document_uri, count(*) as count 
from table 
group by concept_uri, document_uri 

,他們準備..

+0

歡迎來到堆棧溢出!儘管您可能已經解決了此用戶的問題,但僅有代碼的答案對於未來出現此問題的用戶來說並不是很有幫助。請編輯您的答案,以解釋爲什麼您的代碼可以解決原始問題。 –