仅通过一个out字段从union中排除

时间:2010-02-17 05:43:10

标签: sql

SELECT A.ID_ListGroupParIzm, A.Name, 0 AS Point  
FROM CfgListGroupParIzm A, CfgIzmeritel B 
WHERE A.ID_TypeIzmerit = B.ID_TypeIzmerit   
AND B.ID_Izmerit=@ID_Izmerit AND A.ForRun=0 

UNION  

SELECT A.ID_ListGroupParIzm, (C.Name + ' ' + A.Name) AS Name, C.ID_IzmerPoint AS Point  
FROM CfgListGroupParIzm A, CfgIzmeritel B, CfgIzmerPoint C  
WHERE A.ID_TypeIzmerit = B.ID_TypeIzmerit  
AND B.ID_Izmerit=@ID_Izmerit AND A.ForRun=1  
AND C.ID_Izmerit=@ID_Izmerit

EXCEPT

SELECT ID_Group --, '' AS Name, 0 AS Point here I don't know their names and a Point :(
FROM TbUserGroup
WHERE ID_Izmerit=@ID_Izmerit AND
ID_User=@ID_User AND ID_Point=@ID_Point

有想法 - 使用Except但我需要知道它的所有字段(我只知道一个),所以我需要在ID_Group下从这个union节点中排除,除了: - /我正在考虑将它组合起来。

如何制作?或一些想法...或建议...

1 个答案:

答案 0 :(得分:2)

您可以尝试类似

的内容
SELECT  *
FROM    (
            SELECT  A.ID_ListGroupParIzm, 
                    A.Name, 
                    0 AS Point   
            FROM    CfgListGroupParIzm A, 
                    CfgIzmeritel B  
            WHERE   A.ID_TypeIzmerit = B.ID_TypeIzmerit    
            AND     B.ID_Izmerit=@ID_Izmerit 
            AND     A.ForRun=0  
            UNION   
            SELECT  A.ID_ListGroupParIzm, 
                    (C.Name + ' ' + A.Name) AS Name, 
                    C.ID_IzmerPoint AS Point   
            FROM    CfgListGroupParIzm A, 
                    CfgIzmeritel B, 
                    CfgIzmerPoint C   
            WHERE   A.ID_TypeIzmerit = B.ID_TypeIzmerit   
            AND     B.ID_Izmerit=@ID_Izmerit 
            AND     A.ForRun=1   
            AND     C.ID_Izmerit=@ID_Izmerit 
        ) sub 
WHERE   sub.ID_ListGroupParIzm NOT IN 
                (
                    SELECT  ID_Group 
                    FROM    TbUserGroup 
                    WHERE   ID_Izmerit=@ID_Izmerit 
                    AND     ID_User=@ID_User 
                    AND     ID_Point=@ID_Point 
                )