使用 SQL Server 2008 中所有重复行的列的总和显示 table 的不同行
Display distinct rows of a table with the sum of a column of all duplicate rows in SQL Server 2008
有两个table:
Tasks
table :
TaskName (PK)
TaskAllocation
table :
AllocationID(PK),
TaskName(F.K to TaskName in 'Tasks' Table),
UserID( F.K to ID in 'Users' Table),
EngineerType( F.K to ID in 'EngineerType' Table),
Start Date,
End Date,
Hours,
Location
'Users' Table :
ID,
FirstName,
LastName
'EngineerTypes' Table :
ID,
Type
每个任务可以有多个allocations.Hence,任务名称可以在任务分配中出现多次table。同一个任务可以映射到多个用户(UserID)
我需要显示所选任务(作为 U.I 的输入给出)、分配给该任务的用户 ID、开始日期的第一次出现、结束日期的第一次出现以及每个用户的总和(小时)所选任务的数量。
示例:TaskAllocation
数据:
TaskName UserID TypeId StartDate EndDate Hours Location
Task1 1 11 Feb 5 Feb 7 1 NULL
Task1 1 11 Feb 6 Feb 7 2 NULL
Task1 1 11 Feb 7 Feb 7 3 Onsite
Task1 2 12 Feb 8 Feb 10 4 Offshore
Task1 2 12 Feb 9 Feb 10 5 NULL
Task1 2 12 Feb 10 Feb 10 6 NULL
'EngineerTypes' data :
ID Type
11 Type1
12 Type2
'Users' Data :
ID FirstName
1 Name1
2 Name2
我执行的查询是:
Select TaskAllocation.UserId as UserId,Users.FirstName as Name,
TaskAllocation.EngineerType as TypeId,EngineerTypes.Type as Type,
min(TaskAllocation.StartDate) as AllocationStartDate,
max(TaskAllocation.EndDate) as AllocationEndDate,
sum(TaskAllocation.Hours) as Hours, TaskAllocation.Location
from TaskAllocation join Users on TaskAllocation.UserId=Users.ID
join EngineerTypes on EngineerTypes.ID = TaskAllocation.EngineerType
where TaskAllocation.TaskName = Task1' group by
FirstName,UserId,EngineerType,Type,AllocationStartDate,AllocationEndDate,Hours,
Location,TaskName order by TaskName, UserId
输出:UserId 名称 TypeId 类型 AllocationStartDate AllocationEndDate 小时位置
1. 126 Name1 11 Type1 2015-11-23 2015-11-25 0.1 NULL
2. 126 Name1 11 Type1 2015-11-24 2015-11-25 0.2 NULL
3. 126 Name1 11 Type1 2015-11-25 2015-11-25 0.3 NULL
4. 127 Name2 12 Type2 2015-11-23 2015-11-25 0.2 NULL
5. 127 Name2 12 Type2 2015-11-24 2015-11-25 0.3 NULL
6. 127 Name2 12 Type2 2015-11-25 2015-11-25 0.4 NULL
您可以尝试以下方法
方法一:
select taskname,
userid,
min(startdate) as'first occurence',
max(enddate) as'last occurence'
,sum(hours)
from t1
group by taskname,userid
方法二:交叉应用
select
distinct taskname,userid,b.*
from t1
cross apply
(select min(startdate) as Firstoccur,max(startdate) as secondocc,sum(hours) as hrs
from t1 t2 where t1.taskname =t2.taskname and t1.userid=t2.userid
group by t2.taskname,t2.userid) b
方法三:
Window 函数
with cte
as
(
select taskname,userid,
min(startdate) over (partition by taskname,userid) as 'first',
max(enddate) over (partition by taskname,userid) as 'second',
sum(hours) over (partition by taskname,userid) as 'hrs',
ROW_NUMBER() over (partition by taskname,userid order by taskname,userid) as rn
from t1
)
select *from cte where rn=1
有两个table:
Tasks
table :
TaskName (PK)
TaskAllocation
table :
AllocationID(PK),
TaskName(F.K to TaskName in 'Tasks' Table),
UserID( F.K to ID in 'Users' Table),
EngineerType( F.K to ID in 'EngineerType' Table),
Start Date,
End Date,
Hours,
Location
'Users' Table :
ID,
FirstName,
LastName
'EngineerTypes' Table :
ID,
Type
每个任务可以有多个allocations.Hence,任务名称可以在任务分配中出现多次table。同一个任务可以映射到多个用户(UserID)
我需要显示所选任务(作为 U.I 的输入给出)、分配给该任务的用户 ID、开始日期的第一次出现、结束日期的第一次出现以及每个用户的总和(小时)所选任务的数量。
示例:TaskAllocation
数据:
TaskName UserID TypeId StartDate EndDate Hours Location
Task1 1 11 Feb 5 Feb 7 1 NULL
Task1 1 11 Feb 6 Feb 7 2 NULL
Task1 1 11 Feb 7 Feb 7 3 Onsite
Task1 2 12 Feb 8 Feb 10 4 Offshore
Task1 2 12 Feb 9 Feb 10 5 NULL
Task1 2 12 Feb 10 Feb 10 6 NULL
'EngineerTypes' data :
ID Type
11 Type1
12 Type2
'Users' Data :
ID FirstName
1 Name1
2 Name2
我执行的查询是:
Select TaskAllocation.UserId as UserId,Users.FirstName as Name,
TaskAllocation.EngineerType as TypeId,EngineerTypes.Type as Type,
min(TaskAllocation.StartDate) as AllocationStartDate,
max(TaskAllocation.EndDate) as AllocationEndDate,
sum(TaskAllocation.Hours) as Hours, TaskAllocation.Location
from TaskAllocation join Users on TaskAllocation.UserId=Users.ID
join EngineerTypes on EngineerTypes.ID = TaskAllocation.EngineerType
where TaskAllocation.TaskName = Task1' group by
FirstName,UserId,EngineerType,Type,AllocationStartDate,AllocationEndDate,Hours,
Location,TaskName order by TaskName, UserId
输出:UserId 名称 TypeId 类型 AllocationStartDate AllocationEndDate 小时位置
1. 126 Name1 11 Type1 2015-11-23 2015-11-25 0.1 NULL
2. 126 Name1 11 Type1 2015-11-24 2015-11-25 0.2 NULL
3. 126 Name1 11 Type1 2015-11-25 2015-11-25 0.3 NULL
4. 127 Name2 12 Type2 2015-11-23 2015-11-25 0.2 NULL
5. 127 Name2 12 Type2 2015-11-24 2015-11-25 0.3 NULL
6. 127 Name2 12 Type2 2015-11-25 2015-11-25 0.4 NULL
您可以尝试以下方法
方法一:
select taskname,
userid,
min(startdate) as'first occurence',
max(enddate) as'last occurence'
,sum(hours)
from t1
group by taskname,userid
方法二:交叉应用
select
distinct taskname,userid,b.*
from t1
cross apply
(select min(startdate) as Firstoccur,max(startdate) as secondocc,sum(hours) as hrs
from t1 t2 where t1.taskname =t2.taskname and t1.userid=t2.userid
group by t2.taskname,t2.userid) b
方法三: Window 函数
with cte
as
(
select taskname,userid,
min(startdate) over (partition by taskname,userid) as 'first',
max(enddate) over (partition by taskname,userid) as 'second',
sum(hours) over (partition by taskname,userid) as 'hrs',
ROW_NUMBER() over (partition by taskname,userid order by taskname,userid) as rn
from t1
)
select *from cte where rn=1