SQL分析中的MIN的LAG

时间:2015-11-09 10:09:54

标签: sql-server oracle oracle11g analytics window-functions

我有一个包含员工ID,年份ID,客户ID和销售数量的表。例如:

 --------------------------------------
  id_emp | id_year | sales | client id
 --------------------------------------
    4    |    1    |   14  |    1
    4    |    1    |   10  |    2
    4    |    2    |   11  |    1
    4    |    2    |   17  |    2

对于员工,我希望获得每年最低销售额和上一年最低销售额的行。

我尝试的其中一个查询如下:

select distinct 
  id_emp,
  id_year, 
  MIN(sales) OVER(partition by id_emp, id_year) AS min_sales,
  LAG(min(sales), 1) OVER(PARTITION BY id_emp, id_year
                              ORDER BY id_emp, id_year) AS previous
 from facts
where id_emp = 4
group by id_emp, id_year, sales;

我得到了结果:

 -------------------------------------    
  id_emp | id_year | sales | previous
 -------------------------------------
    4    |    1    |   10  |  (null)
    4    |    1    |   10  |    10
    4    |    2    |   11  |  (null)

但我希望得到:

 -------------------------------------
  id_emp | id_year | sales | previous
 -------------------------------------
    4    |    1    |   10  |  (null)
    4    |    2    |   11  |    10

4 个答案:

答案 0 :(得分:2)

你的意思是这样吗?

select id_emp, id_year, min(sales) as min_sales,
    lag(min(sales)) over (partition by id_emp order by id_year) as prev_year_min_sales
from facts
where id_emp = 4
group by id_emp, id_year;

答案 1 :(得分:2)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE EMPLOYEE_SALES ( id_emp, id_year, sales, client_id ) AS
          SELECT 4, 1, 14, 1 FROM DUAL
UNION ALL SELECT 4, 1, 10, 2 FROM DUAL
UNION ALL SELECT 4, 2, 11, 1 FROM DUAL
UNION ALL SELECT 4, 2, 17, 2 FROM DUAL;

查询1

SELECT ID_EMP,
       ID_YEAR,
       SALES AS SALES,
       LAG( SALES ) OVER ( PARTITION BY ID_EMP ORDER BY ID_YEAR ) AS PREVIOUS
FROM   (
  SELECT e.*,
         ROW_NUMBER() OVER ( PARTITION BY id_emp, id_year ORDER BY sales ) AS RN
  FROM   EMPLOYEE_SALES e
)
WHERE rn = 1

查询2

SELECT ID_EMP,
       ID_YEAR,
       MIN( SALES ) AS SALES,
       LAG( MIN( SALES ) ) OVER ( PARTITION BY ID_EMP ORDER BY ID_YEAR ) AS PREVIOUS
FROM   EMPLOYEE_SALES
GROUP BY ID_EMP, ID_YEAR

Results - 两者都提供相同的输出

| ID_EMP | ID_YEAR | SALES | PREVIOUS |
|--------|---------|-------|----------|
|      4 |       1 |    10 |   (null) |
|      4 |       2 |    11 |       10 |

答案 2 :(得分:0)

您可以使用 ROW_NUMBER() LAG()分析函数获得所需的输出。

例如,

表格

SQL> SELECT * FROM t;

    ID_EMP    ID_YEAR      SALES  CLIENT_ID
---------- ---------- ---------- ----------
         4          1         14          1
         4          1         10          2
         4          2         11          1
         4          2         17          2

<强>查询

SQL> WITH DATA AS
  2    (SELECT t.*,
  3      row_number() OVER(PARTITION BY id_emp, id_year ORDER BY sales) rn
  4    FROM t
  5    )
  6  SELECT id_emp,
  7    id_year ,
  8    sales ,
  9    lag(sales) over(order by sales) previous
 10  FROM DATA
 11  WHERE rn =1;

    ID_EMP    ID_YEAR      SALES   PREVIOUS
---------- ---------- ---------- ----------
         4          1         10
         4          2         11         10

答案 3 :(得分:0)

我认为这是因为您在分组声明中使用了销售列。 尝试删除它,然后使用

  GROUP BY id_emp,id_year