laravel SQL SUM表列中的多个值记录单个查询

0x6upsns  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(108)

我想要油型的总和

where fullname = gift and 
      FDate = '01-10-2023' and vehicalType = 'car'

也是油型的总和

where fullname = 'gift' and 
      FDate = '01-10-2023' and vehicalType = 'motorcycle'

在单一查询中。
我写了这段代码,但没有工作。

Select vehicalType, sum(total) as total ,fullname, FDate  
from paking_histories 
where FDate = '01-10-2023' and oiltype ='oil' and Fullname ='gift';

需要的结果。

Car      motorcycle     date 
Total     total        01-10-2023
uplii1fm

uplii1fm1#

您可以使用条件聚合:

select sum(case when vehicalType = 'car' then total else 0 end) as car,
       sum(case when vehicalType = 'motorcycle' then total else 0 end) as motorcycle,
       FDate 
from paking_histories
where FDate = '2023-10-01' and oiltype ='oil' and Fullname ='gift'

相关问题