Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL SUM meny values in table column record a single query

I want sum total of oiltype

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

Also sum total of oiltype

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

In single query.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m write this code but not work.

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

Results needed.

Car      motorcycle     date 
Total     total        01-10-2023

>Solution :

You can use the conditional aggregation :

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading