Dan Hunex
Dan Hunex

Reputation: 5318

prisma group by two fields

I have a database where there is PlantName, datetime (just date component), price, and orderId. I want to aggregate data by facility, sum up the price for the day, and return data. A list of PlantNames is passed in for which we need to calculate the total , however, I see duplicate of PlantNames in the returned data, I was expecting unique data for each Plant


    const ordersWithAggregations=db.Orders.groupBy({
          by: ['PlantName','OrderDate'],
          where: {
            PlantName: {
              in: plantNames  //passed in as an array
            },
            OrderDate: {
              gte: fromDate,   //passed in
              lte: toDate   // passed in
            }
          },
          _sum: {
            TotalPrice: true
          },
          orderBy: {
            OrderDate: 'asc', 
          },
        });
    
    
    const result: IDataVisualize[] = ordersWithAggregations.map(agg => ({
          PlantName: agg.Plant,
          data: [
            {
              date: format(agg.OrderDate),
              grandTotalPrice: agg._sum.TotalPrice || 0
            }
          ]
        }));
        

One other thing, if the OrderDate has a time component, I want to ignore it , and use the date part only

Any idea what I am missing and I am getting data like

      
    [  { plantName: 'Kolfe', data: [{} ] }, 
     { plantName: 'Lafto', data: [{} ] },  
    { plantName: 'Kolfe', data: [{} ] }]   //repeated
       

Upvotes: 1

Views: 300

Answers (0)

Related Questions