2012-01-17 8 views
0

私は検索して検索しました。私はpsqlクエリを実行し、結果を毎日電子メールで送信するbashスクリプトを持っています。 DBは深夜まで更新されず、bashスクリプトは前日のクエリに変数を渡します。このエラーは、渡された変数を使用する場合にのみ得られます。私はまだpsqlとbashを学んでいるとは確信していません。ここでエラー:式として使用されるサブクエリによって複数の行が返される

はbashスクリプトです:

#!/bin/bash 
NOWDATE=`date +%Y-%m-%d -d "yesterday"` 
SUBDATE=`date '+%B %e, %G'` 
DIR=/file/report/ 
FILE=file-$NOWDATE.csv 
[email protected] 

PGPASSWORD=passwrod psql -w -h host -p 5432 -d database -U user -o $DIR/$FILE <<EOF 
select distinct als."Table_AccountID", 
    (select "Table_val_AccountStatusID" from "Table_log_AccountStatus" 
    where "Table_AccountID" = als."Table_AccountID" order by "Date" desc limit 1) 
    as "Table_val_AccountStatusID", 

    CASE 
    when (select count(*) from "Table_UsageHistory" cfuh 
     where cfuh."Disk">'123456' and date_trunc('day',cfuh."Created") = date_trunc('day','$NOWDATE'::timestamp) 
     -- -'1day':: interval 
     and extrTable('day' from "Created"::timestamp) = ac."DesiredBillingDate" 
     and date_trunc('day', "Created"::timestamp) = date_trunc('day', '$NOWDATE'::timestamp) 
     and cfuh."Table_AccountID" in (
      select distinct "Table_AccountID" from "Table_Usage" 
      where date_trunc('day', "Timestamp"::timestamp) = date_trunc('day','$NOWDATE'::timestamp) 
      and "Table_AccountID" = cfuh."Table_AccountID") 
     and cfuh."Table_AccountID" = als."Table_AccountID") >0 
    then 'Y' 
    else 'N' 
    end as "RollUp", 

    (select distinct bc."ID" from "BIL_BillableCharge" bc, "Table_UsageHistory" cfh 
    where date_trunc('day',bc."Date"::timestamp) = date_trunc('day',cfh."Created"::timestamp) 
    and bc."Table_AccountID" = cfh."Table_AccountID" and bc."BIL_val_InvoiceItemTypeID" = '23' 
    and extrTable('month' from "Created"::timestamp) = extrTable('month' from '$NOWDATE'::timestamp) 
    and extrTable('year' from "Created"::timestamp) = extrTable('year' from '$NOWDATE'::timestamp) 
    and cfh."Table_AccountID" = als."Table_AccountID") as "BillableChargeID" 

    from "Table_log_AccountStatus" als, "Table_Account" ac 
    group by als."Table_AccountID", ac."ID", ac."DesiredBillingDate" 
    having (select distinct "Disk" from "Table_UsageHistory" cfu 
    where date_trunc('day', cfu."Created") = date_trunc('day','$NOWDATE'::timestamp) 
    and ac."ID" = cfu."Table_AccountID")>'123456' 
    and extrTable('day' from '$NOWDATE'::timestamp) = ac."DesiredBillingDate" 
    and ac."ID" = als."Table_AccountID" 
    ORDER BY "RollUp" ASC 
EOF 

sed -i '2d' $DIR/$FILE | 
    mailx -a $DIR/$FILE -s " Report for $SUBDATE" -r [email protected] $RECIPIENT 

ここでは読みやすいように再フォーマットSQLは、です。

select distinct 
    als."Table_AccountID", 

    (select "Table_val_AccountStatusID" 
    from "Table_log_AccountStatus" 
    where "Table_AccountID" = als."Table_AccountID" 
    order by "Date" desc limit 1) as "Table_val_AccountStatusID", 

    CASE when 
     (select count(*) 
     from "Table_UsageHistory" cfuh 
     where cfuh."Disk">'123456' 
      and date_trunc('day',cfuh."Created") = date_trunc('day','$NOWDATE'::timestamp) -- -'1day':: interval 
      and extrTable('day' from "Created"::timestamp) = ac."DesiredBillingDate" 
      and date_trunc('day', "Created"::timestamp) = date_trunc('day', '$NOWDATE'::timestamp) 
      and cfuh."Table_AccountID" in 
       (select distinct "Table_AccountID" 
       from "Table_Usage" 
       where date_trunc('day', "Timestamp"::timestamp) = date_trunc('day','$NOWDATE'::timestamp) 
       and "Table_AccountID" = cfuh."Table_AccountID") 
       and cfuh."Table_AccountID" = als."Table_AccountID") > 0 
     then 'Y' 
     else 'N' 
    end as "RollUp", 

    (select distinct bc."ID" 
    from "BIL_BillableCharge" bc, "Table_UsageHistory" cfh 
    where date_trunc('day',bc."Date"::timestamp) = date_trunc('day',cfh."Created"::timestamp) 
    and bc."Table_AccountID" = cfh."Table_AccountID" and bc."BIL_val_InvoiceItemTypeID" = '23' 
    and extrTable('month' from "Created"::timestamp) = extrTable('month' from '$NOWDATE'::timestamp) 
    and extrTable('year' from "Created"::timestamp) = extrTable('year' from '$NOWDATE'::timestamp) 
    and cfh."Table_AccountID" = als."Table_AccountID") as "BillableChargeID" 

from "Table_log_AccountStatus" als, "Table_Account" ac 
group by als."Table_AccountID", ac."ID", ac."DesiredBillingDate" 
having (select distinct "Disk" 
     from "Table_UsageHistory" cfu 
     where date_trunc('day', cfu."Created") = date_trunc('day','$NOWDATE'::timestamp) 
      and ac."ID" = cfu."Table_AccountID")>'123456' 
    and extrTable('day' from '$NOWDATE'::timestamp) = ac."DesiredBillingDate" 
    and ac."ID" = als."Table_AccountID" 
ORDER BY "RollUp" ASC 

ちょうどそれがエラーを出してくれるサーバーのコマンドラインから次のように走った: ERROR:複数行の式

として使用サブクエリから返された、私は助けに感謝、このコミュニティフォーマットは申し訳ありませんが、コピー貼りから来ました。

+1

あなたは 'select distinct bc。"というIDを "BIL_BillableCharge"から "bc、..."とすれば、ちょうど1つの値になると思いますか?明示的なJOIN条件に切り替えることもできます。 –

+0

日付計算がコメントアウトされています。それは問題の一部ですか? –

+1

@muistooshort:私は、単一の値を返さないかもしれない他のサブクエリは 'select distinct" Disk "..."だと思います。 –

答えて

3

SELECT部分​​のサブクエリ、たとえば(SELECT a, b, (SELECT c from d ...))が使用されている場合、これはの1つの値を返す必要があります。サブクエリが1つ以上の行のを返すため、エラーが返されます。すべてのサブクエリを確認して、複数の行が返されないようにします。複数の値が存在しても1つだけが受け入れられる場合は、LIMIT 1句を追加します。

+0

他のクエリに「複数の行」を追加する方法はありますか? – precose

関連する問題