SQL Statement to eliminate duplicates based on value in another column

帅比萌擦擦* 提交于 2019-12-13 04:33:57

问题


I have data that is in two tables and I have a query combining the data as below. I am trying to eliminate duplicates based on the Id column where I pick the record with the oldest split date. Could anyone kindly assist me with the SQL statement to effect this?

|ID     |SecID  |ReportingDate  |SplitDate  |Adjustor|  
|1465   |2      |31-Dec-09      |01-Nov-10  |0.1     |  
|1465   |2      |31-Dec-09      |27-Dec-12  |0.2     |  
|1466   |2      |31-Dec-10      |27-Dec-12  |0.2     |   
|1468   |2      |31-Dec-11      |27-Dec-12  |0.2     |  
|1469   |2      |31-Dec-08      |01-Nov-10  |0.1     |  
|1469   |2      |31-Dec-08      |27-Dec-12  |0.2     | 

The result should be as below:

|ID     |SecId  |ReportingDate  |Adjustor  |  
|1469   |2      |31-Dec-08      |0.1       |  
|1465   |2      |31-Dec-09      |0.1       |  
|1466   |2      |31-Dec-10      |0.2       |  
|1468   |2      |31-Dec-11      |0.2       |  

More Information:
Let me explain what I am trying to do here.

In the fundamentals table I have a row with a unique Line Id, secId( a product identifier) and a reporting date for this line.
This information needs to be adjusted using information from the splitdetails table that has a date from which it becomes applicable, the secId(product) it affects and the adjustor ratio to be used.

For each line in fundamentals table:
-Where any secId that doesnt have an entry in the splits table, adjustor should be 1.
-If secId is present in the Splits table, the split to be used is the oldest one whose date is older than the fundamentals table reporting date being checked.

I am hoping to get results from the sample above would end up look like this:
| ID |SecId |ReportingDate |Adjustor |
|1469 2 31-Dec-08 0.1
|1465 2 31-Dec-09 0.1
|1466 2 31-Dec-10 0.2
|1468 2 31-Dec-11 0.2
|1467 2 31-Dec-12 1

The query I am using is
SELECT Gotten.ID, Gotten.SecID, Gotten.ReportingDate, Gotten.SplitDate, Adjustor
FROM
(SELECT tblFundamentalsDetails.id, tblFundamentalsDetails.SecId, tblFundamentalsDetails.ReportingDate, tblSplitDetails.SplitDate, tblSplitDetails.Adjustor FROM tblFundamentalsDetails
LEFT JOIN tblSplitDetails
ON (tblFundamentalsDetails.ReportingDate

回答1:


One approach, that should be ok in Access SQL (though you might need to change the aliases):

SELECT a.*
FROM Table1 a
INNER JOIN
(
SELECT ID, sdate = min(SplitDate)
FROM Table1
GROUP BY ID
) b
ON a.ID = b.ID
AND a.SplitDate = b.sdate

Also you could take the inner query and make its own qry in Access.




回答2:


For test data in a table (or saved query) named [Source]

ID    SecID  ReportingDate  SplitDate   Adjustor
----  -----  -------------  ----------  --------
1465      2  2009-12-31     2010-11-01       0.1
1465      2  2009-12-31     2012-12-27       0.2
1466      2  2010-12-31     2012-12-27       0.2
1468      2  2011-12-31     2012-12-27       0.2
1469      2  2008-12-31     2010-11-01       0.1
1469      2  2008-12-31     2012-12-27       0.2

the following query has been tested and actually does work in Access:

SELECT 
    Source.ID,
    Source.SecID,
    Source.ReportingDate,
    Source.Adjustor
FROM
    Source
    INNER JOIN
    (
        SELECT ID, MIN(SplitDate) AS MinOfSplitDate
        FROM Source
        GROUP BY ID
    ) AS MinDate
        ON MinDate.ID = Source.ID
            AND MinDate.MinOfSplitDate = Source.SplitDate

returning

ID    SecID  ReportingDate  Adjustor
----  -----  -------------  --------
1465      2  2009-12-31          0.1
1466      2  2010-12-31          0.2
1468      2  2011-12-31          0.2
1469      2  2008-12-31          0.1



回答3:


I can do this on MySQL server, if I understood you this is what you need:

DELETE t1 FROM mytable t1, mytable t2 WHERE t1.ID = t2.ID 
AND t1.ReportingDate < t2.ReportingDate

This will only keep rows in mytable with the newest date and same id

Notice that three rows with same ID will become just one. You'll only keep one row for each ID.



来源:https://stackoverflow.com/questions/21547512/sql-statement-to-eliminate-duplicates-based-on-value-in-another-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!