使用STUFF过滤掉视图文件中的项目

时间:2017-10-02 05:50:01

标签: sql sql-server view

这是我的视图文件。我需要将joint_list过滤到joint_list NOT IN(' 0')。任何人都可以帮我如何过滤掉它。这是代码的输出。 view files

 SELECT  CUSTID,SPAID,STUFF((SELECT ', ' + CAST(RTrim(user1) AS VARCHAR(Max)) [text()]
 FROM xtcwt_JointAcc(nolock)   
 WHERE CUSTID = t.custid and SPAID = t.SPAID and Status='A'
 FOR XML PATH(''), TYPE) 
 .value('.','NVARCHAR(MAX)'),1,2,' ') joint_list  
 FROM xtcwt_JointAcc t with(nolock)  
 GROUP BY CUSTID,SPAID

谢谢!!

1 个答案:

答案 0 :(得分:1)

将您的查询放入sub-select/CTE并过滤joint_list外部

SELECT *
FROM   (SELECT CUSTID,
               SPAID,
               Stuff((SELECT ', ' + Cast(Rtrim(user1) AS VARCHAR(Max)) [text()]
                      FROM   xtcwt_JointAcc(nolock)
                      WHERE  CUSTID = t.custid
                             AND SPAID = t.SPAID
                             AND Status = 'A'
                      FOR XML PATH(''), TYPE) .value('.', 'NVARCHAR(MAX)'), 1, 2, ' ') joint_list
        FROM   xtcwt_JointAcc t WITH(nolock)
        GROUP  BY CUSTID,
                  SPAID) a
WHERE  joint_list <> '0'