2017-03-07 7 views
0
proc sql; 

select sum(counts) 
from (
     select count(*) "counts" from table1 
     union 
     select count(*) "counts" from table2 
    ); 
quit; 

以下のエラーが表示されます。私はエイリアスを削除しようとしました。 OracleでなぜこのSQLクエリはBase SASで動作しませんか?

ERROR: The SUM summary function requires a numeric argument.
ERROR: The following columns were not found in the contributing tables: counts.

+0

私はそれらが2つの異なるエラーだと思いますか? –

答えて

0

あなたは識別子を使用している場合、あなたは常にカウントが予約語ではない場合、あなたはそれらを完全に削除することができ、それらのidentifiers.Ofコースでカラム(複数可)を参照する必要があります。

select sum("counts") 
from (
     select count(*) "counts" from table1 
     union 
     select count(*) "counts" from table2 
    ); 
quit; 
+0

質問の[tag:oracle]がどこから来たのか分かりませんが、質問ではBase SASについて明確に質問します。これはBase SASでは動作しません。 – Joe

3

SASでは、識別子は通常、引用符で囲まずに使用されます。スペースや他の特殊文字を含む識別子が必要な場合は、"identifier"Nを使用して、引用符で囲まれた文字列が名前("my counts"n以下)であることを示すことができます。

クエリについては、unionではなくunion allを使用してカウントを正しく合計します。 SASのテーブルで

proc sql; 
create table table1 (x num); 
create table table2 (x num); 
proc sql; 
select sum(counts) as "my counts"n 
from (
     select count(*) as counts from table1 
     union all 
     select count(*) from table2 
    ); 
quit; 

、あなたはこのようなSASディクショナリ表からの行番号を使用することによって、より良いパフォーマンスを得る可能性があります:

proc sql; 
select sum(nobs - delobs) as "my counts"n 
from dictionary.tables 
where libname = 'WORK' and memname in ('TABLE1', 'TABLE2'); 
quit; 
1

あなたのコードは、内側COUNT(*)のラベルを結果を与えるが、それされますそれらに変数名を与えていません。

SAS PROC SQLでは、SELECTを実行しているとき、またはSELECT *を行っているときにも変数名を指定しないコードを記述できます。

select count(*) "Total number of rows" from sashelp.class ; 
select * from 
    (select count(*) "Label 1" from sashelp.class 
    union all 
    select count(*) "Label 2" from sashelp.class 
    ) 
; 

これらを使用してデータセットを作成することもできます。この場合、SASは変数名を生成します。

create table test1 as 
    (select count(*) "Total number of rows" from sashelp.class) 
; 
describe table test1 ; 

をしかし、あなたはSUM()のような式で変数を使用する場合、あなたは変数に名前だけでなく、ラベルを与えることがあります。これを試してみてください。それ以外の場合は、集計関数呼び出しの中に何を入れるべきか分かりません。一般的には、派生した変数名を与えることをお勧めします。

select sum(counts) as total_count label="Total of count(*)" from 
    (select count(*) as counts from sashelp.class 
    union all 
    select count(*) as counts from sashelp.class 
    ) 
; 

あなたのPROC SQLステートメントにDQUOTE=ANSIオプションを追加してみてください。これにより、文字列ではなく変数名として"name"が処理されます。しかし、まだ名前の前にASキーワードを追加する必要があります。

proc sql dquote=ansi; 
select sum(counts) 
from (
    select count(*) as "counts" from sashelp.class 
    union 
    select count(*) as "counts" from sashelp.class 
    ) 
; 
quit; 
関連する問題