简体   繁体   中英

LINQ Query Multiple Group and count of latest record - Oracle DB

请检查表设计

I tried to divided Linq queries into 3 (total, success, fail) but so far "Total" Linq query is working fine. Please help me to get "Success", "Fail" columns (it has mulitple statuses and we have to check the last column of each transaction and destination) Note: you need to group by ProcessTime, TransactionId, Destination and check last column whether it is success or Fail then apply count (we are using oracle as backend)

LINQ for Total count

 var query = (from filetrans in context.FILE_TRANSACTION
                             join route in context.FILE_ROUTE on filetrans.FILE_TRANID equals route.FILE_TRANID
                             where 
                                filetrans.PROCESS_STRT_TIME >= fromDateFilter && filetrans.PROCESS_STRT_TIME <= toDateFilter 
                             select new { PROCESS_STRT_TIME = DbFunctions.TruncateTime((DateTime)filetrans.PROCESS_STRT_TIME), filetrans.FILE_TRANID, route.DESTINATION }).
                             GroupBy(p => new { p.PROCESS_STRT_TIME, p.FILE_TRANID, p.DESTINATION });
                var result = query.GroupBy(x => x.Key.PROCESS_STRT_TIME).Select(x => new { x.Key, Count = x.Count() }).ToDictionary(a => a.Key, a => a.Count);

Right now I'm thinking there isn't a need to make it too complex; I wouldn't bother wrangling another table into a join and then looking at the end of a transaction ID to find a word "Success" or "Fail" on the DB side of things (especially since they have typos). All your status IDs are nicely even for successes and odd for fails but I'd download them all, grouped:

var query = (from filetrans in context.FILE_TRANSACTION
    join route in context.FILE_ROUTE on filetrans.FILE_TRANID equals route.FILE_TRANID
    where 
      filetrans.PROCESS_STRT_TIME >= fromDateFilter && filetrans.PROCESS_STRT_TIME <= toDateFilter 
      select new { 
        PROCESS_STRT_TIME = DbFunctions.TruncateTime((DateTime)filetrans.PROCESS_STRT_TIME),
        STAT = route.FILE_STATUS_ID < 7 ? route.FILE_STATUS_ID % 2 : 2
      }
  ).GroupBy(p => p) 
  .ToDictionary(g => g.Key, g => g.Count());

You'll end up with STAT of 0 for Success, 1 for Failed or 2 for Completed

If you want the total, just Sum() locally

Check this solution. If it gives wrong result, then I need more details.

var fileTransQuery =
    from filetrans in context.AFRS_FILE_TRANSACTION
    where accountIds.Contains(filetrans.ACNT_ID) &&
      filetrans.PROCESS_STRT_TIME >= fromDateFilter && filetrans.PROCESS_STRT_TIME <= toDateFilter
    select filetrans;

var routesQuery =
    from filetrans in fileTransQuery
    join route in context.AFRS_FILE_ROUTE on filetrans.FILE_TRANID equals route.FILE_TRANID
    select route;

var lastRouteQuery = 
    from d in routesQuery.Select(route => new { route.FILE_TRANID, route.DESTINATION }).Distinct()
    from route in routesQuery
        .Where(route => d.FILE_TRANID == route.FILE_TRANID && d.DESTINATION == route.DESTINATION)
        .OrderByDescending(route => route.ROUTE_ID)
        .Take(1)
    select route;

var recordsQuery =
    from filetrans in fileTransQuery
    join route in lastRouteQuery on filetrans.FILE_TRANID equals route.FILE_TRANID
    select new { filetrans.PROCESS_STRT_TIME, route.CRNT_ROUTE_FILE_STATUS_ID };

var result = recordsQuery
    .GroupBy(p => DbFunctions.TruncateTime((DateTime)p.PROCESS_STRT_TIME))
    .Select(g => new TrendData
    {
        TotalCount = g.Sum(x => x.CRNT_ROUTE_FILE_STATUS_ID != 7 && x.CRNT_ROUTE_FILE_STATUS_ID != 8 ? 1 : 0)
        SucccessCount = g.Sum(x => x.CRNT_ROUTE_FILE_STATUS_ID == 7 ? 1 : 0),
        FailCount = g.Sum(x => failureStatus.Contains(x.CRNT_ROUTE_FILE_STATUS_ID) ? 1 : 0),
        Date = g.Min(x => x.PROCESS_STRT_TIME)
    })
    .OrderBy(x => x.Date)
    .ToList();   

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