SQL查询维持余额

时间:2019-01-11 13:43:10

标签: sql sql-server tsql

我有一个带有列的表

customer ID, Account ID, Transaction Date, balance

我想查询该表并获取在任何给定月份和整个月份中维护$500的所有帐户。如果该帐户除一天以外的整个月的余额都为$500,那么该帐户将失去资格。

如何查询表格?

3 个答案:

答案 0 :(得分:0)

获取all的余额并将其与500进行比较:

DECLARE @mydate datetime = '2018-12-01';

select 
  distinct accountid 
from 
  tablename t
where
  month(t.transactiondate) = month(@mydate) 
  and 
  year(t.transactiondate) = year(@mydate)
  and
  500 <= all (
    select 
      tablename.balance 
    from tablename 
    where
      tablename.accountid = t.accountid
      and 
      month(tablename.transactiondate) = month(@mydate) 
      and 
      year(tablename.transactiondate) = year(@mydate)
  )
union
select t.accountid from (
  select 
    accountid, 
    max(transactiondate) maxtransactiondate 
  from tablename t
  where
    not exists (
      select 
        1 
      from tablename 
      where
        tablename.accountid = t.accountid
        and 
        month(tablename.transactiondate) = month(@mydate) 
        and 
        year(tablename.transactiondate) = year(@mydate)
    )
    and
    transactiondate < @mydate
  group by accountid) t
where (
  select tablename.balance 
  from 
    tablename
  where 
    tablename.accountid = t.accountid
    and
    tablename.transactiondate = t.maxtransactiondate
) >= 500

请参见demo

答案 1 :(得分:0)

首先,您的表中填充了太多的信息。我建议您创建三个表,一个用于客户,一个用于帐户,一个用于交易:

create table tblCustomers
(
    CustomerID int IDENTITY(1,1) NOT NULL,
    CustomerFirstName nvarchar(50),
    CustomerLastName nvarchar(50) NOT NULL,
    ...
)

create table tblAccounts
(
    AccountID int IDENTITY(1,1) NOT NULL,
    AccountNo nvarchar(max) NOT NULL,
    AccountBalance decimal NOT NULL,
    ...
)

create table tblTransactions
(
    TransactionID int IDENTITY(1,1) NOT NULL,
    TransactionFrom int NOT NULL,
    TransactionTo int NOT NULL,
    TransactionDateTime datetime NOT NULL,
    TransactionAmount int NOT NULL
)

这样,您实际上可以来回预定。 现在,您需要一个额外的表格,您可以在其中实际存储每个帐户每天的余额:

create table tblAccountHistory
(
    AccountHistoryID int IDENTITY(1,1) NOT NULL,
    AccountFSID int NOT NULL,
    Balance decimal NOT NULL,
    SnapshotDate datetime NOT NULL,
    IsBelow bit NOT NULL
)

尽管如此,您每天都必须记下余额。 看起来可能像这样:

CREATE PROCEDURE spInsertAccountBalances

AS   

    SET NOCOUNT ON;  
    insert into dbo.tblAccountHistory (AccountFSID, Balance, SnapshotDate, IsBelow)
    Select AccountID, AccountBalance, GETDATE(), 0
    From 
    dbo.tblAccounts

    update dbo.tblAccountHistory
    set
    IsBelow = 1
    where Balance <= 499.99
GO  

只需使用Windows和Powershell中的Task Scheduler每天运行一次即可。

然后您可以创建查询以获取所有这些值

Select ac.AccountID, ac.AccountNo, Count(ah.IsBelow)
From dbo.tblAccounts ac
inner join
dbo.tblAccountHistory ah on ac.AccountID = ah.AccountFSID
Where 
((Month(ah.SnapshotDate) = (Month(GETDATE()) - 1) and (Year(ah.SnapshotDate) = (Year(GETDATE()))
and
Count(ah.IsBelow) < 1

答案 2 :(得分:0)

您想查看一个月,例如2019年1月。您想查找该月所有未低于500 $的帐户。

为此,我们必须首先找到初始值。如果输入的日期为1月1日,则很好,否则,我们必须回头查找之前的最后日期。因此,这是我们正在寻找的1月2日之前的最长日期。

select account_id
from transactions t1
where transaction_date >=
(
  select max(t2.transaction_date)
  from transactions t2
  where t2.account_id = t1.account_id
  and t2.transaction_date < '20190102'
)
and transaction_date < '20190201'
group by account_id
having min(transaction_date) >= 500;

根据您要检查的月份更改日期文字。