2017-06-14 55 views
0

我有一張非常大的表格,顯示產品和事件類型的日常記錄。我打算重新排列查詢如何寫在下面,但認爲案例陳述會爲我提供必要的輸出。CASE聲明不提供所需的答案

TABLE_1:

DATES  PRODUCT EVENT 
5/20/2017 APPLE SALE 
5/21/2017 APPLE SALE 
5/22/2017 APPLE SALE 
5/23/2017 APPLE SALE 
5/24/2017 APPLE SALE 
5/25/2017 APPLE SALE 
5/26/2017 APPLE SALE 
5/27/2017 APPLE FULL PRICE 
5/28/2017 APPLE FULL PRICE 
5/29/2017 APPLE FULL PRICE 
5/30/2017 APPLE FULL PRICE 
5/31/2017 APPLE FULL PRICE 
6/1/2017 APPLE FULL PRICE 
6/2/2017 APPLE FULL PRICE 
6/3/2017 APPLE FULL PRICE 
6/4/2017 APPLE FULL PRICE 
6/5/2017 APPLE FULL PRICE 
6/6/2017 APPLE FULL PRICE 
6/7/2017 APPLE LIQUIDATION 
6/8/2017 APPLE LIQUIDATION 
6/9/2017 APPLE LIQUIDATION 
6/10/2017 APPLE LIQUIDATION 
6/11/2017 APPLE LIQUIDATION 
6/12/2017 APPLE LIQUIDATION 
6/13/2017 APPLE LIQUIDATION 
6/14/2017 APPLE LIQUIDATION 
5/20/2017 BANANA SALE 
5/21/2017 BANANA SALE 
5/22/2017 BANANA SALE 
5/23/2017 BANANA SALE 
5/24/2017 BANANA SALE 
5/25/2017 BANANA SALE 
5/26/2017 BANANA SALE 
5/27/2017 BANANA SALE 
5/28/2017 BANANA SALE 
5/29/2017 BANANA SALE 
5/30/2017 BANANA FULL PRICE 
5/31/2017 BANANA FULL PRICE 
6/1/2017 BANANA FULL PRICE 
6/2/2017 BANANA FULL PRICE 
6/3/2017 BANANA FULL PRICE 
6/4/2017 BANANA FULL PRICE 
6/5/2017 BANANA FULL PRICE 
6/6/2017 BANANA FULL PRICE 
6/7/2017 BANANA FULL PRICE 
6/8/2017 BANANA FULL PRICE 
6/9/2017 BANANA FULL PRICE 
6/10/2017 BANANA FULL PRICE 
6/11/2017 BANANA FULL PRICE 
6/12/2017 BANANA FULL PRICE 
6/13/2017 BANANA FULL PRICE 
6/14/2017 BANANA FULL PRICE 

當前查詢:

select 
    product, 
    case when event in ('liquidation') then 'yes' else 'no' end LIQ, 
    count(dates) as days 
FROM TABLE_1 
where dates between '5/20/2017' and '6/14/2017' 
group by 
    product 

我想一個記錄按產品和是/否,如果它的日期範圍內要清盤。這個查詢執行,但得出以下結果:

PRODUCT LIQ   DAYS 
APPLE  NO   18 
APPLE  YES   8 
BANANA NO   26 

我要尋找一個簡單的是或否,如果一個產品在日期2017年5月20日和2017年6月14日之間的清算。這裏是我正在尋找的:

PRODUCT LIQ  DAYS 
APPLE  YES   26 
BANANA  NO   26 

謝謝!

回答

1

下面使用的「是」和「否」的財產「是」>「無」:

select product, 
     max(case when event in ('liquidation') then 'yes' else 'no' end) as LIQ, 
     count(dates) as days 
from TABLE_1 
where dates between '2017-05-20' and '2017-06-14' 
group by product; 
+0

的作品就像一個魅力,謝謝! – Hery0502