SQLServer树型求和
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- this document design by zzl -- -- function : total about a tree structure -- author : Lori.zhang ,zzl -- display view : a1 3 -- a11 2 -- a111 1 ------------------------------------------ ALT
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- this document design by zzl --
-- function : total about a tree structure
-- author : Lori.zhang ,zzl
-- display view : a1 3
-- a11 2
-- a111 1
------------------------------------------
ALTER proc [dbo].[zzl_task_tree_total] --通过指定的taskid,来获取它和它下面的所有子元素的汇总和
@taskid int
as
declare @level_tt table(taskid nvarchar(1000),parentID nvarchar(1000),level int) --声明一个表变更,ID,上级ID,及层次
declare @level int
set @level=0
insert @level_tt(taskid,parentID,level) --插入到表变量@level_tt,将所有上级ID为null或是为0的记录
select taskid,parentID,@level from task where isnull (parentID,'')=''
while @@ROWCOUNT>0 --当存在这样的记录时
begin
set @level=@level+1 --层次变更自加1
insert @level_tt(taskid,parentID,level) --插入到表变更@level_tt,将所有的task表的上级ID等于@level_tt表的ID并且是它的低一级的记录
select a.taskid,cast(b.parentID as varchar)+cast(a.taskid as varchar),@level
from task a,@level_tt b
where a.parentID=b.taskid and b.level=@level-1
end
select * from @level_tt --显示级联的结构
-- 上面程序执行的结果为:
-- taskid parentid level
-- 1 0 0
-- 2 01 1
-- 3 01 1
-- 4 012 2
-- 从上面的结果可以看到,表中的1,2,4有着关系,他们是树型结果的,如果想求1的结果,需要将2和4的结果相加
declare @tmp table(taskid int,parentid int,total float,moneyTotal float) --再定义表变更@tmp
insert into @tmp --插入到@tmp表,从task表,@level_tt表,求和对象为realmoney,当realmoney为空时,把它设为0,求和条件为
select a.taskid,a.parentID,SUM(isnull(c.realwork,0)) as total,SUM(isnull(c.realmoney,0)) as moneytotal --parentid为parentid%,使用通配符
from task a,@level_tt b,Task c,@level_tt d
where (a.taskid=b.taskid and
c.taskid=d.taskid
and d.parentID like b.parentID+'%' )
and (a.taskid=@taskid)
group by a.taskid,a.parentID
order by a.parentID
--select @total=total from @tmp
if @@rowcount=0 --如果没有找到记录,就向临时表插入空记录
begin
insert into @tmp(taskid,parentid,total,moneytotal)values(0,0,0,0) --如果没有记录,就插入一个0记录
end
select total,moneytotal from @tmp --选择临时表

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



For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

In Python, tuples are immutable sequences that can store multiple elements of different types. They are often used to represent collections of related values. Tuple summation involves adding the corresponding elements of two or more tuples to produce a new tuple. However, in some scenarios, it may be necessary to calculate the absolute sum of elements instead of the traditional sum. In this blog post, we will explore how to perform absolute tuple sums in Python. Traditional Tuple Sum Before we delve into absolute tuple sum, let’s first understand how to do traditional tuple sum. Given two tuples of the same length, we can use a simple Python loop or list comprehension to calculate the sum of the corresponding elements −deftuple_sum(t1,t2):

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati
