Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table
Hadiths_Book
which include column
Book_Id, Book_Arabic_Name, Book_Urdu_Name, Book_English_Name

now i am inserting data from another table name Hadiths
using this query

SQL
insert into Hadiths_Books (Book_Id, Book_Arabic_Name, Book_Urdu_Name, Book_English_Name) 
select distinct ((select MAX(Book_id) from Hadiths_Books) + ROW_NUMBER() OVER(ORDER BY hadith_eng_book)) as Book_Id,
 hadith_book_name, hadith_book_name, CAST(hadith_eng_book as varchar(50)) as hadith_eng_book from Hadiths


but the problem is all duplicate data also inserting into my table while i have use distinct keyword but no progress.
Posted
Comments
jeenamary 30-Sep-13 13:41pm    
what all are fields , which you are using to determine an entry is a duplicate entry?

First, your problem: you're selecting "DISTINCT" but it has no relationship to the target table: nothing is there to stop it from adding entries already present.


You could use a subquery "WHERE [some field] IS NOT IN some query against your target table. That depends upon whether our not your data for each book is unique with respect to [some field].


You may wish, instead, to create a UNION of the two tables - and leave out the ALL keyword then you will only get one copy of each. Beware, however, that any difference two entries will cause them both to appear.

Then, if you've got the UNION working OK, use it t select into a table and you'll have all of your unique entries.

Really, the same problem as before.


 
Share this answer
 
v2
Hello there,

It seems, in this part of the statement,
SQL
select distinct ((select MAX(Book_id) from Hadiths_Books) + ROW_NUMBER() OVER(ORDER BY hadith_eng_book)) 
you are trying to generate an ID that is one greater that the last ID in the table.

So instead of doing that, why don't you set the Identity Specification of Column BookID of the Table to Yes that way you won't have to generate the ID, it'll increment itself and you can just use distinct to get non-duplicate records and insert them like this:
SQL
insert into Hadiths_Books ( Book_Arabic_Name, Book_Urdu_Name, Book_English_Name) 
select distinct 
 hadith_book_name, hadith_book_name, CAST(hadith_eng_book as varchar(50)) as hadith_eng_book from Hadiths


A little update, I found a solution to remove duplicates with your current scenario. Now I don't know what is the primary key column of Hadiths Table, so I have used HadithsID, here it goes:
SQL
insert into Hadiths_Books (Book_Id, Book_Arabic_Name, Book_Urdu_Name, Book_English_Name) 
select A.Book_Id, A.hadith_book_name,A.hadith_book_nameUrDu, A.hadith_eng_book  from (
select (select MAX(Book_id) from Hadiths_Books) + ROW_NUMBER() OVER(ORDER BY hadith_eng_book) as Book_Id,
 hadith_book_name, hadith_book_name as hadith_book_nameUrDu, CAST(hadith_eng_book as varchar(50)) as hadith_eng_book
   ROW_NUMBER() OVER (PARTITION BY hadith_book_name,hadith_eng_book ORDER BY HadithsID) AS RowNumber
  from Hadiths ) as A where A.RowNumber = 1


I tested a similar scenario on my own table, and just merged the solution in your query, so there might be some syntax error, but it works.


Hope it helps, let me know how it goes.

Good luck.

Azee...
 
Share this answer
 
v3
Comments
Muhamad Faizan Khan 1-Oct-13 1:31am    
nice rate 5
Amol_B 1-Oct-13 2:38am    
Good One+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900