sql column data is displayed as horizontal data after query

Scenes

Different types of data need to be counted and displayed as horizontal data.
For example:
Statistics the sales of different types of products of the company.

solution

At first I saw a bit of a cloud. It's actually very simple, just use if or case.

Solve directly in sql

select 
	sum(
	case category 
	when 'Alcohol' then amount
	end
	) as AlcoholAmount,
	sum(
	case category 
	when 'smoke' then amount
	end
	) as SmokeAmount 
from sales
where company='a-company'
group by category

The returned field directly corresponds to the value, which can be received by the entity class.

Return to the list and process in the code

select 
category,sum(amount) as totalAmount
from sales
where company='a-company'
group by category

In this way, a map can be returned and then traversed in java. It is also possible to put it into the entity class according to the type.

Guess you like

Origin blog.csdn.net/enthan809882/article/details/114006881