2017-03-09 54 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