简体   繁体   中英

Comparing Column Name in one Table with Column Data in another table in SQL Server

I am trying to compare column names which exist as a part of column values in tblFactorDefinition with matching column names with actual values in tblConstituent to find discrepancy between this two tables.

I am not getting the expected output, even though discrepancy does exists. I'm posting db schema and sample data below with expected output:

Three tables below are tblFactorDefinition , tblConstituent and tblFamily :

FamilyID | FieldName  |  FactorDefinition  |  PropertyTypeID
---------+------------+--------------------+----------------
10216    |  Factor1   |  901               |  300
10216    |  Factor2   |  901               |  305
10216    |  Factor3   |  901               |  310


ConstituentID | FamilyID |  ListingID | Factor1 | Factor2 | Factor3 | Factor9
--------------+----------+------------+---------+---------+---------+---------
1101          | 10216    |    1       |  0.1    | NULL    |  0.5    |   1.0
1105          | 10216    |    2       |  0.1    | 0.3     |  0.5    |   1.0
1108          | 10216    |    5       |  0.45   | 0.42    |  NULL   |   1.0


FamilyID | OpenDate
---------+------------
10216    | 2016-05-16

Expected output is shown here below:

FamilyID  |  FieldName   |  ConstituentID
----------+--------------+---------------
10216     |   Factor2    |  1101
10216     |   Factor3    |  1108

This is the query and I'm not getting the logic right hence it's returning nothing.

SELECT
    T.FamilyID,
    C.COLUMN_NAME,
    T.ConstituentID
FROM 
    SolaDBServer..tblConstituent T
INNER JOIN 
    INFORMATION_SCHEMA.COLUMNS C ON T.FamilyID = C.COLUMN_NAME 
                                 AND C.TABLE_NAME = 'tblFactorDefinition'  
                                 AND T.FamilyID = 10216
LEFT OUTER JOIN 
    SolaDBServer..tblConstituent tc ON tc.FamilyID = T.FamilyID 
INNER JOIN 
    SolaDBServer..tblFamily tf ON tf.FamilyID = tc.FamilyID  
                               AND tf.OpenDate = CAST(GETDATE() AS DATE)
WHERE 
    C.COLUMN_NAME = 'FieldName'

Any help appreciated with this?

Thanks.

You can use UNPIVOT

Please note I used IIF function in the query. (You need SQL Server 2012 or above version)

If you use the older version, please use the case statements to replace them.

Try this:

select a.FamilyID,a.FieldName,a.ConstituentID from 
(
select FamilyID,FieldName, ConstituentID, indicator
from 
(select c.ConstituentID,c.FamilyID
,iif(factor1 is null,1,0) as Factor1  --indicator for null
,iif(factor2 is null,1,0) as Factor2
,iif(factor3 is null,1,0) as Factor3
,iif(factor9 is null,1,0) as Factor9
from tblConstituent c
join tblFamily f
on f.FamilyID = c.FamilyID
where f.OpenDate = cast (getdate() as date)
)p
unpivot
(Indicator for FieldName
 in ([Factor1],[Factor2],[Factor3],[Factor9])
 ) as unpvt    
) a
join tblFactorDefinition b  --check if their factor(s) exist for specific ID
on a.FamilyID = b.FamilyID
and a.FieldName = b.FieldName 
where a.Indicator = 1

Test Result (I added one more row with different FamilyID):

DB<>Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM