0

Been searching for a few weeks for a solution to this but have come up blank.

I have table of data similar to this:

client_ref  supplier_key  client_amount
1111        GBP           10
1111        GBP           -10
1111        EUR           50
2222        CHF           -22.5
2222        CHF           -20
3333        EUR           -27
3333        EUR           -52
3333        EUR            79

I need to extract all items where the client_ref and supplier_key match and the total of the client_amount equals zero. The output would look like this:

client_ref  supplier_key  client_amount
1111        GBP           10
1111        GBP           -10
3333        EUR           -27
3333        EUR           -52
3333        EUR            79

I have written the following that returns the totals but I need any help you could provide to change this to show the rows that make up the totals rather than just the overall results.

SELECT tbis.client_ref ,tbis.supplier_key ,sum(tbis.client_amount) 
FROM [XXXX].[dbo].[transaction] tbis 
WHERE tbis.tbis.client_amount !=0 
GROUP BY tbis.client_ref, tbis.supplier_key 
HAVING sum(tbis.client_amount) =0 
ORDER BY sum(tbis.client_amount)

Hope this makes sense and my first post is OK. Please feel free to critique my post.

4

6 回答 6

1

试试这个:

SELECT t1.*
FROM transactions AS t1
INNER JOIN
(
  SELECT
    tbis.client_ref ,
    tbis.supplier_key,
    sum(tbis.client_amount) AS total
  FROM transactions tbis 
  WHERE tbis.client_amount !=0 
  GROUP BY tbis.client_ref, tbis.supplier_key 
  HAVING sum(tbis.client_amount) =0 
) AS t2  ON t1.client_ref = t2.client_ref
        AND t1.supplier_key = t2.supplier_key
ORDER BY t2.total;
于 2013-09-30T11:03:58.463 回答
0
SELECT 
    * 
FROM 
    tbis
INNER JOIN
(
    SELECT
        client_ref, supplier_key
    FROM
        tbis
    GROUP by client_ref, supplier_key
    HAVING sum(client_amount) = 0
) match
ON match.client_ref = tbis.client_ref 
AND match.supplier_key = tbis.supplier_key

演示在http://sqlfiddle.com/#!3/a3447/8/0

于 2013-09-30T11:05:56.097 回答
0
SELECT t.* FROM Table1 AS t INNER JOIN
(SELECT [client_ref], [supplier_key], SUM([client_amount]) as Total
FROM Table1 GROUP BY [client_ref], [supplier_key]) AS sumTable
ON t.[client_ref] = sumTable.[client_ref] AND
t.[supplier_key] = sumTable.[supplier_key]
WHERE Total = 0;

样品小提琴

于 2013-09-30T11:12:27.737 回答
0
SELECT client_ref ,supplier_key ,sum(client_amount) 
FROM   transaction
WHERE  client_amount <> 0 
GROUP BY client_ref, supplier_key 
HAVING sum(client_amount) = 0 
于 2013-09-30T11:01:20.860 回答
0

一种可能的方法是使用 SUM() 窗口函数:

SELECT * 
FROM
( SELECT tbis.client_ref ,tbis.supplier_key,tbis.client_amount,
  SUM(tbis.client_amount) OVER (
    PARTITION BY tbis.client_ref, tbis.supplier_key) AS total_client_amount
  FROM [XXXX].[dbo].[transaction] tbis 
  WHERE tbis.client_amount !=0 
)
WHERE total_client_amount = 0

SQL小提琴

于 2013-09-30T11:13:49.520 回答
-1

为您想要的数据创建一个新查询,并通过内部连接将其连接到产生总和的查询,以将其限制为您想要的行。

于 2013-09-30T11:01:11.660 回答