相同结果合并

细嗅蔷薇 2014-07-28 05:47:46
  create table ReceiverRecord
(
ID int identity(1,1) primary key not null,
[Typeid] int
,[ReceiverNumber] int
,[UserId] int
,[ReceiverDateTime] datetime
,TypeName nvarchar(100)
,EmpName nvarchar(100)
,BureauName nvarchar(100)
)


上面是查询出来的结果集,我想把typeid相同的 number相加合并。可以根据部门来查询。求告知。
...全文
125 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiajiaren 2014-07-29
  • 打赏
  • 举报
回复
引用 10 楼 lovesheng1212 的回复:
不加where条件的,就是按typeid跟 empname来分组 [quote=引用 9 楼 DBA_Huangzj 的回复:] 你的ReceiverNumber 没按照typeid合并啊 [quote=引用 7 楼 lovesheng1212 的回复:] [quote=引用 6 楼 DBA_Huangzj 的回复:] 你直接给出期待结果吧,我都不知道你想要什么
/* id typeid ReceiverNumber userid ReceiverDateTime TypeName EmpName BureauName ----------- ----------- -------------- ----------- ----------------------- -------- ------- ---------- 15 20 25 30 2014-07-16 19:42:29.000 记号笔 张三 技术部 22 19 32 14 2014-07-21 15:10:05.000 白板笔 王尼玛 广告部 25 20 22 14 2014-07-28 17:30:00.000 记号笔 王尼玛 广告部 */[/quote][/quote][/quote]
IF OBJECT_ID('[ReceiverRecord]') IS NOT NULL 
    DROP TABLE [ReceiverRecord]
go 
CREATE TABLE [ReceiverRecord]
    (
      [ID] INT ,
      [TypeID] INT ,
      [ReceiverNumber] INT ,
      [UserID] INT ,
      [ReceiverDateTime] DATETIME ,
      [TypeName] VARCHAR(6) ,
      [EmpName] VARCHAR(6) ,
      [BureauName] VARCHAR(6)
    )
insert [ReceiverRecord]
select 15,20,10,30,'2014-07-16 19:42:29','记号笔','张三','技术部' union all
select 22,19,11,14,'2014-07-21 15:10:05','白板笔','王尼玛','广告部' union all
select 23,19,21,14,'2014-07-28 16:17:00','白板笔','王尼玛','广告部' union all
select 24,20,15,30,'2014-07-28 17:00:00','记号笔','张三','技术部' union all
select 25,20,22,14,'2014-07-28 17:30:00','记号笔','王尼玛','广告部'

GO
----------------------------------------------查询-------------------------------------
SELECT  MIN(id) AS id,typeid,SUM(ReceiverNumber) ReceiverNumber,MAX(userid) userid,
min(ReceiverDateTime) ReceiverDateTime,TypeName,EmpName,MAX(BureauName) BureauName  
 FROM [ReceiverRecord] GROUP BY typeid,TypeName,EmpName ORDER BY MIN(id)
 
----------------------------------------------结果-------------------------------------
/* 
id          typeid      ReceiverNumber userid      ReceiverDateTime        TypeName EmpName BureauName
----------- ----------- -------------- ----------- ----------------------- -------- ------- ----------
15          20          25             30          2014-07-16 19:42:29.000 记号笔      张三      技术部
22          19          32             14          2014-07-21 15:10:05.000 白板笔      王尼玛     广告部
25          20          22             14          2014-07-28 17:30:00.000 记号笔      王尼玛     广告部
*/
發糞塗牆 2014-07-29
  • 打赏
  • 举报
回复
按你说的,那么ReceiverNumber不是你那个哦
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(發糞塗牆)
-- Date    :2014-07-29 08:15:22
-- Version:
--      Microsoft SQL Server 2012 - 11.0.5058.0 (X64) 
--	May 14 2014 18:34:29 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[ReceiverRecord]
if object_id('[ReceiverRecord]') is not null drop table [ReceiverRecord]
go 
create table [ReceiverRecord]([ID] int,[TypeID] int,[ReceiverNumber] int,[UserID] int,[ReceiverDateTime] datetime,[TypeName] varchar(6),[EmpName] varchar(6),[BureauName] varchar(6))
insert [ReceiverRecord]
select 15,20,10,30,'2014-07-16 19:42:29','记号笔','张三','技术部' union all
select 22,19,11,14,'2014-07-21 15:10:05','白板笔','王尼玛','广告部' union all
select 23,19,21,14,'2014-07-28 16:17:00','白板笔','王尼玛','广告部' union all
select 24,19,15,30,'2014-07-28 17:00:00','记号笔','张三','技术部' union all
select 25,20,22,14,'2014-07-28 17:30:00','记号笔','王尼玛','广告部'
--------------开始查询--------------------------

SELECT  a.id ,
        a.typeid ,
        b.[ReceiverNumber] ,
        a.userid ,
        a.ReceiverDateTime ,
        a.TypeName ,
        a.EmpName ,
        a.BureauName
FROM    [ReceiverRecord] a
        INNER JOIN ( SELECT  typeid ,EmpName ,
                            SUM([ReceiverNumber]) [ReceiverNumber]
                    FROM    ReceiverRecord
                    GROUP BY typeid,EmpName 
                  ) b ON a.typeid = b.typeid AND a.[ReceiverNumber]=b.[ReceiverNumber]

----------------结果----------------------------
/* 
id          typeid      ReceiverNumber userid      ReceiverDateTime        TypeName EmpName BureauName
----------- ----------- -------------- ----------- ----------------------- -------- ------- ----------
15          20          10             30          2014-07-16 19:42:29.000 记号笔      张三      技术部
24          19          15             30          2014-07-28 17:00:00.000 记号笔      张三      技术部
25          20          22             14          2014-07-28 17:30:00.000 记号笔      王尼玛     广告部
*/
细嗅蔷薇 2014-07-29
  • 打赏
  • 举报
回复
不加where条件的,就是按typeid跟 empname来分组
引用 9 楼 DBA_Huangzj 的回复:
你的ReceiverNumber 没按照typeid合并啊 [quote=引用 7 楼 lovesheng1212 的回复:] [quote=引用 6 楼 DBA_Huangzj 的回复:] 你直接给出期待结果吧,我都不知道你想要什么
/* id typeid ReceiverNumber userid ReceiverDateTime TypeName EmpName BureauName ----------- ----------- -------------- ----------- ----------------------- -------- ------- ---------- 15 20 25 30 2014-07-16 19:42:29.000 记号笔 张三 技术部 22 19 32 14 2014-07-21 15:10:05.000 白板笔 王尼玛 广告部 25 20 22 14 2014-07-28 17:30:00.000 记号笔 王尼玛 广告部 */[/quote][/quote]
發糞塗牆 2014-07-29
  • 打赏
  • 举报
回复
你的ReceiverNumber 没按照typeid合并啊
引用 7 楼 lovesheng1212 的回复:
[quote=引用 6 楼 DBA_Huangzj 的回复:] 你直接给出期待结果吧,我都不知道你想要什么
/* id typeid ReceiverNumber userid ReceiverDateTime TypeName EmpName BureauName ----------- ----------- -------------- ----------- ----------------------- -------- ------- ---------- 15 20 25 30 2014-07-16 19:42:29.000 记号笔 张三 技术部 22 19 32 14 2014-07-21 15:10:05.000 白板笔 王尼玛 广告部 25 20 22 14 2014-07-28 17:30:00.000 记号笔 王尼玛 广告部 */[/quote]
發糞塗牆 2014-07-29
  • 打赏
  • 举报
回复
这是没加where条件还是加了的?也就是没有筛选部门还是筛选了?
细嗅蔷薇 2014-07-29
  • 打赏
  • 举报
回复
引用 6 楼 DBA_Huangzj 的回复:
你直接给出期待结果吧,我都不知道你想要什么
/* id typeid ReceiverNumber userid ReceiverDateTime TypeName EmpName BureauName ----------- ----------- -------------- ----------- ----------------------- -------- ------- ---------- 15 20 25 30 2014-07-16 19:42:29.000 记号笔 张三 技术部 22 19 32 14 2014-07-21 15:10:05.000 白板笔 王尼玛 广告部 25 20 22 14 2014-07-28 17:30:00.000 记号笔 王尼玛 广告部 */
發糞塗牆 2014-07-29
  • 打赏
  • 举报
回复
你直接给出期待结果吧,我都不知道你想要什么
细嗅蔷薇 2014-07-29
  • 打赏
  • 举报
回复
引用 4 楼 DBA_Huangzj 的回复:
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(發糞塗牆)
-- Date    :2014-07-29 08:15:22
-- Version:
--      Microsoft SQL Server 2012 - 11.0.5058.0 (X64) 
--	May 14 2014 18:34:29 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[ReceiverRecord]
if object_id('[ReceiverRecord]') is not null drop table [ReceiverRecord]
go 
create table [ReceiverRecord]([ID] int,[TypeID] int,[ReceiverNumber] int,[UserID] int,[ReceiverDateTime] datetime,[TypeName] varchar(6),[EmpName] varchar(6),[BureauName] varchar(6))
insert [ReceiverRecord]
select 15,20,10,30,'2014-07-16 19:42:29','记号笔','张三','技术部' union all
select 22,19,11,14,'2014-07-21 15:10:05','白板笔','王尼玛','广告部' union all
select 23,19,21,14,'2014-07-28 16:17:00','白板笔','王尼玛','广告部' union all
select 24,19,15,30,'2014-07-28 17:00:00','记号笔','张三','技术部' union all
select 25,20,22,14,'2014-07-28 17:30:00','记号笔','王尼玛','广告部'
--------------开始查询--------------------------

SELECT  a.id ,
        a.typeid ,
        b.[ReceiverNumber] ,
        a.userid ,
        a.ReceiverDateTime ,
        a.TypeName ,
        a.EmpName ,
        a.BureauName
FROM    [ReceiverRecord] a
        LEFT JOIN ( SELECT  typeid ,
                            SUM([ReceiverNumber]) [ReceiverNumber]
                    FROM    ReceiverRecord
                    GROUP BY typeid
                  ) b ON a.typeid = b.typeid
WHERE BureauName='你要的部门名'
----------------结果----------------------------
/* 
id          typeid      ReceiverNumber userid      ReceiverDateTime        TypeName EmpName BureauName
----------- ----------- -------------- ----------- ----------------------- -------- ------- ----------
15          20          32             30          2014-07-16 19:42:29.000 记号笔      张三      技术部
22          19          47             14          2014-07-21 15:10:05.000 白板笔      王尼玛     广告部
23          19          47             14          2014-07-28 16:17:00.000 白板笔      王尼玛     广告部
24          19          47             30          2014-07-28 17:00:00.000 记号笔      张三      技术部
25          20          32             14          2014-07-28 17:30:00.000 记号笔      王尼玛     广告部
*/
相同的物品类型跟人 的数据 合并成一行?
發糞塗牆 2014-07-29
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(發糞塗牆)
-- Date    :2014-07-29 08:15:22
-- Version:
--      Microsoft SQL Server 2012 - 11.0.5058.0 (X64) 
--	May 14 2014 18:34:29 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[ReceiverRecord]
if object_id('[ReceiverRecord]') is not null drop table [ReceiverRecord]
go 
create table [ReceiverRecord]([ID] int,[TypeID] int,[ReceiverNumber] int,[UserID] int,[ReceiverDateTime] datetime,[TypeName] varchar(6),[EmpName] varchar(6),[BureauName] varchar(6))
insert [ReceiverRecord]
select 15,20,10,30,'2014-07-16 19:42:29','记号笔','张三','技术部' union all
select 22,19,11,14,'2014-07-21 15:10:05','白板笔','王尼玛','广告部' union all
select 23,19,21,14,'2014-07-28 16:17:00','白板笔','王尼玛','广告部' union all
select 24,19,15,30,'2014-07-28 17:00:00','记号笔','张三','技术部' union all
select 25,20,22,14,'2014-07-28 17:30:00','记号笔','王尼玛','广告部'
--------------开始查询--------------------------

SELECT  a.id ,
        a.typeid ,
        b.[ReceiverNumber] ,
        a.userid ,
        a.ReceiverDateTime ,
        a.TypeName ,
        a.EmpName ,
        a.BureauName
FROM    [ReceiverRecord] a
        LEFT JOIN ( SELECT  typeid ,
                            SUM([ReceiverNumber]) [ReceiverNumber]
                    FROM    ReceiverRecord
                    GROUP BY typeid
                  ) b ON a.typeid = b.typeid
WHERE BureauName='你要的部门名'
----------------结果----------------------------
/* 
id          typeid      ReceiverNumber userid      ReceiverDateTime        TypeName EmpName BureauName
----------- ----------- -------------- ----------- ----------------------- -------- ------- ----------
15          20          32             30          2014-07-16 19:42:29.000 记号笔      张三      技术部
22          19          47             14          2014-07-21 15:10:05.000 白板笔      王尼玛     广告部
23          19          47             14          2014-07-28 16:17:00.000 白板笔      王尼玛     广告部
24          19          47             30          2014-07-28 17:00:00.000 记号笔      张三      技术部
25          20          32             14          2014-07-28 17:30:00.000 记号笔      王尼玛     广告部
*/
细嗅蔷薇 2014-07-28
  • 打赏
  • 举报
回复
我截图显示的字段都要显示出来的
道玄希言 2014-07-28
  • 打赏
  • 举报
回复
select Typeid, sum(ReceiverNumber) as Number from ReceiverRecord where BureauName ='部门' group by Typeid
LongRui888 2014-07-28
  • 打赏
  • 举报
回复
大概就是这样: select typeid,sum(ReceiverNumber) as xx from tb group by typeid

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧