用SQL Server查询累计值
有这样一个要求,它要创建一个SQL Server查询,其中包括基于事件时刻的累计值。典型的例子就是一个银行账户,因为你每一次都是在不同的时间去存钱和取钱。对任意一个账户来说,在某个时间点都要算出它的借(存款)和贷(取款)之和。每一笔交易之后,你都想知道
有这样一个要求,它要创建一个SQL Server查询,其中包括基于事件时刻的累计值。典型的例子就是一个银行账户,因为你每一次都是在不同的时间去存钱和取钱。对任意一个账户来说,在某个时间点都要算出它的借(存款)和贷(取款)之和。每一笔交易之后,你都想知道当前的余额。列表A创建了这样一个表格的简单的例子。
列表A
下面是一些示例行:
1 2006-11-03 02:33:42.34010000.00
2 2006-11-03 02:34:50.467-500.00
3 2006-11-03 02:35:04.857250.00
4 2006-11-03 02:42:19.763-124.25
因为日期是缺省设定的,你要做的就是加入一些金额。例子中为了简单起见,假设只有一个银行账户(为了更符合实际情况,可以添加一个BankAccountNumber列)。
现在你就能够创建一个包括了当前余额的查询。因为你已经在同一列中用正负数值记录了存款和取款值,所以列中的累计值是很容易算出的。你要得到当前的余额,就要计算出以前所有交易的总值,然后将这个值和当前的交易相加。列表B中的查询实现了这一点。
列表B
这样就得到了以下的结果集:
1 2006-11-03 02:33:42.34010000.00 10000.00
2 2006-11-03 02:34:50.467-500.00 9500.00
3 2006-11-03 02:35:04.857250.00 9750.00
4 2006-11-03 02:42:19.763-124.25 9625.75
如这个例子所示,只要你理解了要求,就可以很容易地建立累计值。所示的例子假设表格中只包括一个账户,但是要处理多个账户也很简单。添加一个BankAccountNumber 列和一个指定利息数量的WHERE谓词即可。
你也可以把这个例子倒过来用,如在一个存货计算中创建一个不同的运行。你从存货1,000开始,然后减去各种花销和收据。
这样的查询有两个优点:
你不需要储存结果。当用账户编号或外键操作时,执行可以如闪电般迅速。
你以一个标签结束事务,它可以逐行进行检查。如果出现问题的话,你可以将导致问题出现的事务隔离。
,
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

If you want to check the activation date using an Apple mobile phone, the best way is to check it through the serial number in the mobile phone. You can also check it by visiting Apple's official website, connecting it to a computer, and downloading third-party software to check it. How to check the activation date of Apple mobile phone Answer: Serial number query, Apple official website query, computer query, third-party software query 1. The best way for users is to know the serial number of their mobile phone. You can see the serial number by opening Settings, General, About This Machine. . 2. Using the serial number, you can not only know the activation date of your mobile phone, but also check the mobile phone version, mobile phone origin, mobile phone factory date, etc. 3. Users visit Apple's official website to find technical support, find the service and repair column at the bottom of the page, and check the iPhone activation information there. 4. User

MySQL and PL/SQL are two different database management systems, representing the characteristics of relational databases and procedural languages respectively. This article will compare the similarities and differences between MySQL and PL/SQL, with specific code examples to illustrate. MySQL is a popular relational database management system that uses Structured Query Language (SQL) to manage and operate databases. PL/SQL is a procedural language unique to Oracle database and is used to write database objects such as stored procedures, triggers and functions. same

Title: How to use Oracle to query whether a table is locked? In Oracle database, table lock means that when a transaction is performing a write operation on the table, other transactions will be blocked when they want to perform write operations on the table or make structural changes to the table (such as adding columns, deleting rows, etc.). In the actual development process, we often need to query whether the table is locked in order to better troubleshoot and deal with related problems. This article will introduce how to use Oracle statements to query whether a table is locked, and give specific code examples. To check whether the table is locked, we

Forum is one of the most common website forms on the Internet. It provides users with a platform to share information, exchange and discuss. Discuz is a commonly used forum program, and I believe many webmasters are already very familiar with it. During the development and management of the Discuz forum, it is often necessary to query the data in the database for analysis or processing. In this article, we will share some tips for querying the location of the Discuz database and provide specific code examples. First, we need to understand the database structure of Discuz

How to check the latest price of Tongshen Coin? Token is a digital currency that can be used to purchase in-game items, services, and assets. It is decentralized, meaning it is not controlled by governments or financial institutions. Transactions of Tongshen Coin are conducted on the blockchain, which is a distributed ledger that records the information of all Tongshen Coin transactions. To check the latest price of Token, you can use the following steps: Choose a reliable price check website or app. Some commonly used price query websites include: CoinMarketCap: https://coinmarketcap.com/Coindesk: https://www.coindesk.com/ Binance: https://www.bin
