2017-04-03 25 views
0

私はSASを初めて使いましたが、SQLコードを使用してproc sqlコードを書こうとすると、PARTITION byはSASで使用できません。やるSQLからPROCへSQL-partition by代替(最小の場合)

Customer_id Item_type Order Size Date  …. 

1. A401 Fruit   Small  3/14/2016 …. 
2. A401 Fruit   Big  5/22/2016 …. 
3. A401 Vegetable  Small  7/12/2016 …. 
4. B509 Vegetable  Small  3/25/2015 …. 
5. B509 Vegetable  Big  3/15/2014 …. 
6. B509 Vegetable  Small  3/1/2014  …. 

説明

Customer_id Item_Type Count  Reason 
1.A401   Fruit  2   X-WRONG-because date corresponding big item is later than others in group 
2.B509   Vegetable 2   RIGHT-Note that count is 2 only because one of the dates is earlier than the Big corresponding item(3/1/2014 is earlier than 3/15/2014) 

SQL出力SQL方言で

Customer_id Item_Type Count  
1.B509   Vegetable  2 
select t.customer_id, t.item_type, count(*) 
from (select t.*, 
     min(case when OrderSize = 'Big' then date end) over (partition by customer_id, item_type) as min_big 
    from t 
) t 
where date > min_big 
group by t.customer_id, t.item_type; 

答えて

1

(MS Accessのは、MySQL、SQLiteの、SASのPROC SQLの場合)。ウィンドウ機能をサポートしていませんほとんどのPARTITION BYコールは、すべての主要なSQL方言でサポートされている相関集約サブクエリで置き換えることができます。以下の調整を考えてみましょう:

select main.customer_id, main.item_type, count(*) as count 
from 
    (select t.customer_id, t.item_type, t.date, 
      (select min(case when OrderSize = 'Big' then date end) 
      from t sub 
      where sub.customer_id = t.customer_id 
      and sub.item_type = t.item_type) as min_big 
    from t 
    ) main 
where main.date > main.min_big 
group by main.customer_id, main.item_type; 
+0

はmain.date doesnotエラーが存在しました。 – viji

+0

おっと!編集を参照してください。派生テーブルの 'SELECT'に* date *を追加してください。あなたのアスタリスクを置き換えると、私はその重要な人を含めることを忘れてしまいます。 – Parfait

関連する問題