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

1 回答 1

2

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

如果是这样,根据 MSDN,该WITH ROLLUP选项会添加 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;

取决于你想要做什么。这需要内部表被物化然后扫描两次,所以它并不是非常高效。如果要按子集进行分组,可以通过将 a 添加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

顺便说一句,请阅读有关基本 SQL 语法词法结构的 postgresql 文档,以了解如何为 PostgreSQL 编写正确的 SQL。一般来说,使用 ANSI 标准样式而不是 MS SQL Server 的样式将是一个很好的起点。

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

于 2013-07-02T06:31:58.207 回答