The main goal is to migrate the appropriate data from one table to another, and since my skills are not sharp in sql, I wrote the logic using linq and foreach statement to accomplish what is needed. However, a sql script is needed to run in different environment, so I started to translate what I previously wrote into sql, and boom my sql thought process was left broken at the GroupBy and looping shores.
Here is my attempt which in fact returns an accurate data result:
SELECT BookID FROM (SELECT bt.BookID, pt.PaperID, ppt.PropID
FROM BooksTable AS bt
JOIN PapersTable AS pt
ON bt.BookID = pt.BookID
JOIN PapersPropertiesTable AS ppt
ON pt.PaperID = ppt.PaperID) AS ResultSet
GROUP BY BookID
Here is the code where I hit a rock:
foreach (var i in ResultSetGroupedByBookID)
{
var resultToMigrate= i.DistinctBy(j => j.PropID).ToList();
foreach (var j in resultToMigrate)
{
db.InsertPropsProcedure(j.BookID, j.PropID);
}
}
The major challenges:
- GroupingBy that returns an inner result set with other columns included
- Loop through the inner result set, DistinctBy PropID and return another set
- Loop through the new set, call the insert procedure and pass the params
Update:
I selected Stepen's answer for its simplicty; however, I ran into the scenario where I needed to Distinct By multiple columns while returning a third, so I had to do the following modifications:
INSERT INTO Table2(BookID, PropID, PropValue)
SELECT DISTINCT bt.BookID, ppt.PropID, MAX(ppt.PropValue) AS PropValue
FROM BooksTable AS bt
JOIN PapersTable AS pt ON bt.BookID = pt.BookID
JOIN PapersPropertiesTable AS ppt ON pt.PaperID = ppt.PaperID
GROUP BY bt.BookID, ppt.PropertyID
ORDER BY bt.BookID