这个SQL语句应该怎么写?

viphk 2011-03-26 09:45:28
我表中有A,B,C三个字段,A字段中存放的都是INT类的值,C字段是插入数据时的时间,我想按B字段的要求取出A字段中今日记录的和值

测试数据
A B C
20 A1 记录时间
26 A2 ........
32 A1 ........
17 A3 ........
98 A1 ........
56 A3 ........
30 A1 ........
11 A3 ........
91 A1 ........
76 A3 ........

求A字段今日记录的和,条件是所有记录中,B=A1的和,B=A2的和,B=A3的和
我主要是想要不同的查询条件返回一个不同的暂时字段
比如B=A1的和
返回一个暂时字段 A1He
B=A2的和
返回一个暂时字段 A2He
B=A3的和
返回一个暂时字段 A3He
这个我就可以在程序中直接拿他的结果了,或者大家有更好的方法也可以告诉我,谢谢了。
...全文
78 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Corporal 2011-03-26
  • 打赏
  • 举报
回复
select sum(A) from ...
viphk 2011-03-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 corporal 的回复:]
select
(select A from 表 where b='a1' and datediff(dd,c,getdate())=0) as a1he,
(select A from 表 where b='a2' and datediff(dd,c,getdate())=0) as a2he,
(select A from 表 where b='a3' and datediff(dd,……
[/Quote]
我看大意了,你这里没有统计符合记录条件中A字段的和值,
如 B=A1
A的值和是271,不过我知道怎么写了,谢谢你了
viphk 2011-03-26
  • 打赏
  • 举报
回复

好的,谢谢你了,这正是我想要的结果
[Quote=引用 7 楼 corporal 的回复:]
select
(select A from 表 where b='a1' and datediff(dd,c,getdate())=0) as a1he,
(select A from 表 where b='a2' and datediff(dd,c,getdate())=0) as a2he,
(select A from 表 where b='a3' and datediff(dd,……
[/Quote]
viphk 2011-03-26
  • 打赏
  • 举报
回复
比如今日所有B=A1,A字段中的和
返回一个暂时字段 A1He
今日所有B=A2,A字段中的和
返回一个暂时字段 A2He
今日所有B=A3,A字段中的和
返回一个暂时字段 A3He
Corporal 2011-03-26
  • 打赏
  • 举报
回复
select
(select A from 表 where b='a1' and datediff(dd,c,getdate())=0) as a1he,
(select A from 表 where b='a2' and datediff(dd,c,getdate())=0) as a2he,
(select A from 表 where b='a3' and datediff(dd,c,getdate())=0) as a3he
from 表

[from 表]
可以不要的。
pengwen420107 2011-03-26
  • 打赏
  • 举报
回复
A1,A2,A3的和怎么理解?
viphk 2011-03-26
  • 打赏
  • 举报
回复
select
(select A from 表 where b='a1' and datediff(dd,c,getdate())=0) as a1he,
(select A from 表 where b='a2' and datediff(dd,c,getdate())=0) as a2he,
(select A from 表 where b='a3' and datediff(dd,c,getdate())=0) as a3he
from 表
完整的是这样吗?
viphk 2011-03-26
  • 打赏
  • 举报
回复
应该就是这个了
[Quote=引用 3 楼 corporal 的回复:]
select
(select A from 表 where b='a1' and datediff(dd,c,getdate())=0) as a1he,
(select A from 表 where b='a2' and datediff(dd,c,getdate())=0) as a2he,
(select A from 表 where b='a3' and datediff(dd,c……
[/Quote]
Corporal 2011-03-26
  • 打赏
  • 举报
回复
select
(select A from 表 where b='a1' and datediff(dd,c,getdate())=0) as a1he,
(select A from 表 where b='a2' and datediff(dd,c,getdate())=0) as a2he,
(select A from 表 where b='a3' and datediff(dd,c,getdate())=0) as a3he,
IT0329 2011-03-26
  • 打赏
  • 举报
回复
试试子查询语句
Shalves 2011-03-26
  • 打赏
  • 举报
回复
A1的和?A2的和?怎么理解?

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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