问个比较难的SQL语句。^_^。

LevnWang 2008-05-26 11:48:57
数据表如下:
----------------------------------
月份 姓名 A1 A2 A3 B1 B2 B3
1 张三 -1 4 -1 -1 4 -1
1 李四 2 4 4 2 -5 -6
1 张三 -3 2 4 -3 2 4
1 李四 -5 -1 7 -5 -1 7
1 张三 4 -9 -11 -4 -9 -11
1 王五 -6 -4 -2 -6 4 -2
1 王五 -1 -4 66 1 -4 66
-----------------------------------------------------------
要求查询结果如下:
统计1月份中各个人的A1,A2,A3为负数的次数,得到新的字段为A;
统计1月份中各个人的B1,B2,B3为负数的次数,得到新的字段为B;
如下所示,1月份中,张三:A1,A2,A3为负数的次数为:5
:B1,B2,B3为负数的次数为:6

---------------
月份 姓名 A B
1 张三 5 6
1 李四 2 4
1 王五 5 3
...全文
101 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2008-05-26
  • 打赏
  • 举报
回复
学习sign

楼楼强人
Limpire 2008-05-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 Herb2 的回复:]
select月份,姓名
,a=0-sum(abs(a1)/a1+abs(a2)/a2+abs(a3)/a3-3)/2,
b=0-sum(abs(b1)/a1+abs(b2)/a2+abs(b3)/a3-3)/2
from tb
[/Quote]

这个出现0值是不行的。
Herb2 2008-05-26
  • 打赏
  • 举报
回复
select 月份,姓名
,a=0-sum(abs(a1)/a1+abs(a2)/a2+abs(a3)/a3-3)/2
,b=0-sum(abs(b1)/a1+abs(b2)/a2+abs(b3)/a3-3)/2
from tb
wzy_love_sly 2008-05-26
  • 打赏
  • 举报
回复
declare @tb table (月份 int,姓名 varchar(20),A1 int, A2 int, A3 int,B1 int,B2 int,B3 int)
insert into @tb select 1,'张三',-1,4,-1,-1,4,-1
insert into @tb select 1,'李四',2,4,4,2,-5,-6
insert into @tb select 1,'张三',-3,2,4,-3,2,4

insert into @tb select 1,'李四',-5,-1,7,-5,-1,7
insert into @tb select 1,'张三',4,-9,-11,-4,-9,-11
insert into @tb select 1,'王五',-6,-4,-2,-6,4,-2
insert into @tb select 1,'王五',-1,-4,66,1,-4,66

select 月份,姓名,
sum(case when a1<0 then 1 else 0 end+case when a2<0 then 1 else 0 end +case when a3<0 then 1 else 0 end) as 'a',
sum(case when b1<0 then 1 else 0 end+case when b2<0 then 1 else 0 end +case when b3<0 then 1 else 0 end) as 'b'
from @tb
group by 月份,姓名


月份 姓名 a b
1 李四 2 4
1 王五 5 3
1 张三 5 6
flairsky 2008-05-26
  • 打赏
  • 举报
回复
select 月份, 姓名, A = sum(a1), B = sum(b1)
from( select 月份, 姓名,
case when a1 < 0 then 1 else 0 end +
case when a2 < 0 then 1 else 0 end +
case when a3 < 0 then 1 else 0 end as a1,
case when b1 < 0 then 1 else 0 end +
case when b2 < 0 then 1 else 0 end +
case when b3 < 0 then 1 else 0 end as b1 from table) c
group by 月份, 姓名
Limpire 2008-05-26
  • 打赏
  • 举报
回复
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (月份 int,姓名 varchar(4),A1 int,A2 int,A3 int,B1 int,B2 int,B3 int)
insert into #T
select 1,'张三',-1,4,-1,-1,4,-1 union all
select 1,'李四',2,4,4,2,-5,-6 union all
select 1,'张三',-3,2,4,-3,2,4 union all
select 1,'李四',-5,-1,7,-5,-1,7 union all
select 1,'张三',4,-9,-11,-4,-9,-11 union all
select 1,'王五',-6,-4,-2,-6,4,-2 union all
select 1,'王五',-1,-4,66,1,-4,66

select 月份,姓名,A=sum((1-sign(A1))/2+(1-sign(A2))/2+(1-sign(A3))/2), B=sum((1-sign(B1))/2+(1-sign(B2))/2+(1-sign(B3))/2) from #T group by 月份,姓名
/*
月份 姓名 A B
----------- ---- ----------- -----------
1 李四 2 4
1 王五 5 3
1 张三 5 6
*/
hrb2008 2008-05-26
  • 打赏
  • 举报
回复
select 月份,姓名,
sum(case
when A1 <0 then 1 else 0
end +
case
when A2 <0 then 1 else 0
end +
case
when A3 <0 then 1 else 0
end) as a
,
sum(case
when B1 <0 then 1 else 0
end +
case
when B2 <0 then 1 else 0
end +
case
when B3 <0 then 1 else 0
end) as b
from tb group by 月份,姓名
hrb2008 2008-05-26
  • 打赏
  • 举报
回复
select 月份,姓名,
sum(case
when A1<0 then 1 else 0
end +
case
when A2<0 then 1 else 0
end +
case
when A3<0 then 1 else 0
end) as a
,
sum(case
when B1<0 then 1 else 0
end +
case
when B2<0 then 1 else 0
end +
case
when B3<0 then 1 else 0
end) as b
from tb group by select 月份,姓名
Herb2 2008-05-26
  • 打赏
  • 举报
回复
 
/******************************************************************************/
/*回复:20080526002总:00049 */
/*主题:求字段统计 */
/*作者:二等草 */
/******************************************************************************/

set nocount on

--数据--------------------------------------------------------------------------

declare @tb table([月份] int,[姓名] varchar(4),[A1] int,[A2] int,[A3] int,[B1] int,[B2] int,[B3] int)
insert into @tb select 1,'张三',-1,4,-1,-1,4,-1
insert into @tb select 1,'李四',2,4,4,2,-5,-6
insert into @tb select 1,'张三',-3,2,4,-3,2,4
insert into @tb select 1,'李四',-5,-1,7,-5,-1,7
insert into @tb select 1,'张三',4,-9,-11,-4,-9,-11
insert into @tb select 1,'王五',-6,-4,-2,-6,4,-2
insert into @tb select 1,'王五',-1,-4,66,1,-4,66

--代码--------------------------------------------------------------------------
select [月份],[姓名]
,a=sum(case when a1 >=0 then 0 else 1 end)+sum(case when a2 >=0 then 0 else 1 end)+sum(case when a3 >=0 then 0 else 1 end)
,a=sum(case when b1 >=0 then 0 else 1 end)+sum(case when b2 >=0 then 0 else 1 end)+sum(case when b3 >=0 then 0 else 1 end)
from @tb
group by [月份],[姓名] order by [月份],[姓名] desc
go

/*结果--------------------------------------------------------------------------
月份 姓名 a a
----------- ---- ----------- -----------
1 张三 5 6
1 王五 5 3
1 李四 2 4
--清除------------------------------------------------------------------------*/
-狙击手- 2008-05-26
  • 打赏
  • 举报
回复
/*
-- Author:Flystone
-- Version:V1.001 Date:2008-05-15 初稿
-- Version:V1.002 Date:2008-05-16 1、 处理空格带来的异常
-- 2、 增加了形如yyyy-mm-dd hh:mm:ss
-- yyyy-m-d h:m:s 格式的处理
*/

-- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(月份 int,姓名 varchar(6),A1 int,A2 int,A3 int,B1 int,B2 int,B3 int)
Go
Insert into ta
select 1,'张三',-1,4,-1,-1,4,-1 union all
select 1,'李四',2,4,4,2,-5,-6 union all
select 1,'张三',-3,2,4,-3,2,4 union all
select 1,'李四',-5,-1,7,-5,-1,7 union all
select 1,'张三',4,-9,-11,-4,-9,-11 union all
select 1,'王五',-6,-4,-2,-6,4,-2 union all
select 1,'王五',-1,-4,66,1,-4,66
Go
--Start
select 月份, 姓名, A = sum(a1), B = sum(b1)
from( select 月份, 姓名,
case when a1 < 0 then 1 else 0 end +
case when a2 < 0 then 1 else 0 end +
case when a3 < 0 then 1 else 0 end as a1,
case when b1 < 0 then 1 else 0 end +
case when b2 < 0 then 1 else 0 end +
case when b3 < 0 then 1 else 0 end as b1 from ta) c
group by 月份, 姓名

--Result:
/*
月份 姓名 A B
----------- ------ ----------- -----------
1 李四 2 4
1 王五 5 3
1 张三 5 6

(所影响的行数为 3 行)

*/
--End
LevnWang 2008-05-26
  • 打赏
  • 举报
回复
数据表如下:
----------------------------------
月份 姓名 A1 A2 A3 B1 B2 B3
1 张三 -1 4 -1 -1 4 -1
1 李四 2 4 4 2 -5 -6
1 张三 -3 2 4 -3 2 4
1 李四 -5 -1 7 -5 -1 7
1 张三 4 -9 -11 -4 -9 -11
1 王五 -6 -4 -2 -6 4 -2
1 王五 -1 -4 66 1 -4 66
-----------------------------------------------------------
要求查询结果如下:
统计1月份中各个人的A1,A2,A3为负数的次数,得到新的字段为A;
统计1月份中各个人的B1,B2,B3为负数的次数,得到新的字段为B;
如下所示,1月份中,张三:A1,A2,A3为负数的次数为:5
:B1,B2,B3为负数的次数为:6

---------------
月份 姓名 A B
1 张三 5 6
1 李四 2 4
1 王五 5 3
-狙击手- 2008-05-26
  • 打赏
  • 举报
回复
select 月份, 姓名, A = sum(a1), B = sum(b1)
from( select 月份, 姓名,case when a1 < 0 then 1 else 0 end + case when a2 < 0 then 1 else 0 + case when a3 < 0 then 1 else 0 end end as a1,
case when b1 < 0 then 1 else 0 end + case when b2 < 0 then 1 else 0 + case when b3 < 0 then 1 else 0 end end as b1 from ta) c
group by 月份, 姓名

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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