Data Comparison

  • I have a table that contains permissions based on a group id. How would I go about building a script that would compare two groups and return only the records that do not match? Any help will be appreciated.

    Example:

    Group ID Item Permission

    123 D-Report Y

    123 G-Report Y

    134 D-Report Y

    134 G-Report N

    Output:

    Group ID Item Permission

    134 G-Report N

  • the trick is to join the table agaisnt itself.

    With MyCTE (GroupID,Item,Permission)

    AS

    (

    SELECT '123','D-Report','Y' UNION ALL

    SELECT '123','G-Report','Y' UNION ALL

    SELECT '134','D-Report','Y' UNION ALL

    SELECT '134','G-Report','N'

    )

    SELECT * FROM MyCTE T1

    LEFT OUTER JOIN MyCTE T2

    ON T1.Item = T2.Item

    AND T1.GroupID = '123'

    AND T2.GroupID = '134'

    WHERE T1.Permission <> T2.Permission

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • bpowers (9/12/2012)


    I have a table that contains permissions based on a group id. How would I go about building a script that would compare two groups and return only the records that do not match? Any help will be appreciated.

    Example:

    Group ID Item Permission

    123 D-Report Y

    123 G-Report Y

    134 D-Report Y

    134 G-Report N

    Output:

    Group ID Item Permission

    134 G-Report N

    Why would you not want to return the (second) record in bold in addition to or instead of the record that is returned?


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • The goal is to find out which permissions one group has that another group does not, or vise verse.

  • 1. How many groups you are going to compare at once?

    2. If the answer to the first question is two, will you provide GroupId's for groups to compare into the query?

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • bpowers (9/13/2012)


    The goal is to find out which permissions one group has that another group does not, or vise verse.

    If so, then why not:

    Group ID Item Permission

    123 D-Report Y

    123 G-Report Y

    134 D-Report Y

    134 G-Report N

    Output:

    Group ID Item Permission

    123 G-Report Y

    134 G-Report N

    The permissions are different between the two.

  • I will be comparing only two groups at a time.

  • That looks good. As long as I can identify which permissions differ between two groups.

  • SELECT

    Item,

    MAX(CASE WHEN [Group ID] = @group1 THEN Permission ELSE '' END) AS Group1_Permission,

    MAX(CASE WHEN [Group ID] = @group2 THEN Permission ELSE '' END) AS Group2_Permission

    FROM

    dbo.#work

    WHERE

    [Group ID] IN ( @group1, @group2 )

    GROUP BY

    Item

    HAVING

    MAX(CASE WHEN [Group ID] = @group1 THEN Permission ELSE '' END) <>

    MAX(CASE WHEN [Group ID] = @group2 THEN Permission ELSE '' END)

    ORDER BY

    Item

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Worked perfect! Thanks.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply