Remove duplicate values with multiple columns as condition in SQL query

Source: Internet
Author: User

Select Top 12 ID, URL, titleorname from t_userscolumn A where Mark = '1' and not exists (select * From t_userscolumn where url =. URL and titleorname =. titleorname and Mark = '1' and ID>. ID) order by ID DESC

========================================================== ========

Remove duplicate values from SQL Server 2000

the table structure is zymc -- major name, NJ -- grade, xnxqh -- academic year number, xxlb -- Study class, XF -- credits, xs -- hours
zymc NJ xnxqh kcmc kcxz XF Xs
AA 2001 20041 2001 English 1 3 60
AA 20041 2001 English 1 0 60
AA 20041 English 2 3.5 70
BB 2002 20041 mathematics 1 5 90
BB 2002 20041 mathematics 1 0 90
BB 2002 20041 mathematics 1 4.5 90
BB 2002 mathematics 1 4 80
BB 2001 20041 physical 1 0 80
expected results:
zymc NJ xnxqh kcmc kcxz XF Xs
AA 2001 20041 english 1 3 60
BB 2002 20041 mathematics 1 5 90
BB 2001 20041 physical 1 0 80
when credits are 0 and non-0 and non-0 values are greater take the first non-0 value for a row, if there is only one row, the credit is 0 and the number of rows is retained
after the duplicate value is removed, you can create primary keys for the tables in zymc, NJ, xnxqh, and kcmc
Primary

Because of the same zymc, NJ, xnxqh, and kcmc, you only need to keep one record, so take the same zymc, NJ, xnxqh, the maximum XF value of kcmc data can meet your requirements.

Select * from Table
Where not exists (select * from table where zymc = A. zymc and nj = A. NJ and xnxqh = A. xnxqh and kcmc = A. kcmc and XF> A. xf)

---------------------------------------------------------------------------------

This is the case if you delete unwanted records.

Delete A from Table
Where exists (select * from table where zymc = A. zymc and nj = A. NJ and xnxqh = A. xnxqh and kcmc = A. kcmc and XF> A. xf)

--------------------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.