无法正确分组SQL结果

时间:2011-03-02 09:00:24

标签: sql

您好我有一个查询,我需要显示用户每天进行的交易次数,每次交易的金额等值。

下面的查询确实这样做(通过获得平均费率找到欧元等价物),但由于货币不同,我得到的结果是货币而不是总数。查询返回的内容是:

Numb Transactions,Date, userid,transaction_type,total value (per currency),eur_equiv
1                  12/12, 2,     test              5                            10
2                   12/12,2,     test              2                             2

而我希望它返回

Numb Transactions,Date, userid,transaction_type,total value (per currency),eur_equiv
1                  12/12, 2,     test              7                           12

查询如下所示

SELECT COUNT(DISTINCT(ot.ID)) AS 'TRANSACTION COUNTER'
      ,CONVERT(VARCHAR(10) ,ot.CREATED_ON ,103) AS [DD/MM/YYYY]
      ,lad.ci
      ,ot.TRA_TYPE
    ,c.C_CODE
      ,CASE 
            WHEN op.CURRENCY_ID='CURRENCY-002' THEN SUM(CAST(op.IT_AMOUNT AS MONEY)) 
                /(
                     SELECT AVG(CAST(cr.B_RATE AS MONEY)) AS AVG_RATE
                     FROM   C_RATE cr
                     WHERE  cr.CURRENCY_ID = 'CURRENCY-002'
                 )
            WHEN op.CURRENCY_ID='-CURRENCY-005' THEN SUM(CAST(op.IT_AMOUNT AS MONEY)) 
                /(
                     SELECT AVG(CAST(cr.B_RATE AS MONEY)) AS AVG_RATE
                     FROM   C_RATE cr
                     WHERE  cr.CURRENCY_ID = 'CURRENCY-005'
                 )
            WHEN op.CURRENCY_ID='CURRENCY-006' THEN SUM(CAST(op.IT_AMOUNT AS MONEY)) 
                /(
                     SELECT AVG(CAST(cr.B_RATE AS MONEY)) AS AVG_RATE
                     FROM   C_RATE cr
                     WHERE  cr.CURRENCY_ID = 'CURRENCY-006'
                 )
                           ELSE '0'
       END AS EUR_EQUIVAL
FROM   TRANSACTION ot
       INNER JOIN PAYMENT op
            ON  op.ID = ot.ID
       INNER JOIN CURRENCY c
            ON  op.CURRENCY_ID = c.ID
       INNER JOIN ACCOUNT a
            ON  a.ID = ot.ACCOUNT_ID
       INNER JOIN ACCOUNT_DETAIL lad
            ON  lad.A_NUMBER = a.A_NUMBER
       INNER JOIN CUST cus
            ON  lad.CI = cus.CI
WHERE  ot.TRA_TYPE_ID IN ('INBANK-TYPE'
                                 ,'IN-AC-TYPE'
                                 ,'DOM-TRANS-TYPE')
       AND ot.STATUS_ID = 'COMPLETED'
       AND cus.BRANCH IN ('123'
                                      ,'456'
                                      ,'789'
                                      ,'789')
GROUP BY
       lad.CI
      ,CONVERT(VARCHAR(10) ,ot.CREATED_ON ,103)
    ,c.C_CODE
      ,op.CURRENCY_ID
      ,ot.TRAN_TYPE_ID
HAVING SUM(CAST(op.IT_AMOUNT AS MONEY))>'250000.00'
ORDER BY
       CONVERT(VARCHAR(10) ,ot.CREATED_ON ,103) ASC

3 个答案:

答案 0 :(得分:0)

SELECT  MIN([Numb Transactions]
        , Date
        , UserID
        , Transaction_type
        , SUM([Total Value]
        , SUM([Eur Equiv]
FROM    (
          ... -- Your current select (without order by)
        ) q
GROUP BY
        Date
        , UserId
        , Transaction_type                

答案 1 :(得分:0)

问题最常出现在联接的双行中。我所做的是先Select *,然后看看哪些列生成了双行。您可能需要调整双行的JOIN关系才能消失。

如果没有任何结果集,则很难重现您所获得的错误。 检查以下事项:

  • Select *只返回数据上的双行,我将与聚合函数合并。如果此处的答案为“NO”,则需要使用Subselect更改JOIN关系。我在想Account Account detail表。
  • 如果连接不够唯一,某些连接可以创建重复的行。也许你必须在这里加入多项内容,例如JOIN table1 ON table1.ID = table2.EXT_ID and table1.Contact = table2.Contact

答案 2 :(得分:0)

一些事情:

1)考虑使用以下函数:     SELECT AVG(CAST(cr.B_RATE AS MONEY)) AS AVG_RATE FROM C_RATE cr WHERE cr.CURRENCY_ID = 'CURRENCY-002' with the currencyID as a parameter

2)分组在这里工作吗?

3)案件或个人的总和是什么?

 sum(CASE ..) vs sum(cast(op.IT_Amount as money)