해당 게시글은 개인 프로젝트인 "광고 관리 플랫폼 대행사 센터 제작" 중
#108 "광고 관리 페이지 (소재 리스트) 수정 및 통계 기능 구현" 이슈를 다루고 있습니다.
Projections.fields
를 통해 필드에 직접 값을 주입하는 방식 선택SELECT
c.id AS creative_id,
c.name AS creative_keyword,
c.budget AS creative_bidingPrice,
c.deleted AS creative_deleted,
c.activated AS creative_activated,
sum(p.view),
sum(p.click),
sum(p.conversion),
sum(p.spend),
sum(p.purchase)
FROM performance as p
LEFT JOIN creative AS c ON c.id = p.creative_id
LEFT JOIN campaign AS ca ON ca.id = c.campaign_id
WHERE p.created_at BETWEEN '2022-10-15' AND '2022-10-19' AND ca.id = 1 AND c.deleted = false
GROUP BY c.id
public List<PerformanceStatisticsDto> findByCampaign_IdAndStatisticsDefault(@Param("id") Long campaignId,
@Param("startDate") LocalDate startDate,
@Param("lastDate") LocalDate lastDate
) {
List<PerformanceStatisticsDto> results = jpaQueryFactory
.select(Projections.fields(PerformanceStatisticsDto.class,
performance.creative.id.as("creativeId"),
performance.creative.keyword.as("keyword"),
performance.creative.bidingPrice.as("bidingPrice"),
performance.creative.deleted.as("deleted"),
performance.creative.activated.as("activated"),
performance.view.sum().as("view"),
performance.click.sum().as("click"),
performance.conversion.sum().as("conversion"),
performance.purchase.sum().as("purchase"),
performance.spend.sum().as("spend")
))
.from(performance)
.leftJoin(performance.creative, creative)
.leftJoin(creative.campaign, campaign)
.where(
performance.createdAt.between(startDate, lastDate),
campaign.id.eq(campaignId),
performance.creative.deleted.eq(false)
)
.groupBy(performance.creative.id)
.fetch();
for (PerformanceStatisticsDto result : results) {
Long spend = result.getSpend();
Long view = result.getView();
Long click = result.getClick();
Long conversion = result.getConversion();
Long purchase = result.getPurchase();
result.setCreativeIndicator(spend, view, click, conversion, purchase);
}
return results;
}
SELECT
sum(p.view),
sum(p.click),
sum(p.conversion),
sum(p.spend),
sum(p.purchase)
FROM performance as p
LEFT JOIN creative AS c ON c.id = p.creative_id
LEFT JOIN campaign AS ca ON ca.id = c.campaign_id
WHERE p.created_at BETWEEN '2022-10-15' AND '2022-10-19' AND ca.id = 1 AND c.deleted = false
GROUP BY c.id
public List<PerformanceStatisticsDto> findByCampaign_IdAndTotalStatisticsDefault(@Param("id") Long campaignId,
@Param("startDate") LocalDate startDate,
@Param("lastDate") LocalDate lastDate
) {
List<PerformanceStatisticsDto> results = jpaQueryFactory
.select(Projections.fields(PerformanceStatisticsDto.class,
performance.view.sum().as("view"),
performance.click.sum().as("click"),
performance.conversion.sum().as("conversion"),
performance.purchase.sum().as("purchase"),
performance.spend.sum().as("spend")
))
.from(performance)
.leftJoin(performance.creative, creative)
.leftJoin(creative.campaign, campaign)
.where(
performance.createdAt.between(startDate, lastDate),
campaign.id.eq(campaignId),
performance.creative.deleted.eq(false)
)
.groupBy(performance.creative.campaign.id)
.fetch();
for (PerformanceStatisticsDto result : results) {
Long spend = result.getSpend();
Long view = result.getView();
Long click = result.getClick();
Long conversion = result.getConversion();
Long purchase = result.getPurchase();
result.setCampaignTotalIndicator(spend, view, click, conversion, purchase);
}
return results;
}