sql - oracle - 选择特定组/ sku的第一个和最后一个记录

时间:2014-08-01 03:03:00

标签: sql oracle

我一直试图回答但没有成功。

需要获得:首次购买的价格和上次购买的价格,并按SKU分组。

查询结果应如下所示:

sku      first_purchase_price      Last_purchase_price   
BC123    3.09                      6.68    
QERT1    9.09                      13.23

我的查询

SELECT sku,PRICE,MAX(purchase_DATE),MIN (purchase_DATE)
FROM store
ORDER By sku

继续:

SQL Error: ORA-00979: not a GROUP BY expression
00979. 00000 -  "not a GROUP BY expression"

SQL Error: ORA-00937: not a single-group group function
00937. 00000 -  "not a single-group group function"

非常感谢任何帮助。

SKU     TRANSAC_ID  purchase_DATE      PRICE
----------------------------------------------
BC123   CHI0018089  21-OCT-09          6.98
BC123   CHI0031199  11-MAR-13          6.68 
BC123   NAP1000890  22-JAN-08          3.09 
BC123   NAP1011123  21-DEC-11          89.9 
QQQ789  NAP1000891  22-JAN-08          4.01 
QERT1   JOL0400090  8-MAR-12           13.23 
QERT1   NAP1000990  22-FEB-08          9.09 
QERT1   NAP1001890  28-FEB-09          2.09 
WW000   CHI0031208  11-MAR-13          200.01 
WW000   CHI0031298  11-MAR-13          200.01 
YZV11   JOL0200080  10-OCT-06          230.23 
YZV11   AUR0700979  14-APR-13          6.68 
YZV11   CHI0018189  03-OCT-09          556.98 
YZV11   JOL0300080  10-MAR-11          300   

4 个答案:

答案 0 :(得分:5)

您可以使用FIRST/LAST聚合函数 简化此类查询。

SQL Fiddle

<强>查询

select
        sku,
        max(price) keep (dense_rank first order by purchase_date) first_purchase_price,
        max(price) keep (dense_rank last order by purchase_date) last_purchase_price
from
        store
group by
        sku;

<强> Results

|    SKU | FIRST_PURCHASE_PRICE | LAST_PURCHASE_PRICE |
|--------|----------------------|---------------------|
|  BC123 |                 3.09 |                6.68 |
|  QERT1 |                 9.09 |               13.23 |
| QQQ789 |                 4.01 |                4.01 |
|  WW000 |               200.01 |              200.01 |
|  YZV11 |               230.23 |                6.68 |

答案 1 :(得分:0)

如果您的意图是,您的查询似乎确实缺少GROUP BY子句 获取特定值“sku”的最大和最小“purchase_DATE”值。 您需要对要引用的列进行分组以获取最大值和最小值。 使用上面的示例数据,您可以尝试此查询:

SELECT sku, MAX(purchase_DATE), MIN (purchase_DATE)
FROM store
GROUP BY sku
ORDER by sku

可以为您提供每个SKU值的最大和最小购买日期,例如:

BC123   2013-03-11  2000-01-22   -- max and min "purchase dates" for "BC123" sku
QERT1   2012-03-08  2008-02-22   -- ...                              "QERT1" sku
QQQ789  2008-01-22  2008-01-22   -- ...                             "QQQ789" sku
...                                 ...                                ...

如果您想要某个“sku”的最低和最高价格,您可以使用非常相似的查询。

答案 2 :(得分:0)

WITH CTE AS
(
SELECT SKU, MAX(PURCHASE_DATE) MAXDATE, MIN(PURCHASE_DATE) MINDATE
FROM STORE
GROUP BY SKU
)
SELECT DISTINCT CTE.SKU, MINVAL.PRICE MINPRICE, MAXVAL.PRICE MAXPRICE
FROM STORE MINVAL, STORE MAXVAL, CTE
WHERE
CTE.SKU = MINVAL.SKU
AND CTE.MINDATE = MINVAL.PURCHASE_DATE
AND CTE.SKU = MAXVAL.SKU
AND CTE.MAXDATE = MAXVAL.PURCHASE_DATE
ORDER BY SKU

小提琴here

答案 3 :(得分:0)

试试这个:

with cte as
(select distinct sku,
 min(purchase_date) over (partition by sku order by sku) mindate,
 max(purchase_date) over (partition by sku order by sku) maxdate
 from store)

select distinct c.sku, 
smin.price first_purchase_price, 
smax.price last_purchase_price
from cte c
inner join store smin on c.mindate = smin.purchase_date and c.sku = smin.sku
inner join store smax on c.maxdate = smax.purchase_date and c.sku = smax.sku
order by c.sku

SQLFiddle