如果与另一列中的列总和值匹配,则获取列总和值

时间:2014-10-17 04:40:14

标签: sql subquery

我有这样的查询:

(
SELECT DISTINCT UniqueBatchNumber AS UBN, 
  count(ID) AS Count, 
  SUM(amount) AS Total, 
  InstrumentType AS IType 
FROM Transactions 
WHERE TransactionDate = '2012-10-01' 
GROUP BY UniqueBatchNumber, InstrumentType HAVING InstrumentType = 'c'
) 
UNION ALL 
(
SELECT DISTINCT 
  UniqueBatchNumber AS UBN, 
  count(ID) AS Count, 
  SUM(amount) AS Total, 
  InstrumentType AS IType 
FROM Transactions 
WHERE TransactionDate = '2012-10-01' 
GROUP BY UniqueBatchNumber, InstrumentType HAVING InstrumentType = 'd'
)

它返回:

UBN  |  Count  | Total       | IType
8237 |  1      | 150689.43   | C
8238 |  26     | 42838.80    | C
8241 |  1      | 2410932.00  | C
8247 |  1      | 100.00      | C
8250 |  1      | 99297.05    | C
8256 |  1      | 1929.00     | C
8259 |  16     | 12623.86    | C
8269 |  1      | 7405022.45  | C
8238 |  1      | 346522.57   | D
8241 |  1      | 303.00      | D
8243 |  1      | 204066.05   | D
8246 |  1      | 100.00      | D
8247 |  1      | 99297.05    | D
8256 |  11     | 847.00      | D
8259 |  1      | 571888.90   | D
8279 |  5      | 3986.75     | D

我想获得IType' c'的批次(UBN)值(或值的总和)。匹配IType d的批次(UBN)值(值的总和)。

例如,信用批量总和( 8247,8250 = 99397.05)将匹配相应的借方批次总和( 8246,8247

我尝试了所有的事物组合

SELECT DISTINCT 
    UniqueBatchNumber AS UBN, 
    COUNT(ID) AS Count, 
    SUM(amount) AS Total
FROM 
    Transactions 
WHERE 
    TransactionDate = '2012-10-01' 
GROUP BY 
    UniqueBatchNumber

但是还没有解决,任何想法?

提前谢谢。

2 个答案:

答案 0 :(得分:0)

这些方面的某些东西能让你接近你正在寻找的东西吗?

SELECT
    C.ubn AS ubn_C,
    D.ubn AS ubn_D,
    C.count AS Count_C,
    D.count AS Count_D,
    C.total AS MatchingTotal
FROM   
    (SELECT DISTINCT 
        uniquebatchnumber AS ubn,
        Count(id) AS count,
        Sum(amount) AS total,
        Instrumenttype AS itype
    FROM            
        transactions
    WHERE           
        transactiondate = '2012-10-01'
        AND instrumenttype = 'c'
    GROUP BY        
        uniquebatchnumber,
        instrumenttype) AS C
JOIN
    (SELECT DISTINCT 
        uniquebatchnumber AS ubn,
        Count(id) AS count,
        Sum(amount) AS total,
        Instrumenttype AS itype
    FROM            
        transactions
    WHERE           
        transactiondate = '2012-10-01'
        AND instrumenttype = 'd'
    GROUP BY        
        uniquebatchnumber,
        instrumenttype) AS D
ON C.Total = D.Total

答案 1 :(得分:0)

试试这个查询。 您完成了90%的工作,只需要在查询中稍微改变一下。

SELECT
    A.UBN,
    SUM(ISNULL(A.Total,0)) Total
FROM
(
    SELECT 
      DISTINCT 
      UniqueBatchNumber AS UBN, 
      count(ID) AS Count, 
      SUM(amount) AS Total, 
      InstrumentType AS IType 
    FROM 
        Transactions 
    WHERE 
        TransactionDate = '2012-10-01' 
    GROUP BY 
        UniqueBatchNumber, 
        InstrumentType 
    HAVING 
        InstrumentType = 'c'

UNION 

    SELECT 
      DISTINCT 
      UniqueBatchNumber AS UBN, 
      count(ID) AS Count, 
      SUM(amount) AS Total, 
      InstrumentType AS IType 
    FROM 
        Transactions 
    WHERE 
        TransactionDate = '2012-10-01' 
    GROUP BY 
        UniqueBatchNumber, 
        InstrumentType 
    HAVING InstrumentType = 'd'
)
AS A
GROUP BY
    A.UBN

我建议您使用UNION而不是UNION ALL Bcoz Union在性能方面比UNION ALL快。

此查询未经过测试。 如果此查询出现任何错误,请回复。