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 8358167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:42:49+00:00 2026-06-09T10:42:49+00:00

This query works fine before I try to grab a count of how many

  • 0

This query works fine before I try to grab a count of how many Payments (trans_key) there are against each account (ACCT).

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

SELECT CONVERT(VARCHAR,D.ACCT) ClientReference,
    ISNULL((SELECT S.SD_DATA 
            FROM SDDATA AS S 
            WHERE S.DEBT_KEY=D.DEBT_KEY AND S.SD_KEY=36),' ') Division,
    REPLACE(RTRIM(DB.FNAME)+' '+RTRIM(DB.LNAME),',',' ') AccountHolder,
    CONVERT(VARCHAR,D.DOR,101) ListedDate,
    ISNULL((SELECT SA.SD_DATA 
            FROM SDDATA AS SA 
            WHERE SA.DEBT_KEY=D.DEBT_KEY 
                AND SA.SD_KEY=2),' ') AssignmentType,
    CONVERT(VARCHAR,D.BAL) CurrentBalance,
    CONVERT(VARCHAR,D.ASSIGNED) PlacementBalance,
    CONVERT(VARCHAR,isnull((SELECT -1*SUM(TD.DIST_AMT) 
                            FROM TRANSDIST AS TD,TRANS AS T,TRANS_CODE AS TC 
                            WHERE T.DEBT_KEY=D.DEBT_KEY 
                                AND TD.TRANS_KEY=T.TRANS_KEY  
                                AND TC.TRANS_CODE=T.TRANS_CODE 
                                AND TC.TRANS_BILL=1),0)) AmountRecovered,
    CONVERT(VARCHAR,isnull((SELECT (-1*SUM(TD.DIST_AMT)+SUM(DIST_NET)) 
                            FROM TRANSDIST AS TD,TRANS AS T,TRANS_CODE AS TC 
                            WHERE T.DEBT_KEY=D.DEBT_KEY 
                                AND TD.TRANS_KEY=T.TRANS_KEY  
                                AND TC.TRANS_CODE=T.TRANS_CODE 
                                AND TC.TRANS_BILL=1),0)) AmountApplied,
    CONVERT(VARCHAR,isnull((SELECT (-1*SUM(DIST_NET)) 
                            FROM TRANSDIST AS TD,TRANS AS T,TRANS_CODE AS TC 
                            WHERE T.DEBT_KEY=D.DEBT_KEY 
                                AND TD.TRANS_KEY=T.TRANS_KEY  
                                AND TC.TRANS_CODE=T.TRANS_CODE 
                                AND TC.TRANS_BILL=1),0)) SMAFees,
    COUNT((select trans_key from trans)) as Payments 
FROM DEBT AS D,DEBTOR AS DB,STATUS AS ST
WHERE ST.STAT_KEY=D.STAT_KEY
    AND DB.DEBTOR_KEY=D.DEBTOR_KEY
    AND D.CLIENT_KEY =43 
    AND ST.CATEGORY='A'

When messing around with the count aggregate function I was able to grab soem data, but not the correct count. It was just giving me the count of all ‘Payments’ inside the table not the amount for each ACCT.

  • 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-06-09T10:42:50+00:00Added an answer on June 9, 2026 at 10:42 am

    I feel like your query is written in a very strange way that will make it difficult to debug. A few thoughts:

    • Stop Converting everything you are selecting (at least until you get
      the results you want). This will make your code more concise and
      easier to debug
    • If possible, stop with the subqueries in your select statement. For example:

      ISNULL((SELECT SA.SD_DATA
      FROM SDDATA AS SA
      WHERE SA.DEBT_KEY=D.DEBT_KEY
      AND SA.SD_KEY=2),’ ‘) AssignmentType,

    can be converted to

    ISNULL(SA.SD_DATA,' ')
    ...
    ...
    FROM DEBT AS D
    LEFT JOIN SDDATA AS SA 
    ON sa.DEBT_KEY = d.DEBT_KEY
    
    • Stop using implied joins

    • Read up on how GROUP BY works with aggregate functions.

    This isn’t a solution for your exact problem, but you need to get this query into a workable state and do some basic research if you want to resolve this issue.

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

Sidebar

Related Questions

This query works fine for me: $query = SELECT p.topnode_id, p.param_key, p.param_value FROM tbl_params
I want to port this SQL query, which works just fine on SQL Server,
this query works fine using the php_mssql driver: INSERT INTO Table(columnName) VALUES ('text'); SELECT
I've this query, which works fine except it takes a long while (7 seconds,
This query works: item = db.GqlQuery(SELECT * FROM Item WHERE CSIN = 13)[0] although
I have problems running a dynamic LIKE statement in my project: this query works
I have a problem with a query. This query works with phpMyAdmin, but I
I've got this query that works ok, select * from Materia where Cursar_Cursada=0 and
I know this particular query works, as I tested it with unprepared, procedural methods.
I have this query which works correctly in MySQL. More background on it here

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.