SELECT Category ,CASE WHEN GROUPING([Category]) = 0 THEN [Category]
ELSE 'Total Shown' END AS [Category]
,SUM([Impressions]) AS [Impressions]
,SUM([Clicks]) AS [Clicks]
,CASE WHEN SUM([Impressions]) = 0 THEN NULL ELSE SUM([Clicks]) / (SUM([Impressions]) * 1.0) END AS [CTR]
,CASE WHEN SUM([Clicks]) = 0 THEN NULL ELSE SUM([Cost]) / (SUM([Clicks]) * 1.0) END AS [CPClick]
,SUM([Cost]) AS [Spend]
,SUM([Transactions]) AS [Transactions]
,SUM([Conversions]) AS [Conversions]
,CASE WHEN SUM([Clicks]) = 0 THEN NULL ELSE SUM([Transactions]) / (SUM([Clicks]) * 1.0) END AS [Trans Rate]
,CASE WHEN SUM([Clicks]) = 0 THEN NULL ELSE SUM([Actions]) / (SUM([Clicks]) * 1.0) END AS [Action Rate]
,SUM([Actions]) AS [Actions]
,Grouping( Category ) as TotalShown
INTO #regularMetrics FROM #tmp
GROUP BY Category WITH ROLLUP
ORDER BY TotalShown,[Impressions] desc, [Category]
postgresqlでグループ化の代替手段はありますか??
私は2つのクエリに分割されていますが、効率的ですか?
SELECT [Category ] , SUM([Impressions]) AS [Impressions] ,SUM([Clicks]) AS [Clicks] ,CASE WHEN SUM([Impressions]) = 0 THEN NULL ELSE SUM([Clicks]) /
(SUM([Impressions]) * 1.0) END AS [CTR] ,CASE WHEN SUM([Clicks]) = 0 THEN NULL ELSE SUM([Cost]) / (SUM([Clicks]) * 1.0) END AS [CPClick] ,SUM([Cost]) AS [Spend]
,SUM([Transactions]) AS [Transactions] ,SUM([Conversions]) AS [Conversions] ,CASE WHEN SUM([Clicks]) = 0 THEN NULL ELSE SUM([Transactions]) / (SUM([Clicks]) * 1.0)
END AS "Trans Rate" ,CASE WHEN SUM([Clicks]) = 0 THEN NULL ELSE SUM([Actions]) / (SUM([Clicks]) * 1.0) END AS "Action Rate" ,SUM([Actions]) AS [Actions] , 0 as TotalShown
INTO #regularMetrics FROM #tmp GROUP BY Category ORDER BY Category ,[Impressions] desc;
insert into #regularmetrics(select 'Total Shown', sum(impressions), sum(clicks), CASE WHEN SUM([Impressions]) = 0 THEN NULL ELSE SUM([Clicks]) / (SUM([Impressions]) * 1.0) END AS [CTR], CASE WHEN SUM([Clicks]) = 0 THEN NULL ELSE SUM([spend]) / (SUM([Clicks]) * 1.0) END AS [CPClick], sum(spend),sum(transactions),sum(conversions), CASE WHEN SUM([Clicks]) = 0 THEN NULL ELSE SUM([Transactions]) / (SUM([Clicks]) * 1.0) END AS "Trans Rate" ,CASE WHEN SUM([Clicks]) = 0 THEN NULL ELSE SUM([Actions]) / (SUM([Clicks]) * 1.0) END AS "Action Rate", sum(actions), 1 from #regularmetrics);