2012-01-17 62 views
0

我已搜索和搜索..我有一個bash腳本,用於運行psql查詢並每天發送電子郵件結果。數據庫不會更新到午夜,我的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

是什麼讓你認爲''從「BIL_BillableCharge」bc,...'選擇不同的bc。「ID」會導致恰好一個值?您可能還想切換到顯式連接條件。 – 2012-01-17 20:04:28

+0

你的日期算術被註釋掉了。這是問題的一部分嗎? – 2012-01-17 20:07:02

+1

@ muistooshort:我認爲可能不會返回單個值的唯一其他子查詢是「select distinct」Disk「...」。 – 2012-01-17 20:15:19

回答

3

當SELECT部分​​中的子查詢(例如,查詢的(SELECT a, b, (SELECT c from d ...)))被使用時,則必須返回一個的值。返回錯誤是因爲子查詢返回多於一行。檢查所有子查詢以確保這些不返回多行。如果存在多個值是可以接受的,則添加LIMIT 1子句,但只有一個值被採用。

+0

有沒有辦法將「多行」追加到另一個查詢? – precose 2013-05-08 19:27:49

相關問題