替代MS SQL Server GROUP BY ...在PostgreSQL中使用ROLLUP?

时间:2013-07-02 05:44:44

标签: postgresql postgresql-9.1

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); 

1 个答案:

答案 0 :(得分:2)

如果我正确理解您的问题,您想知道如何在PostgreSQL中执行等效的MS SQL Server扩展WITH ROLLUP,其余查询似乎是无关的噪音。

如果是,according to MSDN, the WITH ROLLUP option adds subtotals。不幸的是,这不是PostgreSQL目前支持的。

PostgreSQL中的解决方法是编写类似:

的内容
WITH my_real_query AS (
    SELECT a, b
    FROM ...
    -- blah blah
)
SELECT 'line', a, b FROM my_real_query
UNION ALL
SELECT 'sum', sum(a), sum(b) FROM my_real_query;

取决于您想要做什么。这需要实现内部表,然后扫描两次,因此它不是非常高效。如果您想按子集进行分组,可以通过向GROUP BY查询的第二部分添加UNION ALL来执行此操作,例如:

WITH my_real_query AS (
    SELECT a, b
    FROM ...
    -- blah blah
)
SELECT a, b FROM my_real_query
UNION ALL
SELECT a, sum(b) FROM my_real_query GROUP BY a;

对于INSERT部分......通常应该使用SQL标准INSERT INTO ... SELECT语法。假设您想将上述内容插入表格中。你写的是:

INSERT INTO some_table(col1,col2)
WITH my_real_query AS (
    SELECT a, b
    FROM ...
    -- blah blah
)
SELECT a, b FROM my_real_query
UNION ALL
SELECT a, sum(b) FROM my_real_query GROUP BY a;

正如您所看到的,INSERT INTO ... [QUERY] [QUERY] WITH ... SELECT也可以#regularMetrics等。

顺便提一下,请阅读postgresql documentation on basic SQL syntaxlexical structure以了解如何为PostgreSQL编写正确的SQL。通常使用ANSI标准样式而不是MS SQL Server的样式将是一个很好的起点。

[Name]"regularMetrics"之类的东西在PostgreSQL中完全无效,它们不是有效的语法。标识符引用是使用双引号完成的,例如"Name",{{1}}。