table returns 列数而不是计数

The table returns column number instead of count

我正在使用此查询,但它 returns 列编号而不是计数 count(bedroom_count):

select
  states,
  0,
  1,
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9,
  10
from
  (
    select
      states,
      bedroom_count
    from
      hive_metastore.property_db_dev.datatree_assessor_silver
  ) PIVOT (
    count(bedroom_count) FOR bedroom_count IN (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  )
ORDER BY
  states;

您需要使用 back-ticks 转义列名,否则 Spark 会将它们解释为查询中的整数文字:

SELECT states, `0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`
FROM (
   SELECT states,
          bedroom_count
   FROM   hive_metastore.property_db_dev.datatree_assessor_silver 
) 
PIVOT (
   count(bedroom_count) FOR bedroom_count IN (0,1,2,3,4,5,6,7,8,9,10)
)
ORDER BY states;

或者如果您想要所有旋转列,只需使用 select *

SELECT *
-- ...
ORDER BY states;