oracle中的SQL查询展平了一个表

时间:2012-06-22 13:30:19

标签: sql oracle oracle11g

所以我在sql中有一个表 看起来像这样

controlnum  taxyear        payrollg                 alaska
12334        2012           16                      25
12334        2012           16                      25
12334        2012           16                      NULL
12334        2012           16                      NULL
12334        2012           16                      25
12334        2012           16                      25
12332        2012           14                      23
12332        2012           14                      NULL
12333        2012           14                      23
12333        2012           14                      NULL

我想像这样把它压扁

controlnum  taxyear        payroll                 alaska
12334        2012           16                      25
12333        2012           14                      23
12332        2012           14                      23

这是我试过的查询,它给了我语法错误 有人可以帮忙吗?

SELECT 
Controlnum "controlNumber",
taxyear "taxYear",
payrollg "payRollGroup"

FROM 
states

MAX (CASE WHEN alaska IS NOT NULL THEN alaska ELSE NULL END) AS 'Alaska'

3 个答案:

答案 0 :(得分:5)

    SELECT 
    Controlnum "controlNumber",
    taxyear "taxYear",
    payrollg "payRollGroup"
    MAX(NVL(alaska, 0)) 'Alaska'--NVL if you want a zero value when only null values exist...
    -- or NVL(MAX(alaska), 0)
    FROM 
    states
    GROUP BY Controlnum, taxyear, payrollg

答案 1 :(得分:1)

我想你想要

SELECT controlNum,
       taxYear,
       payrollG,
       MAX( alaska )
  FROM states
 GROUP BY controlNum,
          taxYear,
          payrollG;

答案 2 :(得分:1)

不知道oracle,但这应该有用。

SELECT Distinct
    Controlnum "controlNumber",
    taxyear "taxYear",
    payrollg "payRollGroup",
    alaska "Alaska"
FROM states
WHERE alaska IS NOT NULL;

修改 - 添加了阿拉斯加专栏