Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SQL - How to filter data and pick one record

Posted on 2012-09-06
11
Medium Priority
?
301 Views
Last Modified: 2012-09-07
I have made this up to illustrate what I need. I have records of the following format:
Id, field1, field2
And the sample data is as follow
1, D1, D2
1, DD1, DD2
2, D12, D22
3, D13, D23
3, DD13, DD23
4, D14, D24
5, D15, D25
5, DD15, DD25
6, D16 , D26
7, D17, D27

I have a set of rules to go through Id that have duplicate records.  The final result is to have one record per unique id.   My question is to how I write the SQL so I can examine a group of records for a particular ID using a set of rules.

Here is some of the rules I have
1.  Fields1 and Fields2 are equal pick first 1
2.  Fields1 and Fields2 equal the set value choose the next record
0
Comment
Question by:tommym121
  • 5
  • 5
11 Comments
 
LVL 25

Expert Comment

by:lwadwell
ID: 38374931
I do not understand the rules ...
  - your example does not have records that have "Fields1 and Fields2 are equal" that I can tell
  - "Fields1 and Fields2 equal the set value" ... what is the "set value"?

Please provide an expected result.
0
 

Author Comment

by:tommym121
ID: 38374939
Sorry,
My table has
autonum, Id, field1, field2
This is the Data
1, 1, D1, D2
2, 1, D1, D2
3, 2, D12, D22
4, 3, D1, D3
5, 3, D1, D3
6, 4, D14, D24
7, 5, D15, D25
8, 5, DD15, DD25
9, 6, D16 , D26
10, 7, D17, D27

Let say I have these two rules
1.  Fields1 and Fields2 are equal pick first 1
2.  Fields1 and Fields2 equal the DD15 and DD25 correspondingly

This is the result I expect with the data I have
autonum, Id, field1, field2
1, 1, D1, D2
3, 2, D12, D22
4, 3, D1, D3
6, 4, D14, D24
8, 5, DD15, DD25
9, 6, D16 , D26
10, 7, D17, D27

Let me know if you other questions
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38374985
How about this:
IF OBJECT_ID('tempdb..#tempTable') IS NOT NULL
	DROP TABLE #tempTable

create table #tempTable (
    autonum INT,
    id      INT,
    field1  varchar(10),
    field2  varchar(10)
);
insert into #tempTable values
(1, 1, 'D1', 'D2'),
(2, 1, 'D1', 'D2'),
(3, 2, 'D12', 'D22'),
(4, 3, 'D1', 'D3'),
(5, 3, 'D1', 'D3'),
(6, 4, 'D14', 'D24'),
(7, 5, 'D15', 'D25'),
(8, 5, 'DD15', 'DD25'),
(9, 6, 'D16' , 'D26'),
(10, 7, 'D17', 'D27');

-- select * from #tempTable;

select a.*
  from #tempTable a
  left join #tempTable b
         on a.id = b.id 
            and (   (a.field1 = b.field1 and a.field2 = b.field2 and a.autonum > b.autonum)
                 or (a.field1 != b.field1 and a.field2 != b.field2
                     and right(a.field1,len(a.field1)-patindex('%[^a-zA-Z]%',a.field1)+1) = right(b.field1,len(b.field1)-patindex('%[^a-zA-Z]%',b.field1)+1)
                     and right(a.field2,len(a.field2)-patindex('%[^a-zA-Z]%',a.field2)+1) = right(b.field2,len(b.field2)-patindex('%[^a-zA-Z]%',b.field2)+1)
                     and a.autonum < b.autonum))
 where b.id is null

Open in new window

I assumed for rule 2 - you meant the number in the values where equal.
0
Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

 

Author Comment

by:tommym121
ID: 38374995
lwadwell,

Thanks.   One question  I have is there anyway not to use the autonum in the select.  I put the autonum so I can indicate to you which records are being choose.
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38375004
Without the autonum ... how do you determine which is the 'first 1' or the 'next 1'?

An arbitrary one could be added with a row_number() function prior using inline views if necessary.
0
 

Author Comment

by:tommym121
ID: 38375025
Yes. I think I will need the row_number() as you suggested. How do you apply the row_number() function?  Would you be able to elaborate?
0
 
LVL 25

Accepted Solution

by:
lwadwell earned 1400 total points
ID: 38375058
IF OBJECT_ID('tempdb..#tempTable') IS NOT NULL
	DROP TABLE #tempTable;

create table #tempTable (
    id      INT,
    field1  varchar(10),
    field2  varchar(10)
);
insert into #tempTable values
(1, 'D1',  'D2'),
(1, 'D1',  'D2'),
(2, 'D12', 'D22'),
(3, 'D1',  'D3'),
(3, 'D1',  'D3'),
(4, 'D14', 'D24'),
(5, 'D15', 'D25'),
(5, 'DD15','DD25'),
(6, 'D16', 'D26'),
(7, 'D17', 'D27');

-- select * from #tempTable;

;with temp_with_seq as (
select id, field1, field2, row_number()over(order by id) autonum 
from #tempTable
)
select a.*
  from temp_with_seq a
  left join temp_with_seq b
         on a.id = b.id 
            and (   (a.field1 = b.field1 and a.field2 = b.field2 and a.autonum > b.autonum)
                 or (a.field1 != b.field1 and a.field2 != b.field2
                     and right(a.field1,len(a.field1)-patindex('%[^a-zA-Z]%',a.field1)+1) = right(b.field1,len(b.field1)-patindex('%[^a-zA-Z]%',b.field1)+1)
                     and right(a.field2,len(a.field2)-patindex('%[^a-zA-Z]%',a.field2)+1) = right(b.field2,len(b.field2)-patindex('%[^a-zA-Z]%',b.field2)+1)
                     and a.autonum < b.autonum))
 where b.id is null;

Open in new window

0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 600 total points
ID: 38375618
0
 

Author Comment

by:tommym121
ID: 38375914
lwadwell,

I try to disassemble your code to understand how it will work on my situation.

What is 'Where b.id is null' mean?  When I try to remove it it give me all the data including all the duplicate.
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38375936
The query uses a left join ... a left join will return null to the where clause where a match is not found ... you wanted the records that were not matched.
0
 

Author Closing Comment

by:tommym121
ID: 38376425
Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question