1

In my case I need to filter a file based on a ITSUser and its role.

The user can have 2 roles "A" and "B".I want only 1 File per 1 Row based on below conditions.

  • If StateFile has ITSUser exists in the file and has NO role "B" then --> Yes , No
  • Else If StateFile has ITSUser exists in the file and has role "B" then --> Yes , Yes
  • Else If StateFile has NO ITSUser exists in the file and has NO role "B" then --> No , No

Below is the query that i am using.

SELECT DISTINCT MSEF.StatefileID,
MSEF.StateFileName,
AFEE.SubmittedDate,
UNO.DisplayName AS SubmittedBy,
        (select DISTINCT (
    CASE WHEN (
     MSETET.ITSUserID = '99' 
                 AND ET.StateTeamName = N'Live')
            THEN 'Yes'
ELSE 'No'
    END)) AS CHICMAN,
    (select DISTINCT (
    CASE WHEN ( 

    MSETET.ITSUserID  IN( '99') 
          AND ET.StateTeamName  IN( N'Live') 
          AND MSETET.StateTeamID=ET.StateTeamID ) --AND MSEF.StateFileID != MSETET.StateFileID    
                 THEN 'Yes'
ELSE 'No'
    END)) AS CHICTeamIsInTheLive  
from MS.StateFile MSEF WITH (NOLOCK)
INNER JOIN EM.StateFileRem AFEE WITH (NOLOCK) ON MSEF.StateFileID = AFEE.StateFileID
INNER JOIN MS.StateTeamMember MSETM WITH (NOLOCK) ON MSETM.StateFileID=MSEF.StateFileID
INNER JOIN SOP.ITSUser UNO WITH (NOLOCK) ON AFEE.SubmittedByUserID = UNO.ITSUserID
INNER JOIN MS.StateTeamStateTeamMember MSETET ON MSETET.StateFileID=AFEE.StateFileID
INNER JOIN [MS].[StateTeam] ET WITH (NOLOCK) ON ET.StateFileID = MSETET.StateFileID
 ORDER BY
    MSEF.StateFileID 
GO 

My Current Output : enter image description here

My expected Output : enter image description here

enter image description here

Learning_Learning
  • 317
  • 1
  • 5
  • 18

1 Answers1

1

That should work:

select top 1 with ties
    StatefileID
    ,StateFileName
    ,SubmittedDate
    ,SubmittedBy
    ,CHICMAN
    ,CHICTeamIsInTheLive
from 
(
    SELECT DISTINCT MSEF.StatefileID,
    MSEF.StateFileName,
    AFEE.SubmittedDate,
    UNO.DisplayName AS SubmittedBy,
            (select DISTINCT (
        CASE WHEN (
         MSETET.ITSUserID = '99' 
                     AND ET.StateTeamName = N'Live')
                THEN 'Yes'
    ELSE 'No'
        END)) AS CHICMAN,
        (select DISTINCT (
        CASE WHEN ( 

        MSETET.ITSUserID  IN( '99') 
              AND ET.StateTeamName  IN( N'Live') 
              AND MSETET.StateTeamID=ET.StateTeamID ) --AND MSEF.StateFileID != MSETET.StateFileID    
                     THEN 'Yes'
    ELSE 'No'
        END)) AS CHICTeamIsInTheLive  
    from MS.StateFile MSEF WITH (NOLOCK)
    INNER JOIN EM.StateFileRem AFEE WITH (NOLOCK) ON MSEF.StateFileID = AFEE.StateFileID
    INNER JOIN MS.StateTeamMember MSETM WITH (NOLOCK) ON MSETM.StateFileID=MSEF.StateFileID
    INNER JOIN SOP.ITSUser UNO WITH (NOLOCK) ON AFEE.SubmittedByUserID = UNO.ITSUserID
    INNER JOIN MS.StateTeamStateTeamMember MSETET ON MSETET.StateFileID=AFEE.StateFileID
    INNER JOIN [MS].[StateTeam] ET WITH (NOLOCK) ON ET.StateFileID = MSETET.StateFileID
) x
where CHICMAN = 'Yes' or CHICTeamIsInTheLive = 'Yes'
order by row_number() over (partition by StatefileID order by CHICMAN desc, CHICTeamIsInTheLive desc)
  • Raphael, thanks for your time . I have edited my question.Can you also please help me on getting values where the Statefile has NO ITSUser and NO role "B". The out put should be No, No in this case. But what ever you mentioned above does worked for me perfect. Can you please help me on this too when NO ITSUser and NO role too.. Many Thanks. – Learning_Learning Sep 04 '17 at 06:56
  • I did try like , select DISTINCT ( CASE WHEN ( MSETET.ITSUserID = '99' OR MSETET.ITSUserID != '99' . It gives me all users but whether the other files has ITSUser or not it shows Yes to all . Please help me on this. I edited my question too. – Learning_Learning Sep 04 '17 at 09:46
  • 1
    I don't know if I fully understand what you are trying to do, but if a file has no ITSUser, then I would assume that there is no value in AFEE.SubmittedByUserID and then an inner join would drop the rows. If there is no SubmittedByUserID, then you should use a left outer join instead of an inner join for SOP.ITSUser. – Raphael Müllner Sep 04 '17 at 11:04
  • 1
    Upvoted and accepted your response as your response almost fixed my issue. Thanks a lot – Learning_Learning Sep 04 '17 at 16:55