1

所以我在 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'
4

3 回答 3

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
于 2012-06-22T13:33:01.970 回答
1

我想你想要

SELECT controlNum,
       taxYear,
       payrollG,
       MAX( alaska )
  FROM states
 GROUP BY controlNum,
          taxYear,
          payrollG;
于 2012-06-22T13:32:51.460 回答
1

不知道oracle,但这应该可以。

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

编辑- 添加了阿拉斯加专栏

于 2012-06-22T13:35:41.947 回答