仅使用最小COUNT()转置行和列(a.k.a. pivot)?

时间:2012-10-31 21:59:23

标签: sql postgresql pivot crosstab

这是我的表'tab_test':

year    animal  price
2000    kittens 79
2000    kittens 93
2000    kittens 100
2000    puppies 15
2000    puppies 32
2001    kittens 31
2001    kittens 17
2001    puppies 65
2001    puppies 48
2002    kittens 84
2002    kittens 86
2002    puppies 15
2002    puppies 95
2003    kittens 62
2003    kittens 24
2003    puppies 36
2003    puppies 41
2004    kittens 65
2004    kittens 85
2004    puppies 58
2004    puppies 95
2005    kittens 45
2005    kittens 25
2005    puppies 15
2005    puppies 35
2006    kittens 50
2006    kittens 80
2006    puppies 95
2006    puppies 49
2007    kittens 40
2007    kittens 19
2007    puppies 81
2007    puppies 38
2008    kittens 37
2008    kittens 51
2008    puppies 29
2008    puppies 72
2009    kittens 84
2009    kittens 26
2009    puppies 49
2009    puppies 34
2010    kittens 75
2010    kittens 96
2010    puppies 18
2010    puppies 26
2011    kittens 35
2011    kittens 21
2011    puppies 90
2011    puppies 18
2012    kittens 12
2012    kittens 23
2012    puppies 74
2012    puppies 79

这里有一些代码用于转换行和列,因此我获得了'小猫'和'小狗'的平均值:

SELECT
    year,
    AVG(CASE WHEN animal = 'kittens' THEN price END) AS "kittens",
    AVG(CASE WHEN animal = 'puppies' THEN price END) AS "puppies"
FROM tab_test
GROUP BY year
ORDER BY year;

上面代码的输出是:

    year    kittens puppies
    2000    90.6666666666667    23.5
    2001    24.0    56.5
    2002    85.0    55.0
    2003    43.0    38.5
    2004    75.0    76.5
    2005    35.0    25.0
    2006    65.0    72.0
    2007    29.5    59.5
    2008    44.0    50.5
    2009    55.0    41.5
    2010    85.5    22.0
    2011    28.0    54.0
    2012    17.5    76.5

我想要的是像第二个表一样的表,但它只包含第一个表中COUNT()至少为3的项目。换句话说,目标是将 this 作为输出:

year    kittens
2000    90.6666666666667

第一张表中至少有3个'小猫'个体 这在PostgreSQL中是否可行?

4 个答案:

答案 0 :(得分:12)

CASE

如果您的案例与演示一样简单,CASE语句将执行:

SELECT year
     , sum(CASE WHEN animal = 'kittens' THEN price END) AS kittens
     , sum(CASE WHEN animal = 'puppies' THEN price END) AS puppies
FROM  (
   SELECT year, animal, avg(price) AS price
   FROM   tab_test
   GROUP  BY year, animal
   HAVING count(*) > 2
   ) t
GROUP  BY year
ORDER  BY year;

无论您是在外部查询中使用sum()max()还是min()作为聚合函数,都无关紧要。在这种情况下,它们都会产生相同的值。

SQL Fiddle

crosstab()

使用更多类别,crosstab()查询会更简单。对于更大的表,这也应该更快。

您需要安装附加模块tablefunc(每个数据库一次)。从Postgres 9.1开始就像这样简单:

CREATE EXTENSION tablefunc;

此相关答案的详细信息:

SELECT * FROM crosstab(
      'SELECT year, animal, avg(price) AS price
       FROM   tab_test
       GROUP  BY animal, year
       HAVING count(*) > 2
       ORDER  BY 1,2'

      ,$$VALUES ('kittens'::text), ('puppies')$$)
AS ct ("year" text, "kittens" numeric, "puppies" numeric);

此网站没有sqlfiddle,因为该网站不允许使用其他模块。

基准

为了验证我的声明,我在我的小型测试数据库中运行了接近真实数据的快速基准测试。 PostgreSQL 9.1.6。使用EXPLAIN ANALYZE进行测试,最好是10:

使用10020行进行测试设置:

CREATE TABLE tab_test (year int, animal text, price numeric);

-- years with lots of rows
INSERT INTO tab_test
SELECT 2000 + ((g + random() * 300))::int/1000 
     , CASE WHEN (g + (random() * 1.5)::int) %2 = 0 THEN 'kittens' ELSE 'puppies' END
     , (random() * 200)::numeric
FROM   generate_series(1,10000) g;

-- .. and some years with only few rows to include cases with count < 3
INSERT INTO tab_test
SELECT 2010 + ((g + random() * 10))::int/2
     , CASE WHEN (g + (random() * 1.5)::int) %2 = 0 THEN 'kittens' ELSE 'puppies' END
     , (random() * 200)::numeric
FROM   generate_series(1,20) g;

结果:

@bluefeet
总运行时间:95.401毫秒

@wildplasser(不同的结果,包括count <= 3行) 总运行时间:64.497毫秒

@Andreiy(+ ORDER BY
&安培; @ Erwin1 - CASE(两者表现相同)
总运行时间:39.105毫秒

@ Erwin2 - crosstab()
总运行时间:17.644毫秒

大比例(但不相关)的结果只有20行。只有@wildplasser的CTE有更多的开销和尖峰。

只有少数行,crosstab()很快就会占据主导地位。 @ Andreiy的查询与我的简化版本大致相同,外部SELECTmin()max()sum()中的聚合函数没有产生任何可衡量的差异(每组只有两行) )。

一切都如预期,没有惊喜,采取我的设置并尝试@home。

答案 1 :(得分:4)

这是@bluefeet's suggestion的替代方法,它有点类似,但避免了连接(相反,上层分组应用于已经分组的结果集):

SELECT
  year,
  MAX(CASE animal WHEN 'kittens' THEN avg_price END) AS "kittens",
  MAX(CASE animal WHEN 'puppies' THEN avg_price END) AS "puppies"
FROM (
  SELECT
    animal,
    year,
    COUNT(*) AS cnt,
    AVG(Price) AS avg_price
  FROM tab_test
  GROUP BY
    animal,
    year
) s
WHERE cnt >= 3
GROUP BY
  year
;

答案 2 :(得分:3)

这就是你要找的东西:

SELECT t1.year,
    AVG(CASE WHEN t1.animal = 'kittens' THEN t1.price END) AS "kittens",
    AVG(CASE WHEN t1.animal = 'puppies' THEN t1.price END) AS "puppies"
FROM tab_test t1
inner join 
(
  select animal, count(*) YearCount, year
  from tab_test
  group by animal, year
) t2
  on t1.animal = t2.animal 
  and t1.year = t2.year
where t2.YearCount >= 3
group by t1.year

请参阅SQL Fiddle with Demo

答案 3 :(得分:2)

CREATE TABLE pussyriot(year INTEGER NOT NULL
        , animal varchar
        , price integer
        );

INSERT INTO pussyriot(year , animal , price ) VALUES
 (2000, 'kittens', 79)
, (2000, 'kittens', 93)
...
, (2007, 'puppies', 81)
, (2007, 'puppies', 38)
        ;

-- a self join is a poor man's pivot:
WITH cal AS ( -- generate calendar file
        SELECT generate_series(MIN(pr.year) , MAX(pr.year)) AS year
        FROM pussyriot pr
        )
, fur AS (
        SELECT distinct year, animal, AVG(price) AS price
        FROM pussyriot
        GROUP BY year, animal
        -- UPDATE: added next line
        HAVING COUNT(*) >= 3
        )
SELECT cal.year
        , pussy.price AS price_of_the_pussy
        , puppy.price AS price_of_the_puppy
FROM cal
LEFT JOIN fur pussy ON pussy.year=cal.year AND pussy.animal='kittens'
LEFT JOIN fur puppy ON puppy.year=cal.year AND puppy.animal='puppies'
        ;