2017-03-09 53 views
0

我有以下fiels表:PicklistTablePicklistColumnNameFormTableFormColumnName。 FormColumnName是PicklistTable中的一個外鍵,我需要在PicklistColumnName上加入。
基本上我需要找出每個選項列表值在引用該選項列表的所有表單上使用了多少次。因此,查詢將看起來像這樣(也許?):SQL服務器:加入一個循環表格是通過表中的行會

SELECT PicklistColumnName, count(FormColumnName1) + count(FormColumnName2) as Count 
FROM PicklistTable 
INNER JOIN FormTable1 ON PicklistColumnName = FormColumnName1 
INNER JOIN FormTable2 ON PicklistColumnName = FormColumnName2 
GROUP BY PicklistColumnName 

下面是一個例子表:

PicklistTable PicklistColumnName FormTable  FormColumnName 
tblEthnicityValues Ethnicity_ID tblContact   Contact_Ethnicity_ID 
tblEthnicityValues Ethnicity_ID udfOHFTCYPLACIHAC FatherEthnicityID 
tblEthnicityValues Ethnicity_ID udfOHFTCYPLACIHAC MotherEthnicityID 
tblEthnicityValues Ethnicity_ID udfOHFTCYPLACIHAYP EthnicityID 
tblEthnicityValues Ethnicity_ID udfOHFTCYPLACIHAYP FatherEthnicityID 
tblEthnicityValues Ethnicity_ID udfOHFTCYPLACIHAYP MotherEthnicityID 
tblEthnicityValues Ethnicity_ID udfOHFTCYPLACRHAC FatherEthnicityID 
tblEthnicityValues Ethnicity_ID udfOHFTCYPLACRHAC MotherEthnicityID 

所以我會在這種情況下,需要加入tblContact,udfOHFTCYPLACIHAC等上Contact_Ethnicity_ID, FatherEthnicityID等

有人可以幫忙嗎?我希望我解釋得很好。

謝謝! 蔡健雅

+0

這聽起來像一個很可怕的設計。你已經設計了自己被迫使用動態sql的一切。這幾乎違背了使用關係數據庫的觀點。如果你真的想在這裏得到一些幫助是一個很好的開始。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –

+0

是的,恐怕這個設計不會帶你到你身邊想去。你只是不想在動態SQL中這樣做。如果您可以選擇重新設計數據庫結構,請這樣做。 – criticalfix

+0

不幸的是,我不能做任何與數據庫...這聽起來不太有希望...... :( –

回答

0

假設從PicklistTablePicklistColumnName不必存在兩種FormTable1FormTable2表中,該查詢可能會解決您的問題。

SELECT pt.PicklistColumnName, count(ft1.FormColumnName1) + count(ft2.FormColumnName2) as Count 
FROM PicklistTable pt 
LEFT OUTER JOIN FormTable1 ft1 
ON pt.PicklistColumnName = ft1.FormColumnName1 
LEFT OUTER JOIN FormTable2 ft2 
ON pt.PicklistColumnName = ft2.FormColumnName2 
GROUP BY pt.PicklistColumnName 
+0

嗨Venu,感謝您的回答,問題是,我需要加入的表的數量將不同這取決於我所看到的選擇列表,我首先查找引用這個選項列表的所有表格,並將表格名稱放在一個臨時表格中。我想我需要一些在這種情況下的循環的王,它將爲每行添加一個FormTable表格在我的臨時表中。 –

0

你想要得到的SQL語句:

DECLARE @tb TABLE(PicklistTable VARCHAR(30),PicklistColumnName VARCHAR(30),FormTable VARCHAR(30),FormColumnName VARCHAR(30)) 
INSERT INTO @tb(PicklistTable,PicklistColumnName,FormTable,FormColumnName) 
    SELECT 'tblEthnicityValues','Ethnicity_ID','tblContact','Contact_Ethnicity_ID' UNION 
    SELECT 'tblEthnicityValues','Ethnicity_ID','udfOHFTCYPLACIHAC','FatherEthnicityID' UNION 
    SELECT 'tblEthnicityValues','Ethnicity_ID','udfOHFTCYPLACIHAC','MotherEthnicityID' UNION 
    SELECT 'tblEthnicityValues','Ethnicity_ID','udfOHFTCYPLACIHAYP','EthnicityID' UNION 
    SELECT 'tblEthnicityValues','Ethnicity_ID','udfOHFTCYPLACIHAYP','FatherEthnicityID' UNION 
    SELECT 'tblEthnicityValues','Ethnicity_ID','udfOHFTCYPLACIHAYP','MotherEthnicityID' UNION 
    SELECT 'tblEthnicityValues','Ethnicity_ID','udfOHFTCYPLACRHAC','FatherEthnicityID' UNION 
    SELECT 'tblEthnicityValues','Ethnicity_ID','udfOHFTCYPLACRHAC','MotherEthnicityID' 
DECLARE @sql VARCHAR(max) 
SELECT @sql='SELECT '+t.PicklistColumnName+',(0'+t.subCountSQL+') AS'+CHAR(13)+'Count FROM '+t.PicklistTable+CHAR(13)+t.subSQL FROM (
    SELECT DISTINCT PicklistTable,PicklistColumnName,t2.* ,t3.* 
    FROM @tb AS t1 
    CROSS APPLY(SELECT 'INNER JOIN '+FormTable+' ON PicklistColumnName='+tt.FormTable+'.'+tt.FormColumnName+' ' 
       FROM @tb AS tt WHERE tt.PicklistTable=t1.PicklistTable AND tt.PicklistColumnName=t1.PicklistColumnName 
       FOR XML PATH('') 
    ) AS t2(subSQL) 
    CROSS APPLY(SELECT '+COUNT('+tt.FormTable+'.'+tt.FormColumnName +')' 
       FROM @tb AS tt WHERE tt.PicklistTable=t1.PicklistTable AND tt.PicklistColumnName=t1.PicklistColumnName 
       FOR XML PATH('') 
    ) AS t3(subCountSQL) 

)AS t 
PRINT @sql 
EXEC(@SQL) 

變量@SQL將是:

SELECT Ethnicity_ID,(0+COUNT(tblContact.Contact_Ethnicity_ID)+COUNT(udfOHFTCYPLACIHAC.FatherEthnicityID)+COUNT(udfOHFTCYPLACIHAC.MotherEthnicityID)+COUNT(udfOHFTCYPLACIHAYP.EthnicityID)+COUNT(udfOHFTCYPLACIHAYP.FatherEthnicityID)+COUNT(udfOHFTCYPLACIHAYP.MotherEthnicityID)+COUNT(udfOHFTCYPLACRHAC.FatherEthnicityID)+COUNT(udfOHFTCYPLACRHAC.MotherEthnicityID)) AS Count FROM tblEthnicityValues 
INNER JOIN tblContact ON PicklistColumnName=tblContact.Contact_Ethnicity_ID INNER JOIN udfOHFTCYPLACIHAC ON PicklistColumnName=udfOHFTCYPLACIHAC.FatherEthnicityID INNER JOIN udfOHFTCYPLACIHAC ON PicklistColumnName=udfOHFTCYPLACIHAC.MotherEthnicityID INNER JOIN udfOHFTCYPLACIHAYP ON PicklistColumnName=udfOHFTCYPLACIHAYP.EthnicityID INNER JOIN udfOHFTCYPLACIHAYP ON PicklistColumnName=udfOHFTCYPLACIHAYP.FatherEthnicityID INNER JOIN udfOHFTCYPLACIHAYP ON PicklistColumnName=udfOHFTCYPLACIHAYP.MotherEthnicityID INNER JOIN udfOHFTCYPLACRHAC ON PicklistColumnName=udfOHFTCYPLACRHAC.FatherEthnicityID INNER JOIN udfOHFTCYPLACRHAC ON PicklistColumnName=udfOHFTCYPLACRHAC.MotherEthnicityID