Results 1 to 15 of 15

Thread: [RESOLVED] redundant record

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Resolved [RESOLVED] redundant record

    I have redundant record. Record below show twice. How to avoid it

    Selangor, panchang Bedena, 1/2003
    .Open "SELECT [Prestasi hasil].Negeri,[Prestasi hasil].[Lokasi Projek],[Prestasi hasil].Musim,pemilik.Pemilik1,pemilik.Pemilik2,[Prestasi hasil].Plot,[Prestasi hasil].[Hasil Kasar (kg)],[Prestasi hasil].[Purata Hasil Kasar (Tan/ha)],[Prestasi hasil].[Hasil Bersih (-17 %)],[Prestasi hasil].[Purata Hasil Bersih (Tan/ha)],[Prestasi hasil].Masalah FROM [Prestasi hasil],pemilik where [Prestasi hasil].Plot=pemilik.Plot"
    Attached Images Attached Images  

  2. #2
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Re: redundant record

    Have you redundant records? You select a lot of fields, and you showed us only 3... If all other fields you selected in the query and NOT showed here are identical too (so fields in the two rows are completely equals) you can try:

    .Open "SELECT DISTINCT [Prestasi hasil].Negeri,[Prestasi hasil].[Lokasi Projek],[Prestasi hasil].Musim,pemilik.Pemilik1,pemilik.Pemilik2,[Prestasi hasil].Plot,[Prestasi hasil].[Hasil Kasar (kg)],[Prestasi hasil].[Purata Hasil Kasar (Tan/ha)],[Prestasi hasil].[Hasil Bersih (-17 %)],[Prestasi hasil].[Purata Hasil Bersih (Tan/ha)],[Prestasi hasil].Masalah FROM [Prestasi hasil],pemilik where [Prestasi hasil].Plot=pemilik.Plot GROUP BY [Prestasi hasil].Negeri,[Prestasi hasil].[Lokasi Projek],[Prestasi hasil].Musim,pemilik.Pemilik1,pemilik.Pemilik2,[Prestasi hasil].Plot,[Prestasi hasil].[Hasil Kasar (kg)],[Prestasi hasil].[Purata Hasil Kasar (Tan/ha)],[Prestasi hasil].[Hasil Bersih (-17 %)],[Prestasi hasil].[Purata Hasil Bersih (Tan/ha)],[Prestasi hasil].Masalah "

    Best regards,
    Xmas.
    Learn, this is the Keyword...

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: redundant record

    Before doing your INSERT, run a SELECT to see if the record already exists.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: redundant record

    Xmas79 - why did you suggest DISTINCT and GROUP BY in the same query?

    They basically produce the same output - right?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: redundant record

    Quote Originally Posted by Xmas79
    Have you redundant records? You select a lot of fields, and you showed us only 3... If all other fields you selected in the query and NOT showed here are identical too (so fields in the two rows are completely equals) you can try:
    While correct (w/si's caveat), if all fields in the records are identical, you need to fix your db design. There should never be identical records in a table. If you don't want duplicate values in a field or group of fields, consider making them into a primary key. This will prevent duplicate entries. Si's method will allow you to check first, and give the user an error message when entering data.
    If the selected fields can contain duplicates (i.e. all fields in the record, including those you haven't selected, aren't duplicates), use the DISTINCT or GROUP BY clauses. One of the gurus here once posted that GROUP BY is better, but can't recall why.
    Tengo mas preguntas que contestas

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: redundant record

    In some database engines DISTINCT is performed after the "working resultset" is completely built.

    GROUP BY only inserts rows inthe the "working resultset" if they do not already appear. And of course GROUP BY processes any aggregate functions at the same time.

    With MS SQL 2000 this difference went away - DISTINCT now uses the more powerful GROUP BY logic.

    I would imagine that not all DB engines do this the same way.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: redundant record

    Thanks, szlamany, that makes a lot of sense.
    Tengo mas preguntas que contestas

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: redundant record

    How to used Distinct for this SQL ?

    Code:
     InfoRS3.Open "Select * from bancian order by Plot", conn, adOpenDynamic, adLockOptimistic
    I have alot of field name so I used * to show all field name.

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: redundant record

    As Salvelinus already indicated - you should never have duplicate rows in a single table.

    Are you saying when you run this query

    Select * from bancian order by Plot

    You see duplicate rows??

    Can you post a couple of them here and explain why?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: redundant record

    no, I not run the SQl..

    I just want to know where I need to put the DISTINCT if the SQL command like this because the sample they give stated the field name in the SQL command

    Select * from bancian order by Plot

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: redundant record

    I hate to tell this...

    It's

    Select Distinct * from bancian order by Plot

    I don't like DISTINCT. I want to always know why it's being used - as it usually is never needed.

    Although - I just used it myself in a STORED PROCEDURE I'm writing for a web-page

    Code:
    Select	 Distinct SC.ConfData
    	,BU.BldgName
    	,TM.TeamDescr
    	,RM.Team
    	,RM.Bldg
    	,RM.Yr
    	,@RemoteUser
    	From Building_T BU
    	Left Join Room_T RM on RM.Yr=@Yr and RM.Bldg=BU.Bldg
    	Left Join Stufiles_T SC on SC.ConfItem='DistrictName'
    	Left Join Team_T TM on TM.Yr=RM.Yr and TM.Bldg=RM.Bldg and TM.Team=RM.Team
    	Where BU.Bldg=@Bldg
    	Order by RM.Bldg,RM.Team
    I did this because there are dozens and dozens of rooms for a building in the ROOM_T table. But all I want is the "distinct" list of TEAM fields from that table (there might be only 3 or 4 different teams in a school building).

    So I understand that my JOIN to ROOM_T is "weak" - but it's intentional because of what my end goal is - to get the DISTINCT TEAMS from that ROOM table.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Re: [RESOLVED] redundant record

    Xmas79 - why did you suggest DISTINCT and GROUP BY in the same query?

    They bsically produce the same output - right?
    They produce the same output, but they can procude different execution plans, depending on the engine and the query itself (ok, not the query above). But sorry for putting both in the same query.......
    And sorry for answering late too... VBForums mail just arrived a few minutes ago...

    Best regards,
    Xmas.
    Learn, this is the Keyword...

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] redundant record

    Thanks for the clarification...

    Quote Originally Posted by Xmas79
    ...they can procude different execution plans, depending on the engine and the query itself...
    I discussed the execution plan differences in post #6 here...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Re: [RESOLVED] redundant record

    Yes, I know... Credits of course go to you

    Best regards,
    Xmas.
    Learn, this is the Keyword...

  15. #15
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: redundant record

    Quote Originally Posted by szlamany
    As Salvelinus already indicated - you should never have duplicate rows in a single table.
    For that matter, you should never have rows duplicated between tables, i.e. the same values in two tables.
    The only exception I can see is if one of the tables has fields that can be populated by calculated values which can change over time. This would probably be better dealt with by a query, but there may be times not. Not the SQL guru myself.
    Tengo mas preguntas que contestas

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