Results 1 to 13 of 13

Thread: Get duplicate and related data on same table?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Get duplicate and related data on same table?

    using SQL Server 2008

    its difficult to explain but actually easy to see.

    I have a table:

    DeviceHistory
    ID
    DevID
    Value1 (varchar)
    Value2 (varchar)


    Value1 contains things like:

    serialnumber
    manufacturercode

    think of them as sections/columns/groups. or even better: a heading title.

    value2 contains - its related value.

    I want to pull results from that table for each of the headings (value1) where the value2 is duplicated but for different DevIDs

    makes sense?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Get duplicate and related data on same table?

    Try some thing like this maybe (coded right here might need to fix up)

    Code:
    Select * From (
        Select 
           DeviceId ,
           Value1 ,
           Value2 ,
           RowNumber() OVER (Partition BY Value2, Value1 Order By DeviceId) AS MyRowNum
         From tableName
    ) x
    Where x.MyRowNum > 1
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Get duplicate and related data on same table?

    great. almost my friend!

    so now I get these results:

    Identifier ValueText1 ValueText2 MyRowNum
    2106064 isamirn 633597819001036177 2
    2106064 isamirn 633597819001036177 3
    2106064 isamirn 633597819001036177 4
    8080430 isamirn 633597819001036177 5
    8080430 isamirn 633597819001058692 2
    2106064 isamirn 633597819001058916 2
    8080430 isamirn 633597819001058916 3
    8080430 isamirn 633597819001058916 4
    8080430 isamirn 633597819001058916 5
    8080430 isamirn 633597819001058916 6
    thats cool, but, in this case for example:

    2106064 isamirn 633597819001058916 2
    8080430 isamirn 633597819001058916 3
    8080430 isamirn 633597819001058916 4
    8080430 isamirn 633597819001058916 5
    8080430 isamirn 633597819001058916 6

    I only want to show:

    8080430 isamirn 633597819001058916 6
    2106064 isamirn 633597819001058916 2
    how?


    I also noticed:

    8080430 isamirn 633597819001058692 2
    does not show the other devId ?
    Last edited by Techno; Oct 20th, 2011 at 11:15 AM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Get duplicate and related data on same table?

    If I understand that last one correctly you only want to show the max rownumber (so only once)?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Get duplicate and related data on same table?

    What about this:

    Code:
    Select 
       DeviceId ,
       Value1 ,
       Value2 ,
       Count(*)
     From tableName
    GROUP BY 
    	DeviceID,
    	Value1,
    	Value2
    Having Count(*) > 1
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Get duplicate and related data on same table?

    correct, once per device but showing the devices that have that same valuetext2

    so there are duplicates in that table but I want to filter it so that I get back records where there are no duplicates but only showing the records where the valuetext2 is related to/used on more than 1 devID

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  7. #7
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Get duplicate and related data on same table?

    Code:
    --mimic your table
    declare @myTable table(id int, devid varchar(10), value1 varchar(50), value2 varchar(5))
    
    insert into @myTable(id,devid,value1,value2) Values 
    (2106064, 'isamirn', '633597819001058916', '2'),
    (8080430, 'isamirn', '633597819001058916', '3'),
    (8080430, 'isamirn', '633597819001058916', '4'),
    (8080430, 'isamirn', '633597819001058916', '5'),
    (8080430, 'isamirn', '633597819001058916', '6') 
    
    --load temp table with distinct list of ids and value1s
    SELECT DISTINCT id, value1
    INTO #temp
    FROM @myTable
    
    --find duplicates
    select *
    from #temp
    where value1 in (
    	SELECT value1 
    	from #temp
    	group by value1
    	having count(*) > 1)
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Get duplicate and related data on same table?

    Quote Originally Posted by GaryMazzone View Post
    What about this:

    Code:
    Select 
       DeviceId ,
       Value1 ,
       Value2 ,
       Count(*)
     From tableName
    GROUP BY 
    	DeviceID,
    	Value1,
    	Value2
    Having Count(*) > 1
    nope. thats worse. LOL

    gives me:

    Identifier ValueText1 ValueText2 (No column name)
    2106064 isamirn 633597819001036177 3
    2106064 isamirn 633597819001058916 2
    8080430 isamirn 633597819001058916 4

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  9. #9
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Get duplicate and related data on same table?

    What about this:
    Code:
    Select DeviceId, Value1,Value2,Count(*) +1 AS Counts FROM (
    	Select * From (
    		Select 
    			DeviceId ,
    			Value1 ,
    			Value2 ,
    			RowNumber() OVER (Partition BY Value2, Value1 Order By DeviceId) AS MyRowNum
    		From tableName
    	) x
    Where x.MyRowNum > 1
    )y
    Group BY 
    	DeviceId, 
    	Value1,
    	Value2
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Get duplicate and related data on same table?

    thankyou.

    unfortunately not quite. still the same problem where '633597819001058692' is only showing 1 deviceID related to it, when there are actually 2.

    i used to have decent knowledge of SQL a while ago. but now... my brain is just crampt and cannot remember things as I used to!

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Get duplicate and related data on same table?

    just to make it easier, i thought i would attach a script with data

    be sure to join Device to DeviceHistory on DeviceID in order to get the DeviceIdentifier (on Device table)

    just create a blank SQL Server 2008/5 DB and run the script against it.
    Attached Files Attached Files

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Get duplicate and related data on same table?

    this *seems* to work:

    SELECT DISTINCT(DeviceId), ValueText1, ValueText2
    FROM [DeviceHistory]
    WHERE [ValueText2] IN
    (
    SELECT ValueText2
    FROM DeviceHistory
    WHERE ValueText1 = 'isamirn' AND ValueText2 <> ''
    GROUP BY ValueText2
    HAVING COUNT(*) > 1
    )
    ORDER BY ValueText2




    but now to alter it so I am not hardcoding it for isamirn but instead for the unique valuetext1 values instead
    Last edited by Techno; Oct 21st, 2011 at 04:30 AM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Get duplicate and related data on same table?

    hmm. almost there. one of the records is actually duplicated for DeviceID and ValueText2 but I want to eliminate that also

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width