Results 1 to 16 of 16

Thread: Finding duplicates w/sql

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780

    Finding duplicates w/sql

    how does one find duplicates with SQL?

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    If I understand your question - I just answered a similar one yesterday.

    See this thread: http://www.vbforums.com/showthread.p...hreadid=293577

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    i read the thread but i'm not sure how that would work for me, i have a table of which has some machine numbers and it also has more than one date entry for each machine. in the table veiw of access97 there is more than one entry for each machine what i am trying to get VB to do is to read in all the values for the machiines and then in a combo box put the date(s) that corrospond to that machine.

  4. #4
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524
    Originally posted by Dubya007
    ....what i am trying to get VB to do is to read in all the values for the machiines and then in a combo box put the date(s) that corrospond to that machine.
    what values should VB read ? the entry date ?

    from szlamany's t-sql :

    SELECT THECOLUMN,SUM(1) FROM THETABLE GROUP BY THECOLUMN HAVING SUM(1)>1

    change to :

    SELECT machine_id,SUM(1) FROM machineTABLE GROUP BY machine_id HAVING SUM(1)>1

    the query will list machine with more than one entry date. then you can use the ID to get the entry date and put in combo box.

    VB Code:
    1. private sub module_one()
    2.    dim rs as new adodb.recordset
    3.    'assume that myconn is your connection var
    4.  
    5.    rs.open "SELECT machine_id,SUM(1) FROM machineTABLE GROUP BY machine_id HAVING SUM(1)>1",myconn,adopenkeyset,adlockoptimistic,adcmdtext
    6.  
    7.    if rs.recordcount>0 then
    8.       rs.movefirst
    9.  
    10.       do while not rs.eof
    11.            call fill_combo(trim(rs!machine_id))
    12.  
    13.            rs.movenext
    14.       loop
    15.    end if
    16.  
    17.    rs.close
    18.    set rs=nothing
    19. end sub
    20.  
    21. private sub fill_combo(byval machine_id as string)
    22.      dim rs2 as new adodb.recordset
    23.  
    24.       rs2.open "select entry_date from machineTABLE where machine_id = '" & trim(machine_id) & "' order by entry_date asc", myconn,adopenkeyset, adlockoptimistic, adcmdtext
    25.  
    26.       if rs2.recordcount > 0 then
    27.            rs2.movefirst
    28.            
    29.            do while not rs2.eof
    30.                 'assume combobox name = Combo1
    31.                 combo1.additem format(rs2!entry_date, "mm/dd/yyyy")
    32.  
    33.                 rs2.movenext
    34.            loop
    35.       end if
    36.  
    37.       rs2.close
    38.       set rs2=nothing
    39. end sub

    do i make mistake here with the help ? i hope that's your point....
    if not, pls clarify me / us here.
    Regards,
    [-w-]

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    i adapted your code, but it does not work. i have posted it below, maybe you can look @ it and tell me where i am going wrong. i didn't use the
    rs!.... because i didn't understand it, maybe that is where i am going wrong. here is the code
    VB Code:
    1. Private Sub Form_Load()
    2. MachID = Trim(txtID.Text)
    3. Debug.Print MachID
    4. Dim sql As String
    5. sql = "SELECT IDNum, COUNT(IDNum) FROM Testble GROUP BY IDNum  HAVING COUNT(*)>1"
    6.  
    7. If rs.State = adStateClosed Then
    8.     rs.Open sql, DBConn, adOpenDynamic, adLockBatchOptimistic, adCmdText
    9.     'Display function just displays the Machine numnber in a text box for now _
    10.     it will be used to display other things later.
    11.     Call display(0)
    12. Else
    13.     Call display(0)
    14. End If
    15.  
    16. If rs.RecordCount > 0 Then
    17.     rs.MoveFirst
    18.    
    19.     Do While Not rs.EOF
    20.         Call FillList(Trim(rs.Fields("IDNum")))
    21.         rs.MoveNext
    22.         Loop
    23.     End If
    24. End Sub
    25. Private Sub FillList(ByVal IDNum As String)
    26. Dim trs As New ADODB.Recordset
    27. Dim sql As String
    28.  
    29.  
    30. sql = "SELECT Date FROM Testble " _
    31.     & "WHERE IDNum = '" & Trim(IDNum) & "' ORDER BY Date ASC"
    32.  
    33.  
    34. trs.Open sql, DBConn, adOpenKeyset, adLockOptimistic, adCmdText
    35.  
    36. If trs.RecordCount > 0 Then
    37.     trs.MoveFirst
    38.     Do While Not trs.EOF
    39.         cmbDates.AddItem trs.Fields("Date").Value
    40.         trs.MoveNext
    41. Loop
    42. End If
    43. End Sub

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    oh i almost forgot to tell you that i don't get an error, it finds all the machines that are in there more than once but it doesn't fill the combo box with the corrosponding dates. the combo box is just empty

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    put a break point on this line:
    VB Code:
    1. If trs.RecordCount > 0 Then

    I'd bet that .RecordCount returns -1. Try this instead:
    VB Code:
    1. If not (trs.EOF And trs.BOF) Then
    2.     trs.MoveFirst
    3.     Do While Not trs.EOF
    4.         cmbDates.AddItem trs.Fields("Date").Value
    5.         trs.MoveNext
    6. Loop
    7. End If


    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    that didn't work either. i did some testing and the FillList sub is never even being called! so i have to figure out why this is.

  9. #9
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524
    that didn't work either. i did some testing and the FillList sub is never even being called! so i have to figure out why this is.

    no need to be panic.....
    pls take a look at your coding again. I'll show you why FillList sub is never being called....

    in your code :
    VB Code:
    1. sql = "SELECT IDNum, COUNT(IDNum) FROM Testble GROUP BY IDNum  HAVING COUNT(*)>1"
    2.  
    3. If rs.State = adStateClosed Then
    4.     rs.Open sql, DBConn, adOpenDynamic, adLockBatchOptimistic, adCmdText
    5.     'Display function just displays the Machine numnber in a text box for now _
    6.     it will be used to display other things later.
    7.     Call display(0)
    8. Else
    9.     Call display(0)
    10. End If
    11.  
    12. If rs.RecordCount > 0 Then
    13.     rs.MoveFirst
    14.    
    15.     Do While Not rs.EOF
    16.         Call FillList(Trim(rs.Fields("IDNum")))
    17.         rs.MoveNext
    18.         Loop
    19.     End If
    20. End Sub

    please note that you open the recordset by using adOpenDynamic. And .recordcount method can only be used if you open recordset by using adOpenKeyset.

    If open method <> adOpenKeyset, then .RecordCount will return -1

    If you open by adOpenDynamic, you can try to use :
    VB Code:
    1. If rs.bof <> true and rs.eof <> true Then
    2. .....
    3. else
    4. ....
    5. end if

    try it, I hope your FillList sub will be called
    Last edited by Wen Lie; Jun 16th, 2004 at 12:13 AM.
    Regards,
    [-w-]

  10. #10
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Originally posted by Wen Lie
    please note that you open the recordset by using adOpenDynamic. And .recordcount method can only be used if you open recordset by using adOpenKeyset.
    Um, I use it with static (snapshot) of the table, and recordcount works with that, for you info

    Personally I use:
    - dynamic - updated/editting
    - static - read only - filling list/combo boxes


    Vince
    Last edited by Ecniv; Jun 16th, 2004 at 03:44 AM.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    i actually was able to get it to workwith out having any if statements(if this is bad let me know) but here is my code the problem now is now that i want to be able to click on one of the dates that is in the box. the problem is that no matter what i use for the SQL statement says that i have a syntax error but i can't find it.
    VB Code:
    1. Private Sub lstDates_Click()
    2. Dim cmdCommand As New ADODB.Command
    3. Dim sql As String
    4. Dim trs As New ADODB.Recordset
    5.  
    6. Set cmdCommand.ActiveConnection = DBConn
    7. cmdCommand.CommandType = adCmdText
    8.  
    9. sql = "SELECT DISTINCT Testble.IDNum, tblMachines.machine_desc, Testble.NeedACheck," _
    10.        & "tblMachines.NeedsCheck, Testble.Date, tblMachines.deptId, Testble.Supervisor " _
    11.        & "FROM tblMachines, Testble WHERE tblMachines.ID = Testble.IDNum AND (Testble.Date = '" & lstDates.Text '")
    12.  
    13.  
    14. cmdCommand.CommandText = sql
    15.  
    16. Set trs = cmdCommand.Execute
    17. Unload Me
    18. frmSafeGuard.Show
    19. End Sub

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    i fixed the syntax error but the problem is that it will only show the data for the first date entry. for example
    if i have a machine with the number of 8
    and it has the entries
    3/6/2004
    3/7/2004
    3/12/2004
    3/13/2004
    3/14/2004

    and i click on 3/7/2004
    it will show the entry for 3/6/2004
    here is my SQL statment
    VB Code:
    1. sql = "SELECT DISTINCT Testble.IDNum, tblMachines.machine_desc, Testble.NeedACheck," _
    2.        & "tblMachines.NeedsCheck, Testble.Date, tblMachines.deptId, Testble.Supervisor " _
    3.        & "FROM tblMachines, Testble WHERE tblMachines.ID = Testble.IDNum AND Testble.[Date] = " & lstDates.Text

  13. #13
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524
    Originally posted by Ecniv
    Um, I use it with static (snapshot) of the table, and recordcount works with that, for you info

    Personally I use:
    - dynamic - updated/editting
    - static - read only - filling list/combo boxes


    Vince

    huehuehue... thx for the correction dude. I forgot. And just remember when you tell me

    I use static sometimes (rarely), nd works also with .recordcount method ;p~

    I just remember ;p nd thx to remind me bout this ;p~
    but for dynamic, .recordcount never works.
    Regards,
    [-w-]

  14. #14
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524
    Originally posted by Dubya007
    .......
    3/6/2004
    3/7/2004
    3/12/2004
    3/13/2004
    3/14/2004

    and i click on 3/7/2004
    it will show the entry for 3/6/2004
    here is my SQL statment
    VB Code:
    1. sql = "SELECT DISTINCT Testble.IDNum, tblMachines.machine_desc, Testble.NeedACheck," _
    2.        & "tblMachines.NeedsCheck, Testble.Date, tblMachines.deptId, Testble.Supervisor " _
    3.        & "FROM tblMachines, Testble WHERE tblMachines.ID = Testble.IDNum AND Testble.[Date] = " & lstDates.Text
    it should be 3/7/2004 as the output ???
    for your T-SQL, try below :
    Code:
    sql = "SELECT DISTINCT Testble.IDNum, tblMachines.machine_desc" _
    & ", Testble.NeedACheck, tblMachines.NeedsCheck, Testble.Date" _
    & ", tblMachines.deptId, Testble.Supervisor FROM tblMachines" _
    & ", Testble WHERE tblMachines.ID = Testble.IDNum AND Testble.[Date] = '" _
    & lstDates.Text & "' and tblMachines.ID = 8"
    if tblMachines.ID is a float data type, and suppose your ID input is txtMachineID then try this :

    Code:
    sql = "SELECT DISTINCT Testble.IDNum, tblMachines.machine_desc" _
    & ", Testble.NeedACheck, tblMachines.NeedsCheck, Testble.Date" _
    & ", tblMachines.deptId, Testble.Supervisor FROM tblMachines" _
    & ", Testble WHERE tblMachines.ID = Testble.IDNum AND Testble.[Date] = '" _
    & lstDates.Text & "' and tblMachines.ID = " & val(txtMachineID.Text) & ""
    at your given query to us, seems you've missed the machine ID. Please take a look at your where clause. You only set tblMachines.ID = Testble.IDNum....
    but you didn't specify which machine ID you need to query.

    hope that's what you're trying to fix
    Last edited by Wen Lie; Jun 17th, 2004 at 04:15 AM.
    Regards,
    [-w-]

  15. #15
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Originally posted by Dubya007
    i fixed the syntax error but the problem is that it will only show the data for the first date entry. for example
    if i have a machine with the number of 8
    and it has the entries
    3/6/2004
    3/7/2004
    3/12/2004
    3/13/2004
    3/14/2004

    and i click on 3/7/2004
    it will show the entry for 3/6/2004

    here is my SQL statment
    VB Code:
    1. sql = "SELECT DISTINCT Testble.IDNum, tblMachines.machine_desc, Testble.NeedACheck," _
    2.        & "tblMachines.NeedsCheck, Testble.Date, tblMachines.deptId, Testble.Supervisor " _
    3.        & "FROM tblMachines, Testble WHERE tblMachines.ID = Testble.IDNum AND Testble.[Date] = " & lstDates.Text
    The bit in bold suggests that the code ID you use to pull the data onto the form is offset. combo lists start at 0, but if you are using the listindex, it should point to the correct place.
    VB Code:
    1. strSql="SELECT DISTINCT Testble.IDNum, tblMachines.machine_desc, Testble.NeedACheck, tblMachines.NeedsCheck, Testble.Date, tblMachines.deptId, Testble.Supervisor "
    2. strsql=strsql & "FROM tblMachines, Testble "
    3. strsql=strsql & "WHERE tblMachines.ID = Testble.IDNum AND Testble.[Date] = #" & format(cdate(lstDates.list(lstDates.listindex,0)),"dd mmm yyyy") & "#"
    Sql is in american format, so uk dates may be off. If it accepts the above, it should convert to a date reformat it to a nicer, non confusing date.

    If there is an id for a particular record, this should be held in the combo, making the combo a two column list, the first column being Zero. I don't know if this is required, because if you have duplicate records for the date, then an id is not required.

    Was the db in Sql Server or Oracle? (just wondering on the join)

    Hope its going ok.


    Vince
    EDIT:> Just noticed you are from us... so forget the reformatting of the date function

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    thanx for all the help i am going to try this stuff later, when i get a chance, i'll let you know how it goes.
    "...Men will still say THIS was our finest hour"
    If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?

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