sqlserver的groupby问题

麻烦的一笔 2018-12-17 11:51:13
我的sql语句只要groupby一个字段,但是要查询所有表里数据这个怎么写?
select c_swunit_data_id,sum(c_value) as number,c_swstation_id,c_value,c_get_time from t_swunit_data_his 
group by c_swunit_data_id
...全文
341 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
二月十六 2018-12-17
  • 打赏
  • 举报
回复
可以试试这样
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([c_swunit_data_id] int,[c_get_time] Date,[c_value] int)
Insert #T
select 1,'2018-12-10',10 union all
select 1,'2018-12-12',10 union all
select 2,'2018-12-13',10 union all
select 3,'2018-12-15',10
Go
--测试数据结束
Select *,SUM(c_value)OVER(PARTITION BY c_swunit_data_id ORDER BY GETDATE()) AS [number] from #T


吉普赛的歌 2018-12-17
  • 打赏
  • 举报
回复
不能这样做, 逻辑上是错误的。 要么按多个字段分组, 要么不分组的字段加上 max, min, count, sum 等聚合函数。 因为你只按一个字段分组, 其它不分组的字段可能在一个结果记录中对应多个值, 你必须给出对应的解决方案。
SELECT c_swunit_data_id,
       SUM(c_value) AS number,
       c_swstation_id,
       c_value,
       c_get_time
FROM   t_swunit_data_his
GROUP BY
       c_swunit_data_id,
       c_swstation_id,
       c_value,
       c_get_time
卖水果的net 2018-12-17
  • 打赏
  • 举报
回复
给一下测试数据,和你的预期结果。 用 excel 画一下。
麻烦的一笔 2018-12-17
  • 打赏
  • 举报
回复
谢谢各位,最后还是采纳了10楼 的解决方案,感谢二月十六版主的真诚帮助!
二月十六 2018-12-17
  • 打赏
  • 举报
回复
--测试数据
if not object_id(N'Tempdb..#T') is null
	drop table #T
Go
Create table #T([c_swunit_data_id] int,[c_get_time] Date,[c_value] int)
Insert #T
select 1,'2018-12-10',10 union all
select 1,'2018-12-12',10 union all
select 2,'2018-12-13',10 union all
select 3,'2018-12-15',10
Go
--测试数据结束
SELECT t.c_swunit_data_id,t1.c_get_time,t1.c_value FROM (
SELECT c_swunit_data_id,SUM(c_value)c_value FROM #T GROUP BY c_swunit_data_id)t
CROSS APPLY(SELECT TOP 1 * FROM #T WHERE t.c_swunit_data_id = c_swunit_data_id)t1
换成楼主自己的表名和实际字段,这个是个例子:
麻烦的一笔 2018-12-17
  • 打赏
  • 举报
回复
引用 8 楼 麻烦的一笔 的回复:
[quote=引用 6 楼 二月十六 的回复:]
[quote=引用 5 楼 麻烦的一笔 的回复:]
[quote=引用 3 楼 二月十六 的回复:]
可以试试这样
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([c_swunit_data_id] int,[c_get_time] Date,[c_value] int)
Insert #T
select 1,'2018-12-10',10 union all
select 1,'2018-12-12',10 union all
select 2,'2018-12-13',10 union all
select 3,'2018-12-15',10
Go
--测试数据结束
Select *,SUM(c_value)OVER(PARTITION BY c_swunit_data_id ORDER BY GETDATE()) AS [number] from #T



你看下我上面数据[/quote]
想要的对应的结果什么样?[/quote]
[/quote]
就比如你上面的数据1有两条记录,但是只能出现一次,c_value用sum累加,其他几列不在groupby里面但也要现实出来
Dear SQL(燊) 2018-12-17
  • 打赏
  • 举报
回复
引用 7 楼 麻烦的一笔 的回复:
[quote=引用 6 楼 二月十六 的回复:] [quote=引用 5 楼 麻烦的一笔 的回复:] [quote=引用 3 楼 二月十六 的回复:] 可以试试这样
--测试数据
if not object_id(N'Tempdb..#T') is null
	drop table #T
Go
Create table #T([c_swunit_data_id] int,[c_get_time] Date,[c_value] int)
Insert #T
select 1,'2018-12-10',10 union all
select 1,'2018-12-12',10 union all
select 2,'2018-12-13',10 union all
select 3,'2018-12-15',10
Go
--测试数据结束
Select *,SUM(c_value)OVER(PARTITION BY c_swunit_data_id ORDER BY GETDATE()) AS [number] from #T
你看下我上面数据[/quote] 想要的对应的结果什么样?[/quote] 我用你上面的sql提示order附近有错误, 我的需求是根据c_swunit_data_id分组,sum出c_swunit_data_id下的c_value值,因为c_swunit_data_id有重复的[/quote] 你的sql版本过低
if not object_id(N'Tempdb..#T') is null
    drop table #T
Go
Create table #T([c_swunit_data_id] int,[c_get_time] Date,[c_value] int)
Insert #T
select 1,'2018-12-10',10 union all
select 1,'2018-12-12',10 union all
select 2,'2018-12-13',10 union all
select 3,'2018-12-15',10
Go
--测试数据结束

;
with list as(
	select [c_swunit_data_id],sum([c_value]) as sum_c_value
	from #T
	group by [c_swunit_data_id]
)
select a.*,b.sum_c_value
from #T a
inner join list b on a.[c_swunit_data_id]=b.[c_swunit_data_id]
二月十六 2018-12-17
  • 打赏
  • 举报
回复
数据库的版本是什么? 还有楼主你具体想要什么样的结果,楼主你现在说的统计,就是一个sum统计
SELECT c_swunit_data_id,
       SUM(c_value) AS c_value
FROM 表
GROUP BY c_swunit_data_id;
你可以写出来目前的数据是 1 2 3 我想要 2 3 4 或者我想要的结果是 6 这样写出来就知道应该怎么写了
麻烦的一笔 2018-12-17
  • 打赏
  • 举报
回复
引用 6 楼 二月十六 的回复:
[quote=引用 5 楼 麻烦的一笔 的回复:]
[quote=引用 3 楼 二月十六 的回复:]
可以试试这样
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([c_swunit_data_id] int,[c_get_time] Date,[c_value] int)
Insert #T
select 1,'2018-12-10',10 union all
select 1,'2018-12-12',10 union all
select 2,'2018-12-13',10 union all
select 3,'2018-12-15',10
Go
--测试数据结束
Select *,SUM(c_value)OVER(PARTITION BY c_swunit_data_id ORDER BY GETDATE()) AS [number] from #T



你看下我上面数据[/quote]
想要的对应的结果什么样?[/quote]
麻烦的一笔 2018-12-17
  • 打赏
  • 举报
回复
引用 6 楼 二月十六 的回复:
[quote=引用 5 楼 麻烦的一笔 的回复:]
[quote=引用 3 楼 二月十六 的回复:]
可以试试这样
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([c_swunit_data_id] int,[c_get_time] Date,[c_value] int)
Insert #T
select 1,'2018-12-10',10 union all
select 1,'2018-12-12',10 union all
select 2,'2018-12-13',10 union all
select 3,'2018-12-15',10
Go
--测试数据结束
Select *,SUM(c_value)OVER(PARTITION BY c_swunit_data_id ORDER BY GETDATE()) AS [number] from #T



你看下我上面数据[/quote]
想要的对应的结果什么样?[/quote]
我用你上面的sql提示order附近有错误,
我的需求是根据c_swunit_data_id分组,sum出c_swunit_data_id下的c_value值,因为c_swunit_data_id有重复的
二月十六 2018-12-17
  • 打赏
  • 举报
回复
引用 5 楼 麻烦的一笔 的回复:
[quote=引用 3 楼 二月十六 的回复:] 可以试试这样
--测试数据
if not object_id(N'Tempdb..#T') is null
	drop table #T
Go
Create table #T([c_swunit_data_id] int,[c_get_time] Date,[c_value] int)
Insert #T
select 1,'2018-12-10',10 union all
select 1,'2018-12-12',10 union all
select 2,'2018-12-13',10 union all
select 3,'2018-12-15',10
Go
--测试数据结束
Select *,SUM(c_value)OVER(PARTITION BY c_swunit_data_id ORDER BY GETDATE()) AS [number] from #T
你看下我上面数据[/quote] 想要的对应的结果什么样?
麻烦的一笔 2018-12-17
  • 打赏
  • 举报
回复
引用 3 楼 二月十六 的回复:
可以试试这样
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([c_swunit_data_id] int,[c_get_time] Date,[c_value] int)
Insert #T
select 1,'2018-12-10',10 union all
select 1,'2018-12-12',10 union all
select 2,'2018-12-13',10 union all
select 3,'2018-12-15',10
Go
--测试数据结束
Select *,SUM(c_value)OVER(PARTITION BY c_swunit_data_id ORDER BY GETDATE()) AS [number] from #T



你看下我上面数据
麻烦的一笔 2018-12-17
  • 打赏
  • 举报
回复
c_swstation_id c_swunit_data_id c_value c_get_time
1 301 0 20:59.0
1 304 0 20:59.0
1 307 0 20:59.0
1 310 0 20:59.0
1 313 0 20:59.0
1 316 0 20:59.0
1 319 0 20:59.0
1 322 0 20:59.0
1 325 0 20:59.0
1 328 0 20:59.0
1 331 0 20:59.0
1 334 0 20:59.0
1 337 0 20:59.0
1 340 0 20:59.0
1 343 0 20:59.0
1 289 0 20:59.0
1 346 0 20:59.0
1 349 0 20:59.0
1 352 0 20:59.0
1 355 0 20:59.0
1 358 0 20:59.0
1 144 1 23:45.0
1 360 1 23:45.0
1 363 1 23:45.0
1 366 1 23:45.0
1 369 1 23:45.0
1 372 1 23:45.0
1 375 1 23:45.0
1 378 1 23:45.0
1 381 1 23:45.0
1 384 1 23:45.0
1 387 1 23:45.0
1 390 1 23:45.0
1 393 1 23:45.0
1 396 1 23:45.0
1 402 1 23:45.0
1 408 1 23:45.0
1 411 1 23:45.0
1 414 1 23:45.0
1 417 1 23:45.0
1 420 1 23:45.0
1 423 1 23:45.0
1 426 1 23:45.0
1 429 1 23:45.0
1 399 1 23:45.0
1 405 1 23:45.0
1 160 0 23:45.0
1 159 1 23:48.0
1 1 1 23:54.0
1 291 1 23:54.0
1 294 1 23:54.0
1 297 1 23:54.0
1 300 1 23:54.0
1 303 1 23:54.0
1 306 1 23:54.0
1 309 1 23:54.0
1 312 1 23:54.0
1 315 1 23:54.0
1 318 1 23:54.0
1 321 1 23:54.0
1 324 1 23:54.0
1 327 1 23:54.0
1 330 1 23:54.0
1 333 1 23:54.0
1 336 1 23:54.0
1 342 1 23:54.0
1 339 1 23:54.0
1 288 1 23:54.0
1 345 1 23:54.0
1 348 1 23:54.0
1 351 1 23:54.0
1 354 1 23:54.0
1 357 1 23:54.0
1 17 0 23:54.0
1 16 1 23:57.0
1 144 0 24:01.0
1 414 0 24:02.0
1 360 0 24:02.0
1 363 0 24:02.0
1 366 0 24:02.0
1 369 0 24:02.0
1 372 0 24:02.0
1 375 0 24:02.0
1 378 0 24:02.0
1 381 0 24:02.0
1 384 0 24:02.0
大致的数据时这样的,根据c_swunit_data_id分组排列,c_value=0或1的计算综合

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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