SQL-为单行中的多列选择MAX(date)的结果

时间:2018-11-22 23:36:17

标签: sql date max

我想从拆分成销售订单的表中获取最后一个订单日期

请参见下面的示例

示例

PRODUCT     ORDER_DATE  ORDER_PRICE ORDER_TYPE
0000016920  2018-05-11  135.440     Purchase Order
0000016920  2018-05-11  135.440     Sales Order
0000022670  2018-08-01  0.010       Sales Order
0000024280  2018-09-25  661.9757    Purchase Order
0000024280  2018-09-25  661.9757    Sales Order
0000025560  2018-03-14  265.5953    Sales Order
0000025560  2018-01-16  224.6733    Sales Order
0000025560  2018-03-14  265.5953    Purchase Order
0000025560  2018-03-01  224.6733    Sales Order
0000025560  2018-01-16  224.6733    Purchase Order
0000025590  2018-10-02  841.3115    Sales Order
0000025590  2018-10-02  841.3115    Purchase Order
0000025960  2018-10-02  1070.6024   Purchase Order
0000025960  2018-10-02  1070.6024   Purchase Order
0000025960  2018-10-02  1013.0212   Purchase Order
0000025960  2018-01-11  747.2314    Purchase Order

期望的输出

PRODUCT     SALESDATE       SALES_PRICE         PURCHASE_DATE       PPRICE
0000016920  2018-05-11      135.4400            2018-05-11          135.4400            
0000022670  2018-08-01      0.0100              2018-08-01          0.0100              
0000024280  2018-09-25      661.9757            2018-09-25          661.9757            
0000025560  2018-03-14      265.5953            2018-03-14          265.5953            
0000025560  2018-01-16      224.6733            2018-01-16          224.6733            
0000025560  2018-03-01      224.6733            2018-03-01          224.6733            
0000025590  2018-10-02      841.3115            2018-10-02          841.3115            
0000025960  2018-03-01      747.2314            2018-03-01          747.2314            
0000025960  2018-10-02      1056.2071           2018-10-02          1056.2071           
0000025960  2018-10-02      1070.6024           2018-10-02          1070.6024           
0000025960  2018-01-12      747.2314            2018-01-12          747.2314

3 个答案:

答案 0 :(得分:0)

我相信您需要这样的东西:

var unkownSegments = grouped.Where(x => x.ActivityType == null);

foreach (var group in unkownSegments)
{
   var tempLists = new List<List<LocationResult>>();

   //This variable keeps track of the beginning of the next range
   var rangeStart = 0;

   for (int i = 0; i < group.Items.Count - 1; i++)
   {
      var point1 = group.Items[i];
      var point2 = group.Items[i + 1];

      var sCoord = new GeoCoordinate(point1.Lat, point1.Long);
      var eCoord = new GeoCoordinate(point2.Lat, point2.Long);

      var distance = sCoord.GetDistanceTo(eCoord);

      if(distance > 30)
      {
         var tempList = group.Items.GetRange(rangeStart, i - rangeStart + 1);
         tempLists.Add(tempList);
         rangeStart = i + 1;//Next range will begin on the following item
      }
   }

   if (group.Items.Count - rangeStart > 0)
   {
      //Add all remainging (not added yet) points as the last range.
      var tempList = group.Items.GetRange(rangeStart, group.Items.Count - rangeStart);
      tempLists.Add(tempList);
   }
}

答案 1 :(得分:0)

我只会使用条件聚合:

select product,
       max(case when order_type = 'Sales Order' then order_date end) as sales_date,
       max(case when order_type = 'Sales Order' then order_price end) as sales_price,
       max(case when order_type = 'Purchase Order' then order_date end) as purchase_date,
       max(case when order_type = 'Purchase Order' then order_price end) as purchase_price
from example
group by product;

答案 2 :(得分:-1)

您可以使用类似的东西

SELECT (SELECT MAX(DATE1) from tbl) as date1, (SELECT MAX(DATE2) from tbl) as date2