表中特定列的值的存在

时间:2015-12-23 06:42:10

标签: sql

我有两张表DriverApplication

Driver表格中,特定parttypeWeightage大于100且小于600,表name中有一个属性application和值应用程序表中的值不应为NULL或空白。

如果它为空或NULL,那么该记录应该在输出中

DECLARE @DRIVER TABLE
(
PartType VARCHAR (50),
AttributeName VARCHAR (50),
Weightage VARCHAR (50)
)


INSERT @DRIVER


SELECT 'Air Filter',    'Shape',    '100' UNION ALL
SELECT 'Fender Flare',  'Color Group',  '500' UNION ALL
SELECT 'Tonneau Cover', 'Lead Type',    '999' UNION ALL
SELECT 'Lug Bolt',  'Thread Size',  '100' 



DECLARE @application TABLE
(
Part_Number VARCHAR (50),   
Part_Type  VARCHAR (50),    
Description_1  VARCHAR (50) ,
Description_1_Value  VARCHAR (50),
Description_2    VARCHAR (50),
Description_2_Value  VARCHAR (50),
Description_3    VARCHAR (50),
Description_3_Value  VARCHAR (50)

)

Insert @application


SELECT 'AAA19-1405',    'Air Filter',   'Shape',    'Universal',    'Number 
Of Pieces', '2',    'Design',   'No Logo' Union All
SELECT 'ZORLYM12',  'Air Filter',   'Shape',    '',             'Number Of     
Pieces',    '4',    'Design',   'No Logo' Union All
SELECT 'AAA19-1508',    'Fender Flare', 'Type', 'Universal',    'Color 
Group', 'Red',  'Design',   'No Logo' Union All
SELECT 'NORFLEX89', 'Fender Flare', 'Type', 'Universal',    'Color Group',  
NULL,   'Design',   'No Logo' Union All
SELECT 'AAA19-2305',    'Lug Bolt', 'Type', 'Universal',    'Number Of 
Pieces',    '4',    'Thread Size',  'MAX' UNION ALL
SELECT 'BIOCONINSULIN', 'Lug Bolt', 'Type', 'Universal',    'Number Of 
Pieces',    '4',    'Thread Size',  NULL UNION ALL
SELECT 'BBB19-2305',    'Tonneau Cover','Lead Type',    NULL,   'Number Of 
Pieces',    '4',    'Thread Size',  NULL union all
SELECT 'XXXXXX',  'Air Filter',   'Shape',    'CATCH',             'Number  
Of Pieces',    '4',    'Design',   NULL Union All

预期输出

/*
AAA19-1405  Air Filter  Shape   Universal   Number Of Pieces    2   Design  No Logo-- here for part Type Air filter value not present for Shape
NORFLEX89   Fender Flare    Type    Universal   Color Group NULL    Design  No Logo-- COLOR GROUP IS NULL FOR Fender Flare
BIOCONINSULIN   Lug Bolt    Type    Universal   Number Of Pieces    4   Thread Size NULL-- Thread size is Null for part type Lug Bolt
*/

- 这不应出现在输出中,因为Weightage大于600

BBB19-2305  Tonneau Cover   Lead Type   NULL    Number Of Pieces    4   Thread Size NULL

非常感谢

代码我试过但没有给出结果

SELECT * FROM @DRIVER D
INNER JOIN
@application A
ON A.Part_Type = D.PartType
WHERE D.AttributeName in 
(
SELECT Description_1 , Description_2 , Description_3 FROM @APPLICATION

)

AND A.Description_1_Value IS NULL or A.Description_2_Value is null or     
A.A.Description_3_Value IS NULL

1 个答案:

答案 0 :(得分:1)

你需要尝试这样的事情:

SELECT * 
FROM @DRIVER D
INNER JOIN @application A ON A.Part_Type = D.PartType
WHERE 
    (D.AttributeName = a.Description_1 AND A.Description_1_Value IS NULL)
    OR
    (D.AttributeName = a.Description_2 AND A.Description_2_Value IS NULL)
    OR
    (D.AttributeName = a.Description_3 AND A.Description_3_Value IS NULL)
    AND d.Weightage BETWEEN 100 AND 600        

更新:

我得到的SELECT声明的输出以及OP提供的示例数据是:

enter image description here

和我不要看到任何带有" Air过滤器"的行不应该根据你的评论显示....请详细解释!