在同一列上执行连接的任何其他方法?

时间:2014-11-14 07:43:51

标签: sql-server-2008

我只是在重写我的问题,因为这个答案对我不起作用。我有一个存储过程如下:

--GetAllFilteredProjects        
Alter PROCEDURE [GetAllFilteredProjects]                              
 @UserId Int,                                    
 @ServerGroupId int,                                
 @ProjectIDs  UDT_ProjectIDs Readonly,                                
 @ProjectDesc NVARCHAR(2000),                            
 @TopRecords INT,                  
 @StartProjectID INT,                
 @CustomerGroupId INT,            
 @SearchStates dbo.[UDT_SearchStates] Readonly                                  
AS                                    
BEGIN                                    
 SET NOCOUNT ON;                   
 DECLARE @StartProjectNumber INT                                  
 DECLARE @AcceptUnAcceptFilterNumberv INT 
 DECLARE @IgnoreRestrictedNumberv INT                                   
 DECLARE @SearchProjectIDsCount INT                                
 DECLARE @SearchProjectDesc VARCHAR  
 SET @IgnoreRestrictedNumberv=0                              
 SET @AcceptUnAcceptFilterNumberv =0                                 



select @AcceptUnAcceptFilterNumberv = COUNT(PropertyName)                                    
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
and PropertyName in('StartProjectIDFilter')                                    
where USERID = @UserId                        



 SET @ProjectDesc = REPLACE(@ProjectDesc, '*', '%')                                    
 SET @ProjectDesc = REPLACE(@ProjectDesc, '?', '_')                                   

  Print(@ProjectDesc)                              
 Select @SearchProjectIDsCount=count(*) from @ProjectIDs;                                
 select @AcceptUnAcceptFilterNumberv = COUNT(PropertyName)                                    
 from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
  and PropertyName in('ProjectFilterUnAccept','ProjectFilterAccept')                                    
 where USERID = @UserId                                    

 IF(@AcceptUnAcceptFilterNumberv = 1)                                    
  BEGIN                                    
   select @AcceptUnAcceptFilterNumberv = COUNT(PropertyName)                                    
   from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
   and PropertyName in('ProjectFilterAccept')                                    
   where USERID = @UserId                                    

   IF @AcceptUnAcceptFilterNumberv = 0                                    
    BEGIN                                    
     select @AcceptUnAcceptFilterNumberv = COUNT(PropertyName)                                    
     from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
     and PropertyName in('ProjectFilterUnAccept')                                    
     where USERID = @UserId                                    

     IF(@AcceptUnAcceptFilterNumberv = 1) -- UnAccepted                                    
      BEGIN                                    
       SET @AcceptUnAcceptFilterNumberv = 3                                    
      END                                    
    END                                    
  END   

  select @IgnoreRestrictedNumberv = COUNT(PropertyName)                                    
 from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
  and PropertyName in('ProjectFilterIgnoreRestricted')                                    
 where USERID = @UserId                                   

 IF OBJECT_ID('TEMPDB..#PROJECTS') IS NOT NULL DROP TABLE #PROJECTS                                    

 CREATE TABLE #PROJECTS                                    
 (                                    
  TMP_PROJECT_ID INT,                                    
  TMP_SERVERGROUP_ID INT,                                    
  TMP_DESCRIPTION NVARCHAR(1000),                          
  TMP_PROJECT_STATE_ID INT,                                    
  TMP_ACCEPTED_STATE_ID INT,      
  TMP_ISRESTRICTED_ID INT                                    
 )                                    

 IF OBJECT_ID('TEMPDB..#SELECTED_STATES') IS NOT NULL DROP TABLE #SELECTED_STATES                                    

 CREATE TABLE #SELECTED_STATES           
 (                                    
  TMP_PREFSET_STATEID INT                                    
 )                                    

 -- All the project accepted for server group and unaccepted for server group which are promoted to                                    
 -- Validation or Pilot or Factory states                           
  IF @IgnoreRestrictedNumberv=1
  BEGIN                           
  INSERT INTO #PROJECTS                                    
  SELECT  p.projectid,servergroupid,p.description,p.StateId AS ProjectState, pa.stateid ,p.IsRestricted                                    
  FROM v_Project p left outer join ProjectAcception pa ON p.ProjectId = pa.ProjectId and pa.ServerGroupId=@ServerGroupId              
  WHERE P.CustomerGroupId = @CustomerGroupId AND P.StateId IN(Select StateID From @SearchStates) and P.StateId in (select ss.AFPStateId from ServerGroupStateSettings ss where ss.servergroupid=@ServerGroupId)                                
  --added by shankar, regarding the filtering functionality                                 
  --in projects tab of a server group                                
  and (@SearchProjectIDsCount =0 Or p.projectid in                                
  (1673))                                
  AND (Upper(p.Description)  LIKE '%' +Upper(@ProjectDesc) + '%')                    
  AND p.projectid>=@StartProjectID    
  AND p.IsRestricted = 0  
  END

  ELSE
  BEGIN
  INSERT INTO #PROJECTS                                    
  SELECT  p.projectid,servergroupid,p.description,p.StateId AS ProjectState, pa.stateid ,p.IsRestricted                                    
  FROM v_Project p left outer join ProjectAcception pa ON p.ProjectId = pa.ProjectId and pa.ServerGroupId=@ServerGroupId                   
  WHERE P.CustomerGroupId = @CustomerGroupId AND P.StateId IN(Select StateID From @SearchStates) and P.StateId in (select ss.AFPStateId from ServerGroupStateSettings ss where ss.servergroupid=@ServerGroupId)                                
  --added by shankar, regarding the filtering functionality                                 
  --in projects tab of a server group                                
  and (@SearchProjectIDsCount =0 Or p.projectid in                                
  (1673))                                
  AND (Upper(p.Description)  LIKE '%' +Upper(@ProjectDesc) + '%')                    
  AND p.projectid>=@StartProjectID                       
  END                           

 -- State selected by user to filter the projects                                    
 INSERT INTO #SELECTED_STATES                                    
 select ap.AFPStateId                                    
 from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
   inner join AFPState ap on 'ProjectFilter' + ap.AFPStateDesc = PropertyName                                     
 where USERID = @UserId                                    

 IF @AcceptUnAcceptFilterNumberv = 1  -- Accepted                                    
  BEGIN                                    
   SELECT TOP(@TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,                                    
     (case                                    
      when TMP_PROJECT_STATE_ID = 1 then 'Development'                                    
      when TMP_PROJECT_STATE_ID = 2 then 'Validation'                                    
      when TMP_PROJECT_STATE_ID = 3 then 'Pilot'                                    
      when TMP_PROJECT_STATE_ID = 4 then 'Factory'                                    
     end) as State,                                     
     (case [1] when 2 then 1                                    
       else 0                                    
     end) as Validation,                                    
     (case                                    
      when [1] = 3 then 1                                    
      when [2] = 3 then 1                                    
 else 0                                    
     end) as Pilot,                                    
     (case                                    
      when [1] = 4 then 1                                    
      when [2] = 4 then 1                                    
      when [3] = 4 then 1                                    
      else 0                                    
     end) as Factory,PL.LockedBy,PA.CreatedBy,      
     (CASE           
     WHEN TMP_ISRESTRICTED_ID = 0 THEN 1          
     ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = @ServerGroupId )          
     END          
     )AS IsUnRestrictedServerGroup                                       
   FROM (                                    
     SELECT #PROJECTS.TMP_PROJECT_ID,                                    
       #PROJECTS.TMP_DESCRIPTION,                                     
       #PROJECTS.TMP_SERVERGROUP_ID,                                    
       #PROJECTS.TMP_PROJECT_STATE_ID,                                    
       #PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM, #PROJECTS.TMP_ISRESTRICTED_ID                               
     FROM #PROJECTS                                     
     WHERE #PROJECTS.TMP_PROJECT_ID IN                                    
       (                                    
        SELECT DISTINCT #PROJECTS.TMP_PROJECT_ID                                    
        FROM #PROJECTS                                     
        GROUP BY #PROJECTS.TMP_PROJECT_ID HAVING MAX(#PROJECTS.TMP_ACCEPTED_STATE_ID) IN                                    
        --WHERE #PROJECTS.TMP_ACCEPTED_STATE_ID IN                                    
            (                                    
             SELECT #SELECTED_STATES.TMP_PREFSET_STATEID                                     
             FROM #SELECTED_STATES                                    
            )                                    
       )                                    
    ) A                                    
   PIVOT                                   
   (                                    
    MAX(TMP_ACCEPTED_STATE_ID)                                    
    FOR ROWNUM IN ([1],[2],[3],[4])                                    
   )B                      
   LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID                       
   LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID                                
  END                                    
 ELSE IF @AcceptUnAcceptFilterNumberv = 2 -- Both                                    
  BEGIN                                    
   SELECT TOP(@TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,                                    
     (case                                    
      when TMP_PROJECT_STATE_ID = 1 then 'Development'                                    
      when TMP_PROJECT_STATE_ID = 2 then 'Validation'                                    
      when TMP_PROJECT_STATE_ID = 3 then 'Pilot'                                    
      when TMP_PROJECT_STATE_ID = 4 then 'Factory'                                    
     end) as State,                                     
     (case                                     
      when [1]=2 then 1                                    
       else 0                                    
     end) as Validation,                                    
     (case                                    
      when [1] = 3 then 1                                    
      when [2] = 3 then 1                                    
      else 0                                    
     end) as Pilot,                                    
     (case                                    
      when [1] = 4 then 1                                    
      when [2] = 4 then 1                                    
      when [3] = 4 then 1                                    
      else 0                                    
     end) as Factory,PL.LockedBy,PA.CreatedBy,      
     (CASE           
     WHEN TMP_ISRESTRICTED_ID = 0 THEN 1          
     ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = @ServerGroupId )          
     END          
     )AS IsUnRestrictedServerGroup                                          
   FROM (                                    
      SELECT #PROJECTS.TMP_PROJECT_ID,                                    
        #PROJECTS.TMP_DESCRIPTION,                                     
        #PROJECTS.TMP_SERVERGROUP_ID,                                    
    #PROJECTS.TMP_PROJECT_STATE_ID,                                    
        #PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM , #PROJECTS.TMP_ISRESTRICTED_ID                                
     FROM #PROJECTS                                     
     WHERE #PROJECTS.TMP_PROJECT_ID in                                    
      (                                     
       SELECT DISTINCT PJ.TMP_PROJECT_ID                                    
       FROM #PROJECTS PJ                                    
       --WHERE isnull(PJ.TMP_ACCEPTED_STATE_ID,0) <=                                    
       GROUP BY PJ.TMP_PROJECT_ID HAVING MAX(isnull(PJ.TMP_ACCEPTED_STATE_ID,0)) <=                                    
         (                                    
   SELECT max(#SELECTED_STATES.TMP_PREFSET_STATEID)                                    
          FROM #SELECTED_STATES                                    
         )                                    
      )                                    
    ) A                                    
   PIVOT                                    
   (                                    
    MAX(TMP_ACCEPTED_STATE_ID)                                    
    FOR ROWNUM IN ([1],[2],[3],[4])                                    
   )B                      
   LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID                        
   LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID                                            
  END                                    
 ELSE IF @AcceptUnAcceptFilterNumberv = 3 -- UnAccepted                                    
  BEGIN                                    
  SELECT TOP(@TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,                                    
     (case                                    
      when TMP_PROJECT_STATE_ID = 1 then 'Development'                                    
      when TMP_PROJECT_STATE_ID = 2 then 'Validation'                                    
      when TMP_PROJECT_STATE_ID = 3 then 'Pilot'                                    
      when TMP_PROJECT_STATE_ID = 4 then 'Factory'                                    
     end) as State,                                
     (case [1] when 2 then 1                                    
       else 0                                    
     end) as Validation,                                    
     (case                                    
      when [1] = 3 then 1                                    
      when [2] = 3 then 1                                    
      else 0                                    
     end) as Pilot,               
     (case                                    
      when [1] = 4 then 1                                    
      when [2] = 4 then 1                                    
      when [3] = 4 then 1                                    
      else 0                                    
     end) as Factory,PL.LockedBy,PA.CreatedBy,      
     (CASE           
     WHEN TMP_ISRESTRICTED_ID = 0 THEN 1          
     ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = @ServerGroupId )          
     END          
     )AS IsUnRestrictedServerGroup                                          
   FROM (                                  
     SELECT #PROJECTS.TMP_PROJECT_ID,                                    
      #PROJECTS.TMP_DESCRIPTION,                          
      #PROJECTS.TMP_SERVERGROUP_ID,                 
      #PROJECTS.TMP_PROJECT_STATE_ID,                                    
      #PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM , #PROJECTS.TMP_ISRESTRICTED_ID                                 
     FROM #PROJECTS                                    
     WHERE #PROJECTS.TMP_PROJECT_ID IN                              
       (                                    
        SELECT DISTINCT p.TMP_PROJECT_ID                                     
        FROM #PROJECTS p                                    
        WHERE (SELECT max(ISNULL(PJ.TMP_ACCEPTED_STATE_ID,0))                                    
          FROM #PROJECTS PJ                                    
          GROUP BY PJ.TMP_PROJECT_ID HAVING PJ.TMP_PROJECT_ID = p.TMP_PROJECT_ID                                    
            )                                           <                                      
           (                                    
          SELECT MAX(#SELECTED_STATES.TMP_PREFSET_STATEID)                                    
          FROM #SELECTED_STATES                                    
            )                                    
       )                                    
    ) A                                    
   PIVOT                                    
   (                                    
    MAX(TMP_ACCEPTED_STATE_ID)                                    
    FOR ROWNUM IN ([1],[2],[3],[4])                                    
   )B                      
   LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID                        
   LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID                             
  END           
  END 

现在,当我将值插入到我的表项目中时,我的条件是p.IsRestrictedID = 0。 在这里,我还有一件事我需要考虑。我需要插入具有IsRestricted = 1的项目,这可以在此语句返回1

时执行
SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = @ServerGroupId

表示当上面的select查询返回1时,我需要考虑IsRestricted = 1的项目。

我想将此添加到插入到项目查询中。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

只需在每个使用列名称的位置添加表别名的前缀,以防止出现不明确的列错误:

SELECT 
     p.projectid, 
     pa.servergroupid,        -- Here I added "pa." as a prefix
     p.description,
     . . .

答案 1 :(得分:0)

我得到了解决方案,我只需要在插入Projects时添加OR条件。

修改后的内容如下:

--GetAllFilteredProjects        
Alter PROCEDURE [GetAllFilteredProjects]                              
 @UserId Int,                                    
 @ServerGroupId int,                                
 @ProjectIDs  UDT_ProjectIDs Readonly,                                
 @ProjectDesc NVARCHAR(2000),                            
 @TopRecords INT,                  
 @StartProjectID INT,                
 @CustomerGroupId INT,            
 @SearchStates dbo.[UDT_SearchStates] Readonly                                  
AS                                    
BEGIN                                    
 SET NOCOUNT ON;                   
 DECLARE @StartProjectNumber INT                                  
 DECLARE @AcceptUnAcceptFilterNumberv INT 
 DECLARE @IgnoreRestrictedNumberv INT                                   
 DECLARE @SearchProjectIDsCount INT                                
 DECLARE @SearchProjectDesc VARCHAR  
 SET @IgnoreRestrictedNumberv=0                              
 SET @AcceptUnAcceptFilterNumberv =0                                 



select @AcceptUnAcceptFilterNumberv = COUNT(PropertyName)                                    
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
and PropertyName in('StartProjectIDFilter')                                    
where USERID = @UserId                        



 SET @ProjectDesc = REPLACE(@ProjectDesc, '*', '%')                                    
 SET @ProjectDesc = REPLACE(@ProjectDesc, '?', '_')                                   

  Print(@ProjectDesc)                              
 Select @SearchProjectIDsCount=count(*) from @ProjectIDs;                                
 select @AcceptUnAcceptFilterNumberv = COUNT(PropertyName)                                    
 from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
  and PropertyName in('ProjectFilterUnAccept','ProjectFilterAccept')                                    
 where USERID = @UserId                                    

 IF(@AcceptUnAcceptFilterNumberv = 1)                                    
  BEGIN                                    
   select @AcceptUnAcceptFilterNumberv = COUNT(PropertyName)                                    
   from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
   and PropertyName in('ProjectFilterAccept')                                    
   where USERID = @UserId                                    

   IF @AcceptUnAcceptFilterNumberv = 0                                    
    BEGIN                                    
     select @AcceptUnAcceptFilterNumberv = COUNT(PropertyName)                                    
     from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
     and PropertyName in('ProjectFilterUnAccept')                                    
     where USERID = @UserId                                    

     IF(@AcceptUnAcceptFilterNumberv = 1) -- UnAccepted                                    
      BEGIN                                    
       SET @AcceptUnAcceptFilterNumberv = 3                                    
      END                                    
    END                                    
  END   

  select @IgnoreRestrictedNumberv = COUNT(PropertyName)                                    
 from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
  and PropertyName in('ProjectFilterIgnoreRestricted')                                    
 where USERID = @UserId                                   

 IF OBJECT_ID('TEMPDB..#PROJECTS') IS NOT NULL DROP TABLE #PROJECTS                                    

 CREATE TABLE #PROJECTS                                    
 (                                    
  TMP_PROJECT_ID INT,                                    
  TMP_SERVERGROUP_ID INT,                                    
  TMP_DESCRIPTION NVARCHAR(1000),                          
  TMP_PROJECT_STATE_ID INT,                                    
  TMP_ACCEPTED_STATE_ID INT,      
  TMP_ISRESTRICTED_ID INT                                    
 )                                    

 IF OBJECT_ID('TEMPDB..#SELECTED_STATES') IS NOT NULL DROP TABLE #SELECTED_STATES                                    

 CREATE TABLE #SELECTED_STATES           
 (                                    
  TMP_PREFSET_STATEID INT                                    
 )                                    

 -- All the project accepted for server group and unaccepted for server group which are promoted to                                    
 -- Validation or Pilot or Factory states                           
  IF @IgnoreRestrictedNumberv=1
  BEGIN                           
  INSERT INTO #PROJECTS                                    
  SELECT  p.projectid, servergroupid,p.description,p.StateId AS ProjectState, pa.stateid ,p.IsRestricted                                    
  FROM v_Project p left outer join ProjectAcception pa ON p.ProjectId = pa.ProjectId and pa.ServerGroupId=@ServerGroupId  
  WHERE P.CustomerGroupId = @CustomerGroupId AND P.StateId IN(Select StateID From @SearchStates) and P.StateId in (select ss.AFPStateId from ServerGroupStateSettings ss where ss.servergroupid=@ServerGroupId)                                
  --added by shankar, regarding the filtering functionality                                 
  --in projects tab of a server group                                
  and (@SearchProjectIDsCount =0 Or p.projectid in                                
  (1673))                                
  AND (Upper(p.Description)  LIKE '%' +Upper(@ProjectDesc) + '%')                    
  AND p.projectid>=@StartProjectID    
  AND p.IsRestricted = 0  
  OR p.IsRestricted IN (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = p.projectid AND PR.ServerGroupId = @ServerGroupId )          
  END

  ELSE
  BEGIN
  INSERT INTO #PROJECTS                                    
  SELECT  p.projectid,servergroupid,p.description,p.StateId AS ProjectState, pa.stateid ,p.IsRestricted                                    
  FROM v_Project p left outer join ProjectAcception pa ON p.ProjectId = pa.ProjectId and pa.ServerGroupId=@ServerGroupId                   
  WHERE P.CustomerGroupId = @CustomerGroupId AND P.StateId IN(Select StateID From @SearchStates) and P.StateId in (select ss.AFPStateId from ServerGroupStateSettings ss where ss.servergroupid=@ServerGroupId)                                
  --added by shankar, regarding the filtering functionality                                 
  --in projects tab of a server group                                
  and (@SearchProjectIDsCount =0 Or p.projectid in                                
  (1673))                                
  AND (Upper(p.Description)  LIKE '%' +Upper(@ProjectDesc) + '%')                    
  AND p.projectid>=@StartProjectID                       
  END                           

 -- State selected by user to filter the projects                                    
 INSERT INTO #SELECTED_STATES                                    
 select ap.AFPStateId                                    
 from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID                                    
   inner join AFPState ap on 'ProjectFilter' + ap.AFPStateDesc = PropertyName                                     
 where USERID = @UserId                                    

 IF @AcceptUnAcceptFilterNumberv = 1  -- Accepted                                    
  BEGIN                                    
   SELECT TOP(@TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,                                    
     (case                                    
      when TMP_PROJECT_STATE_ID = 1 then 'Development'                                    
      when TMP_PROJECT_STATE_ID = 2 then 'Validation'                                    
      when TMP_PROJECT_STATE_ID = 3 then 'Pilot'                                    
      when TMP_PROJECT_STATE_ID = 4 then 'Factory'                                    
     end) as State,                                     
     (case [1] when 2 then 1                                    
       else 0                                    
     end) as Validation,                                    
     (case                                    
      when [1] = 3 then 1                                    
      when [2] = 3 then 1                                    
 else 0                                    
     end) as Pilot,                                    
     (case                                    
      when [1] = 4 then 1                                    
      when [2] = 4 then 1                                    
      when [3] = 4 then 1                                    
      else 0                                    
     end) as Factory,PL.LockedBy,PA.CreatedBy,      
     (CASE           
     WHEN TMP_ISRESTRICTED_ID = 0 THEN 1          
     ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = @ServerGroupId )          
     END          
     )AS IsUnRestrictedServerGroup                                       
   FROM (                                    
     SELECT #PROJECTS.TMP_PROJECT_ID,                                    
       #PROJECTS.TMP_DESCRIPTION,                                     
       #PROJECTS.TMP_SERVERGROUP_ID,                                    
       #PROJECTS.TMP_PROJECT_STATE_ID,                                    
       #PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM, #PROJECTS.TMP_ISRESTRICTED_ID                               
     FROM #PROJECTS                                     
     WHERE #PROJECTS.TMP_PROJECT_ID IN                                    
       (                                    
        SELECT DISTINCT #PROJECTS.TMP_PROJECT_ID                                    
        FROM #PROJECTS                                     
        GROUP BY #PROJECTS.TMP_PROJECT_ID HAVING MAX(#PROJECTS.TMP_ACCEPTED_STATE_ID) IN                                    
        --WHERE #PROJECTS.TMP_ACCEPTED_STATE_ID IN                                    
            (                                    
             SELECT #SELECTED_STATES.TMP_PREFSET_STATEID                                     
             FROM #SELECTED_STATES                                    
            )                                    
       )                                    
    ) A                                    
   PIVOT                                   
   (                                    
    MAX(TMP_ACCEPTED_STATE_ID)                                    
    FOR ROWNUM IN ([1],[2],[3],[4])                                    
   )B                      
   LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID                       
   LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID                                
  END                                    
 ELSE IF @AcceptUnAcceptFilterNumberv = 2 -- Both                                    
  BEGIN                                    
   SELECT TOP(@TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,                                    
     (case                                    
      when TMP_PROJECT_STATE_ID = 1 then 'Development'                                    
      when TMP_PROJECT_STATE_ID = 2 then 'Validation'                                    
      when TMP_PROJECT_STATE_ID = 3 then 'Pilot'                                    
      when TMP_PROJECT_STATE_ID = 4 then 'Factory'                                    
     end) as State,                                     
     (case                                     
      when [1]=2 then 1                                    
       else 0                                    
     end) as Validation,                                    
     (case                                    
      when [1] = 3 then 1                                    
      when [2] = 3 then 1                                    
      else 0                                    
     end) as Pilot,                                    
     (case                                    
      when [1] = 4 then 1                                    
      when [2] = 4 then 1                                    
      when [3] = 4 then 1                                    
      else 0                                    
     end) as Factory,PL.LockedBy,PA.CreatedBy,      
     (CASE           
     WHEN TMP_ISRESTRICTED_ID = 0 THEN 1          
     ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = @ServerGroupId )          
     END          
     )AS IsUnRestrictedServerGroup                                          
   FROM (                                    
      SELECT #PROJECTS.TMP_PROJECT_ID,                                    
        #PROJECTS.TMP_DESCRIPTION,                                     
        #PROJECTS.TMP_SERVERGROUP_ID,                                    
    #PROJECTS.TMP_PROJECT_STATE_ID,                                    
        #PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM , #PROJECTS.TMP_ISRESTRICTED_ID                                
     FROM #PROJECTS                                     
     WHERE #PROJECTS.TMP_PROJECT_ID in                                    
      (                                     
       SELECT DISTINCT PJ.TMP_PROJECT_ID                                    
       FROM #PROJECTS PJ                                    
       --WHERE isnull(PJ.TMP_ACCEPTED_STATE_ID,0) <=                                    
       GROUP BY PJ.TMP_PROJECT_ID HAVING MAX(isnull(PJ.TMP_ACCEPTED_STATE_ID,0)) <=                                    
         (                                    
   SELECT max(#SELECTED_STATES.TMP_PREFSET_STATEID)                                    
          FROM #SELECTED_STATES                                    
         )                                    
      )                                    
    ) A                                    
   PIVOT                                    
   (                                    
    MAX(TMP_ACCEPTED_STATE_ID)                                    
    FOR ROWNUM IN ([1],[2],[3],[4])                                    
   )B                      
   LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID                        
   LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID                                            
  END                                    
 ELSE IF @AcceptUnAcceptFilterNumberv = 3 -- UnAccepted                                    
  BEGIN                                    
  SELECT TOP(@TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,                                    
     (case                                    
      when TMP_PROJECT_STATE_ID = 1 then 'Development'                                    
      when TMP_PROJECT_STATE_ID = 2 then 'Validation'                                    
      when TMP_PROJECT_STATE_ID = 3 then 'Pilot'                                    
      when TMP_PROJECT_STATE_ID = 4 then 'Factory'                                    
     end) as State,                                
     (case [1] when 2 then 1                                    
       else 0                                    
     end) as Validation,                                    
     (case                                    
      when [1] = 3 then 1                                    
      when [2] = 3 then 1                                    
      else 0                                    
     end) as Pilot,               
     (case                                    
      when [1] = 4 then 1                                    
      when [2] = 4 then 1                                    
      when [3] = 4 then 1                                    
      else 0                                    
     end) as Factory,PL.LockedBy,PA.CreatedBy,      
     (CASE           
     WHEN TMP_ISRESTRICTED_ID = 0 THEN 1          
     ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = @ServerGroupId )          
     END          
     )AS IsUnRestrictedServerGroup                                          
   FROM (                                  
     SELECT #PROJECTS.TMP_PROJECT_ID,                                    
      #PROJECTS.TMP_DESCRIPTION,                          
      #PROJECTS.TMP_SERVERGROUP_ID,                 
      #PROJECTS.TMP_PROJECT_STATE_ID,                                    
      #PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM , #PROJECTS.TMP_ISRESTRICTED_ID                                 
     FROM #PROJECTS                                    
     WHERE #PROJECTS.TMP_PROJECT_ID IN                              
       (                                    
        SELECT DISTINCT p.TMP_PROJECT_ID                                     
        FROM #PROJECTS p                                    
        WHERE (SELECT max(ISNULL(PJ.TMP_ACCEPTED_STATE_ID,0))                                    
          FROM #PROJECTS PJ                                    
          GROUP BY PJ.TMP_PROJECT_ID HAVING PJ.TMP_PROJECT_ID = p.TMP_PROJECT_ID                                    
            )                                           <                                      
           (                                    
          SELECT MAX(#SELECTED_STATES.TMP_PREFSET_STATEID)                                    
          FROM #SELECTED_STATES                                    
            )                                    
       )                                    
    ) A                                    
   PIVOT                                    
   (                                    
    MAX(TMP_ACCEPTED_STATE_ID)                                    
    FOR ROWNUM IN ([1],[2],[3],[4])                                    
   )B                      
   LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID                        
   LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID                             
  END           
  END