一组表格数据,怎么得到他的不同次数

weixin_42827947 2018-07-29 06:58:52
我举个例子,好比这个图中的数据,有上百个这样的箱子 ,怎么得到下边那个表格的数据,这个循环该怎么写才能得到我想要的数据,谢谢



...全文
107 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_42827947 2018-07-30
  • 打赏
  • 举报
回复
引用 2 楼 Imaor 的回复:

use tempdb

if OBJECT_ID('#tmp') is not null drop table #tmp
create table #tmp(
tId int identity(1,1) not null,
tBox int null default(0),
tApple int null default(0)
)

insert into #tmp (tBox,tApple)
select 1,8
union all select 2,3
union all select 3,0
union all select 4,8
union all select 5,2
union all select 6,0
union all select 7,1
union all select 8,11
union all select 9,5
union all select 10,4
--- 只读取前10行
;with cte as(
select tApple,COUNT(tId ) as cnt from #tmp group by tApple
)
select top 10 CAST(number as nvarchar(100))+' 个苹果' as N'ColA','有 '+CAST(isnull(cnt,0) as nvarchar(100))+' 个箱子' as N'ColB' from master.dbo.spt_values v
left join cte c on c.tApple =v.number
where v.type='p'
order by number




(10 行受影响)
ColA ColB
----------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------
0 个苹果 有 2 个箱子
1 个苹果 有 1 个箱子
2 个苹果 有 1 个箱子
3 个苹果 有 1 个箱子
4 个苹果 有 1 个箱子
5 个苹果 有 1 个箱子
6 个苹果 有 0 个箱子
7 个苹果 有 0 个箱子
8 个苹果 有 2 个箱子
9 个苹果 有 0 个箱子

(10 行受影响)



我写出来了,谢谢你
weixin_42827947 2018-07-29
  • 打赏
  • 举报
回复
引用 2 楼 Imaor 的回复:

use tempdb

if OBJECT_ID('#tmp') is not null drop table #tmp
create table #tmp(
tId int identity(1,1) not null,
tBox int null default(0),
tApple int null default(0)
)

insert into #tmp (tBox,tApple)
select 1,8
union all select 2,3
union all select 3,0
union all select 4,8
union all select 5,2
union all select 6,0
union all select 7,1
union all select 8,11
union all select 9,5
union all select 10,4
--- 只读取前10行
;with cte as(
select tApple,COUNT(tId ) as cnt from #tmp group by tApple
)
select top 10 CAST(number as nvarchar(100))+' 个苹果' as N'ColA','有 '+CAST(isnull(cnt,0) as nvarchar(100))+' 个箱子' as N'ColB' from master.dbo.spt_values v
left join cte c on c.tApple =v.number
where v.type='p'
order by number




(10 行受影响)
ColA ColB
----------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------
0 个苹果 有 2 个箱子
1 个苹果 有 1 个箱子
2 个苹果 有 1 个箱子
3 个苹果 有 1 个箱子
4 个苹果 有 1 个箱子
5 个苹果 有 1 个箱子
6 个苹果 有 0 个箱子
7 个苹果 有 0 个箱子
8 个苹果 有 2 个箱子
9 个苹果 有 0 个箱子

(10 行受影响)

你好,谢谢,如果不用sql,在程序里面用for循环,怎么写,我水平低,sql看不懂,只能通过slq查询出图片里面的第一张表,然后我想通过第一张表的数据用for循环或者foreach能写出来吗,谢谢
IEEE_China 2018-07-29
  • 打赏
  • 举报
回复

use tempdb

if OBJECT_ID('#tmp') is not null drop table #tmp
create table #tmp(
tId int identity(1,1) not null,
tBox int null default(0),
tApple int null default(0)
)

insert into #tmp (tBox,tApple)
select 1,8
union all select 2,3
union all select 3,0
union all select 4,8
union all select 5,2
union all select 6,0
union all select 7,1
union all select 8,11
union all select 9,5
union all select 10,4
--- 只读取前10行
;with cte as(
select tApple,COUNT(tId ) as cnt from #tmp group by tApple
)
select top 10 CAST(number as nvarchar(100))+' 个苹果' as N'ColA','有 '+CAST(isnull(cnt,0) as nvarchar(100))+' 个箱子' as N'ColB' from master.dbo.spt_values v
left join cte c on c.tApple =v.number
where v.type='p'
order by number




(10 行受影响)
ColA ColB
----------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------
0 个苹果 有 2 个箱子
1 个苹果 有 1 个箱子
2 个苹果 有 1 个箱子
3 个苹果 有 1 个箱子
4 个苹果 有 1 个箱子
5 个苹果 有 1 个箱子
6 个苹果 有 0 个箱子
7 个苹果 有 0 个箱子
8 个苹果 有 2 个箱子
9 个苹果 有 0 个箱子

(10 行受影响)

jhone99 2018-07-29
  • 打赏
  • 举报
回复
先循环找到最大数,再循环里判断累加

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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