首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 分组统计
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 16:52:52 楼主
    大家好,
    第一次登陆并使用此平台寻求帮助,希望可以帮我解决以下问题

    案例如下:
    表in
    id name type total
    1  a  aa    0
    2  a1  aa    2
    3  a2  aa    3
    4  d  bb    0
    5  d1  bb    6
    6  d2  bb    2
    表out
    id name type total
    1  a  aa    5
    2  a1  aa    0
    3  a2  aa    0
    4  d  bb    8
    5  d1  bb    0
    6  d2  bb    0
    应该是分组计算吧,希望大家可以帮我提些建议!

    20  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • dawugui
    • 等级:
    发表于:2008-05-11 17:16:371楼 得分:0
    SQL code
    select t.id , t.name , t.type , total = case when len(rtrim(t.a)=1) then (select sum(total) from [in] where name like '%' + t.name + '%') else 0 end from [in] t
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • liangCK
    • 等级:
    发表于:2008-05-11 17:16:452楼 得分:0
    结果怎么得出来的..至少也得说说吧.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:17:363楼 得分:0
    结果,没做出来,所以请教高手:)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:20:414楼 得分:0
    你写的好复杂,可不可以不和id关联,应用不同的type,按组计算呢?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:20:445楼 得分:0
    select a.id,a.name,type ,case when isnull(name3,'')='' then 0 else sum1 end total  from in a left join

    (select type ,sum(total) sum1 from mm group by type) b
    on  a.type=b.type
    left join (select min(id) name3 from mm group by type) c
    on a.name1=c.name3


    这样的查询结果就符合你的条件了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • dawugui
    • 等级:
    发表于:2008-05-11 17:20:486楼 得分:0
    SQL code
    create table [in](id int,name varchar(10) , type varchar(10) , total int) insert into [in] values(1 , 'a' , 'aa' , 0) insert into [in] values(2 , 'a1', 'aa' , 2) insert into [in] values(3 , 'a2', 'aa' , 3) insert into [in] values(4 , 'd' , 'bb' , 0) insert into [in] values(5 , 'd1', 'bb' , 6) insert into [in] values(6 , 'd2', 'bb' , 2) go select t.id , t.name , t.type , total = (case when len(rtrim(t.name))=1 then (select sum(total) from [in] where name like '%' + t.name + '%') else 0 end) from [in] t /* id name type total ----------- ---------- ---------- ----------- 1 a aa 5 2 a1 aa 0 3 a2 aa 0 4 d bb 8 5 d1 bb 0 6 d2 bb 0 (所影响的行数为 6 行) */ select t.id , t.name , t.type , total = (case when len(rtrim(t.name))=1 then (select sum(total) from [in] where type = t.type) else 0 end) from [in] t /* id name type total ----------- ---------- ---------- ----------- 1 a aa 5 2 a1 aa 0 3 a2 aa 0 4 d bb 8 5 d1 bb 0 6 d2 bb 0 (所影响的行数为 6 行) */ drop table [in]
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:22:207楼 得分:0
    这样是完全可以实现的
    已经试过了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:26:238楼 得分:0
    SQL code
    select tab.id,tab.name,tab.[type],isnull(tab2.total,0) as total from tab left outer join ( select a.id,a.[type],b.total from ( select [type],min(id) as id from tab group by [type] )as a inner join ( select [type],sum(total) as total from tab group by [type] ) as b on a.[type] = b.[type] ) as tab2 on tab.id = tab2.id and tab.[type] = tab2.[type]
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:30:289楼 得分:0
    假设表out已经存在,为tout

    SQL code
    insert into tout select * from tin where total > 0 union all select a.id, a.name, b.type, b.total from tin a inner join (select TYPE, SUM(total) as total from tin group by type) b on a.type = b.type where a.total = 0 order by id


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:31:1910楼 得分:0
    对不起大家,我想要的结果是
    id name type total
    1  a  aa    5
    2  a1  aa    2
    3  a2  aa    3
    4  d  bb    8
    5  d1  bb    6
    6  d2  bb    2
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:34:5611楼 得分:0
    select a.id,a.name,type ,case when isnull(name3,'')='' then total else sum1 end total  from in a left join 

    (select type ,sum(total) sum1 from mm group by type) b
    on  a.type=b.type
    left join (select min(id) name3 from mm group by type) c
    on a.name1=c.name3


    这样就可以呀
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:37:2112楼 得分:0


    select a.id,a.name,type ,case when isnull(name3,'')='' then total else sum1 end total  from in a left join 

    (select type ,sum(total) sum1 from mm group by type) b 
    on  a.type=b.type 
    left join (select min(id) name3 from mm group by type) c 
    on a.id=c.name3 

    这样就可以呀


    刚才一个字段错了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:39:4213楼 得分:0
    SQL code
    select * from tin where total > 0 union all select a.id, a.name, b.type, b.total from tin a inner join (select TYPE, SUM(total) as total from tin group by type) b on a.type = b.type where a.total = 0 order by id /* id name type total ----------- ---------- ---------- ----------- 1 a aa 5 2 a1 aa 2 3 a2 aa 3 4 d bb 8 5 d1 bb 6 6 d2 bb 2 (6 row(s) affected)*/
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • dawugui
    • 等级:
    发表于:2008-05-11 17:45:0914楼 得分:0
    引用 10 楼 snow1103 的回复:
    对不起大家,我想要的结果是
    id name type total 
    1  a  aa    5 
    2  a1  aa    2 
    3  a2  aa    3 
    4  d  bb    8 
    5  d1  bb    6 
    6  d2  bb    2 


    SQL code
    create table [in](id int,name varchar(10) , type varchar(10) , total int) insert into [in] values(1 , 'a' , 'aa' , 0) insert into [in] values(2 , 'a1', 'aa' , 2) insert into [in] values(3 , 'a2', 'aa' , 3) insert into [in] values(4 , 'd' , 'bb' , 0) insert into [in] values(5 , 'd1', 'bb' , 6) insert into [in] values(6 , 'd2', 'bb' , 2) go select t.id , t.name , t.type , total = (case when len(rtrim(t.name))=1 then (select sum(total) from [in] where name like '%' + t.name + '%') else total end) from [in] t /* id name type total ----------- ---------- ---------- ----------- 1 a aa 5 2 a1 aa 2 3 a2 aa 3 4 d bb 8 5 d1 bb 6 6 d2 bb 2 (所影响的行数为 6 行) */ select t.id , t.name , t.type , total = (case when len(rtrim(t.name))=1 then (select sum(total) from [in] where type = t.type) else total end) from [in] t /* id name type total ----------- ---------- ---------- ----------- 1 a aa 5 2 a1 aa 2 3 a2 aa 3 4 d bb 8 5 d1 bb 6 6 d2 bb 2 (所影响的行数为 6 行) */ drop table [in]
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:50:3215楼 得分:0
    非常感谢大家,我试了一种做法,是jobine 的,但有错误"使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。"
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 18:01:1416楼 得分:0
    try

    SQL code
    select id, name, type,total from tin where total > 0 union all select a.id, a.name, b.type, b.total from tin a inner join (select TYPE, SUM(total) as total from tin group by type) b on a.type = b.type where a.total = 0 order by id


    你的in表可能含有其他字段。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 18:05:2017楼 得分:0
    晕.....................
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 18:08:3118楼 得分:0
    select a.id,a.name,type ,case when isnull(name3,'')='' then total else sum1 end total  from in a left join   

    (select type ,sum(total) sum1 from in group by type) b 
    on  a.type=b.type 
    left join (select min(id) id  from in group by type) c 
    on a.id=c.id 


    我自己建的表内容,刚才字段错了,
    晕................
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 18:09:5119楼 得分:0
    select a.id,a.name,type ,case when isnull(c.id,'')='' then total else sum1 end total  from in a left join   

    (select type ,sum(total) sum1 from in group by type) b   
    on  a.type=b.type   
    left join (select min(id) id  from in group by type) c   
    on a.id=c.id 

    我自己建的表内容,刚才字段错了,
    晕................
    好了
    不写了,有点累
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 18:14:3620楼 得分:0
    是呀,我说name3是什么意思呢
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 18:44:4021楼 得分:0
    to jobine


    对的,我还有很多字段需要显示,所以我不得不用group by id,name,type,order by
    但我算出的结果是
    1  a  aa    5
    2  a1  aa    5
    3  a2  aa    5
    4  d  bb    8
    5  d1  bb    8
    6  d2  bb    8
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 18:50:2222楼 得分:0
    不能group by id, name,因为你是根据type求和的。
    试试
    SQL code
    select id, name, type,total, 其他字段 from tin where total > 0 union all select a.id, a.name, b.type, b.total, 其他字段 from tin a inner join (select TYPE, SUM(total) as total from tin group by type) b on a.type = b.type where a.total = 0 order by id
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 19:03:1523楼 得分:0
    to dawugui
    我算出来的total都是0
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 19:13:0624楼 得分:0
    to jobine
    结果还是一样
    我大概说一下表的情况:
    我想统计