如何使用Sql获得两个日期之间的最小金额?

时间:2017-12-29 10:03:46

标签: sql sql-server sql-server-2008

我需要一些逻辑来获得两个日期之间的最小金额,我的表格如

create table #tmp(id int,deposit_id int,transaction_amount decimal(15,2),trn_date date,trn_type varchar(50));
insert into #tmp
select 1,101,15000,'2017-12-01','Receipt' -- 15000
union all
select 2,101,-5000,'2017-12-09','Payment' -- 10000
union all
select 3,101,6000,'2017-12-16','Receipt'   --16000
union all
select 4,101,-15000,'2017-12-20','Payment' -- 1000
union all
select 5,101,10000,'2017-12-22','Receipt' --11000
union all
select 5,101,-1000,'2017-12-22','Payment' --10000
union all
--- another deposit details
select 55,102,50000,'2017-12-04','Receipt' -- 50000
union all
select 56,102,5000,'2017-12-10','Receipt' -- 55000

我想在2017-12-01' 2017-12-01之间保持最低的每笔存款余额。并且' 2017-12-31'。我无法获得查询的逻辑以获得此结果,因为我如何在每天检查平衡,例如在2017-12-24'。请给出一些提示。我想要的结果是

deposit_id   date   LeastAmt
    101  - '2017-12-20'  - 1000
    102  - '2017-12-04'  - 50000

4 个答案:

答案 0 :(得分:3)

这个问题可以分为两部分。首先,我们需要计算存款表的滚动余额。然后,我们需要在给定的日期范围内找到每个帐户的最小余额。

WITH cte AS (
    SELECT d1.*,
        (SELECT SUM(d2.transaction_amount) FROM deposits d2
         WHERE d1.deposit_id  = d2.deposit_id AND d2.trn_date <= d1.trn_date) bal
    FROM deposits d1
)

SELECT
    id, deposit_id, transaction_amount, bal, trn_date, trn_type
FROM
(
    SELECT *, ROW_NUMBER() OVER (PARTITION BY deposit_id ORDER BY bal) rn
    FROM cte
    WHERE trn_date BETWEEN '2017-12-01' AND '2017-12-31'
) t
WHERE t.rn = 1;

答案 1 :(得分:2)

这是另一种解决方案。

CREATE TABLE #tmp(id int,deposit_id int,transaction_amount decimal(15,2),trn_date date,trn_type varchar(50));

INSERT INTO #tmp
SELECT 1,101,15000,'2017-12-01','Receipt' 
UNION ALL
SELECT 2,101,-5000,'2017-12-09','Payment' 
UNION ALL
SELECT 3,101,6000,'2017-12-16','Receipt'   
UNION ALL
SELECT 4,101,15000,'2017-12-20','Payment'
UNION ALL
SELECT 5,101,10000,'2017-12-20','Receipt'
UNION ALL
SELECT 55,102,50000,'2017-12-04','Receipt'
UNION ALL
SELECT 56,102,5000,'2017-12-10','Receipt'

SELECT id, date, transaction_amount 
FROM (
    SELECT trn_date AS date, MIN(transaction_amount) AS LeastAmt
    FROM #tmp 
    WHERE trn_date BETWEEN '2017-12-01' AND '2017-12-31'
    GROUP BY trn_date
    ) A
    INNER JOIN #tmp ON trn_date = A.date AND transaction_amount = A.LeastAmt
ORDER BY id

DROP TABLE #tmp 

答案 2 :(得分:0)

ORDER BY列上使用balance,如下所示:

注意:我给出了mysql的例子,你可以为你的数据库服务器做同样的逻辑。

SELECT balance 
FROM table_name 
WHERE date_created BETWEEN date1 AND date2 
ORDER BY balance limit 1;

如果值是来自union的动态,则可以使用like:

SELECT balance 
FROM (
    SELECT bal1, date_created 
    FROM table1 
    UNION 
    SELECT bal2, date_created 
    FROM table2 
    UNION...
) AS A 
WHERE date_created BETWEEN date1 AND date2 
ORDER BY balance limit 1; 

答案 3 :(得分:0)

试试这个:

SELECT deposit_id,trn_date,transaction_amount 
FROM tmp t 
WHERE trn_date >= '2017-12-01' AND 
    trn_date <= '2017-12-31' AND
    transaction_amount = (
        SELECT MIN(transaction_amount) 
        FROM tmp 
        WHERE t.deposit_id=deposit_id AND 
            trn_date >= '2017-12-01' AND 
            trn_date <= '2017-12-31')