0

このクエリは、各アカウント (ACCT) に対して存在する支払い (trans_key) の数を取得する前に問題なく動作します。

集計またはサブクエリを含む式に対して集計関数を実行することはできません。

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'

count 集計関数をいじると、soem データを取得できましたが、正しいカウントは取得できませんでした。各ACCTの金額ではなく、テーブル内のすべての「支払い」の数を教えてくれました。

4

1 に答える 1

1

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.

于 2012-08-08T16:20:02.163 に答える