场景:我有一张桌子返回无法出售我的水果的市场。最多有4种不同的条件将水果排除在市场之外。
我的代码是:
SELECT DISTINCT RPin, Variety, SubDivisionId, BlockId
, STUFF((
SELECT ',' + CAST(ClearanceID as varchar)
FROM [RPin_ExclusionT] t1
where t1.RPin = t2.RPin AND t1.Variety = t2.Variety AND t1.SubDivisionId = t2.SubDivisionId AND t1.BlockId = t2.BlockId
order by t1.ClearanceID
FOR XML PATH('')
), 1, 1, '') AS Clearance
FROM [RPin_ExclusionT] t2
例如:
Rpin Variety Subdivision Block Clearance
1234 039 B A RUS
1234 039 B A CHN
会导致:
Rpin Variety Subdivision Block Clearance
1234 039 B A RUS, CHN
如果每个字段都有数据,我的代码就可以工作。有时候,间隙可能只有一个RPin,其他所有内容都为NULL
Rpin Variety Subdivision Block Clearance
1234 039 B A RUS
1234 039 B A CHN
1234 NULL NULL NULL JAP
1234 NULL NULL NULL TWN
我是要返回与以前相同的结果
Rpin Variety Subdivision Block Clearance
1234 039 B A RUS, CHN
1234 NULL NULL NULL JAP, TWN
但是,任何地方任何列中都有NULL值都会导致我的清除列中有NULL值。
Rpin Variety Subdivision Block Clearance
1234 039 B A RUS, CHN
1234 NULL NULL NULL NULL
我充其量只是SQL的中级课程,不能完全像上面那样调整查询条件……任何指针将不胜感激。
答案 0 :(得分:4)
我更喜欢在子查询之前执行.navbar>.container-fluid {
display: block;
padding-right: 0px;
}
nav.navbar .collapse {
display: inline-block;
font-size: 1.2em;
position: relative;
top: -140px;
float: right;
margin: 15px 15px 8px 0;
/*padding: 10px;*/
background-color: transparent;
background-image: none;
border: 1px solid transparent;
border-radius: 4px;
-moz-box-shadow: none;
-webkit-box-shadow: none;
box-shadow: none;
overflow-x: visible;
padding-right: 15px;
padding-left: 15px;
padding-bottom: 10px;
border-top: none;
-webkit-overflow-scrolling: touch;
}
.menu-main-menu-container {
position: relative;
right: 80px;
}
。您需要distinct
安全的比较:
NULL
答案 1 :(得分:1)
假设所有字段都是varchars-请尝试以下操作:
SELECT DISTINCT RPin, Variety, SubDivisionId, BlockId
,STUFF((
SELECT ',' + CAST(ClearanceID AS VARCHAR(8000))
FROM [RPin_ExclusionT] t1
WHERE COALESCE(t1.RPin,'') = COALESCE(t2.RPin,'')
AND COALESCE(t1.Variety,'') = COALESCE(t2.Variety,'')
AND COALESCE(t1.SubDivisionId,'') = COALESCE(t2.SubDivisionId,'')
AND COALESCE(t1.BlockId,'') = COALESCE(t2.BlockId,'')
ORDER BY t1.ClearanceID
FOR XML PATH('')
), 1, 1, '') AS [Clearance]
FROM [RPin_ExclusionT] t2