简体   繁体   中英

How to compare two datasets to check for missing rows which don't exist in the other dataset?

I have built a method which takes two datasets: dataset1 and retirementSimpleData . It matches the two datasets based on a primary key/number, defined as cnum , in the code below. I wanted to return the value of the difference between the getAssets value and the getSums value, and this is working, except for one little problem.

Some of the cnums that exist in dataset1 don't exist in retirementSimpleData . Similarly, some cnums which may exist in retirementSimpleData may not exist in dataset1 . This is resulting in no data being returned for that cnum .

I would like to implement two passes at the end which check in one direction to see if I missed anything. The second pass would check in the opposite direction. However, not sure how I would go about implementing this.

 public void getReportData(int index) {

        String date1 = Util.dateTimeToShortString(reportDate);
        String date2 = reportDao.getPreviousRetirementDate(date1);

        List<SurveyCompareAssetsCheckData> dataset1 = reportDao.getSurveyCheckCompareAssetsData(date1);

        List<RetSurveyAssets> retirementSimpleData = reportDao.findRetSurveyByDate(date1);

        for (SurveyCompareAssetsCheckData surveyCompareAssetsCheckData : dataset1) {
            for (RetSurveyAssets surveyCompareAssetsCheckData2 : retirementSimpleData) {
                if (surveyCompareAssetsCheckData.getCnum() == surveyCompareAssetsCheckData2.getCnum()) {
                    surveyCompareAssetsCheckData.setRetirementsimple(surveyCompareAssetsCheckData2.getSums());
                    surveyCompareAssetsCheckData.setDifference(surveyCompareAssetsCheckData.getAssets() - surveyCompareAssetsCheckData2.getSums());

Caveat: dataset1 and retirementSimpledata both use existing SQL pulls which I am not allowed to touch, otherwise I would have simply defined new SQL for these methods in my "DAOImpl." Therefore, I have to work with the data I am getting, and programmatically check for this.

Below, is the report which is being generated with my code. As you can see, I am ending up with zeros, which is showing the difference (incorrectly) as zeros, because Cnum #45, in this example simply doesn't exist in the second dataset ( retirementSimpleData ) 在此处输入图片说明

What is the datatype of Cnum, if it is int then default value is Zero.

You have to add else-if condition to check for example:

else if (surveyCompareAssetsCheckData2.getCnum()== 0){

-------- logic here --------------------

}
else if (surveyCompareAssetsCheckData.getCnum() ==0){

----------logic here -----------

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM