Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7065949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:01:18+00:00 2026-05-28T05:01:18+00:00

I have searched and searched.. I have a bash script that is used to

  • 0

I have searched and searched.. I have a bash script that is used to run a psql query and email the results on a daily basis. The DB does not update till midnight and my bash script passes a variable to the query for the day prior. I am getting this error only when I use the passed variable otherwise if I put the date in the query manually it runs fine. Not quite sure for I am still learning psql and bash.

Here is the bash script:

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

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@anotheremail.com $RECIPIENT

Here’s the SQL, reformatted for readability.

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

When ran just like this from command line on the server it spits out the error:
ERROR: more than one row returned by a subquery used as an expression

I appreciate the help, this community is the best.. Sorry for the formatting, it came from the copy paste.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:01:18+00:00Added an answer on May 28, 2026 at 5:01 am

    When a subquery in the SELECT part, e.g., (SELECT a, b, (SELECT c from d ...)) of a query is used then this must return one value. An error is returned because one the subqueries returns more than one row. Check all the subqueries to ensure that these do not return more than one row. Add a LIMIT 1 clause if it is acceptable that more than one value exists, but only one is taken.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bash scripting does my head in. I have searched for regex assignment, but not
I have searched around, and it seems that this is a limitation in MS
I have searched far and wide on the Internet but have not found anything
I have searched on the web for software that batch convert image at different
(have searched, but not been able to find a simple solution to this one
I have searched and searched and all I could see was that to use
I have written (tried to) this small bash script for searching through a range
I have searched and found a couple of solutions on this site, that didn't
I have searched the web for hours but I can not find anything about
I have searched and can not find the answer. I double checked the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.