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 中是否有任何分组的替代方法?
我被分成两个查询是否有效?
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);