请问 完成这个查询,Sql语句应该咋写??查询视图?查询过程?? 谢谢! 我是刚刚用sql。不吝赐教!

「已注销」 2015-01-28 01:06:40
我有一张数据表 结构为
//------------------------------------------------------------

**序号*************时间**************值1******值2******值3******值3
1 2015-1-1 00:00:00 1 1 1 1
2 2015-1-1 00:25:00 1 2 1 1
3 2015-1-1 00:40:00 1 1 3. 5
... ......... ... ... ... ...
... ......... ... ... ... ...
n 2015-1-1 00:59:00 1 1 3. 4
n+1 2015-1-2 00:00:00 1 1 1 1
n+2 2015-1-2 00:25:00 1 2 1 1
n+3 2015-1-2 00:40:00 1 1 3. 5
... ......... ... ... ... ...
... ......... ... ... ... ...
... 2015-1-2 00:59:00 112 1 3. 4
... 2015-1-3 00:00:00 .... ... ... ...
... ......... ... ... ... ...
... 2015-1-3 23:59:00 ... ... ... ...
... ......... ... ... ... ...
... 2015-1-4 00:00:00 .... ... ... ...
... ......... ... ... ... ...
... 2015-1-4 23:59:00 ... ... ... ...
... ......... ... ... ... ...
... ......... ... ... ... ...
... ......... ... ... ... ...
... ......... ... ... ... ...
... 2015-1-31 00:00:00 .... ... ... ...
... ......... ... ... ... ...
... 2015-1-31 23:59:00 ... ... ... ..

//**************************************************************************

如何设计一sql过程计算这个月里每一天的 值1之和、值2之和、值3之和、值3这和
返回数据集格式为:

//*****************************************************************************

**序号*************时间**************值1和******值2和******值3和******值3和
1 2015-1-1 12557 11011 4871 1041
2 2015-1-2 11111 35982 981 11247
3 2015-1-3 875621 9854 3000. 54687
... ......... ... . .. ... ...
... ......... ... ... ... ...
31 2015-1-31 178954 9187 3047. 456978

//***************************************************************************************

请问 完成这个查询,Sql语句应该咋写??查询视图?查询过程?? 谢谢! 我是刚刚用sql。不吝赐教!

...全文
373 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2015-01-31
  • 打赏
  • 举报
回复
引用 15 楼 thisisdell 的回复:

select row_number()over(order by convert(varchar(10),lng_time,120) asc) as 序号
        count(*)  次数
       ,convert(varchar(10),lng_time,120) as lng_time
       ,sum(lng_weight) mweight
       ,sum(amount) amount
      ,''  备注
from lng_lng_record
group by convert(varchar(10),lng_time,120)

咋请教高手一个问题,在sum时候如何制定每一天的计算时间段,比如每一天时间段为早8点到次日早7点59 谢谢赐教
「已注销」 2015-01-31
  • 打赏
  • 举报
回复
引用 17 楼 u011105813 的回复:
[quote=引用 15 楼 thisisdell 的回复:]

select row_number()over(order by convert(varchar(10),lng_time,120) asc) as 序号
        count(*)  次数
       ,convert(varchar(10),lng_time,120) as lng_time
       ,sum(lng_weight) mweight
       ,sum(amount) amount
      ,''  备注
from lng_lng_record
group by convert(varchar(10),lng_time,120)

咋请教高手一个问题,在sum事阿赫制定每一天的计算时间段,比如每一天时间段为早8点到次日早7点59 谢谢赐教[/quote] 咋请教高手一个问题,在sum时候如何制定每一天的计算时间段,比如每一天时间段为早8点到次日早7点59 谢谢赐教
「已注销」 2015-01-31
  • 打赏
  • 举报
回复
引用 15 楼 thisisdell 的回复:

select row_number()over(order by convert(varchar(10),lng_time,120) asc) as 序号
        count(*)  次数
       ,convert(varchar(10),lng_time,120) as lng_time
       ,sum(lng_weight) mweight
       ,sum(amount) amount
      ,''  备注
from lng_lng_record
group by convert(varchar(10),lng_time,120)

咋请教高手一个问题,在sum事阿赫制定每一天的计算时间段,比如每一天时间段为早8点到次日早7点59 谢谢赐教
吉普赛的歌 2015-01-31
  • 打赏
  • 举报
回复
引用 16 楼 u011105813 的回复:
[quote=引用 15 楼 thisisdell 的回复:]

select row_number()over(order by convert(varchar(10),lng_time,120) asc) as 序号
        count(*)  次数
       ,convert(varchar(10),lng_time,120) as lng_time
       ,sum(lng_weight) mweight
       ,sum(amount) amount
      ,''  备注
from lng_lng_record
group by convert(varchar(10),lng_time,120)
再次请教:如何指定“备注”字段为varchar(100)[/quote] convert(varchar(100), '' ) as [备注]
「已注销」 2015-01-30
  • 打赏
  • 举报
回复
引用 15 楼 thisisdell 的回复:

select row_number()over(order by convert(varchar(10),lng_time,120) asc) as 序号
        count(*)  次数
       ,convert(varchar(10),lng_time,120) as lng_time
       ,sum(lng_weight) mweight
       ,sum(amount) amount
      ,''  备注
from lng_lng_record
group by convert(varchar(10),lng_time,120)
再次请教:如何指定“备注”字段为varchar(100)
thisisdell 2015-01-29
  • 打赏
  • 举报
回复

select row_number()over(order by convert(varchar(10),lng_time,120) asc) as 序号
        count(*)  次数
       ,convert(varchar(10),lng_time,120) as lng_time
       ,sum(lng_weight) mweight
       ,sum(amount) amount
      ,''  备注
from lng_lng_record
group by convert(varchar(10),lng_time,120)

「已注销」 2015-01-29
  • 打赏
  • 举报
回复
引用 13 楼 thisisdell 的回复:

select row_number()over(order by convert(varchar(10),lng_time,120) asc) as 序号
        count(*)  次数
       ,convert(varchar(10),lng_time,120) as lng_time
       ,sum(lng_weight) mweight
       ,sum(amount) amount 
from lng_lng_record
group by convert(varchar(10),lng_time,120)

谢谢,我再请教一个问题。如果在结果集动态加一个字段“备注”,应该咋写? ps:“备注”字段在表里没有
thisisdell 2015-01-29
  • 打赏
  • 举报
回复

select row_number()over(order by convert(varchar(10),lng_time,120) asc) as 序号
        count(*)  次数
       ,convert(varchar(10),lng_time,120) as lng_time
       ,sum(lng_weight) mweight
       ,sum(amount) amount 
from lng_lng_record
group by convert(varchar(10),lng_time,120)

「已注销」 2015-01-29
  • 打赏
  • 举报
回复
**序号*************时间******当日加入次数********值1和******值2和******值3和******值3和 1 2015-1-1 ? 12557 11011 4871 1041 2 2015-1-2 11111 35982 981 11247 3 2015-1-3 875621 9854 3000. 54687 ... ......... ... . .. ... ... ... ......... ... ... ... ... 31 2015-1-31 178954 9187 3047. 456978 //*************************************************************************************** 请问 如何把每一天的“序号”也统计一下变为“每日加入次数” 谢谢赐教
「已注销」 2015-01-29
  • 打赏
  • 举报
回复
引用 10 楼 thisisdell 的回复:
哦,我没看前面的,只看到最后一楼,这样改:

select row_number()over(order by convert(varchar(10),lng_time,120) asc) as 序号
        ,convert(varchar(10),lng_time,120) as lng_time,sum(lng_weight) mweight
        ,sum(amount) amount 
from lng_lng_record
group by convert(varchar(10),lng_time,120)
我还有一个问题 **序号*************时间**************值1******值2******值3******值3 1 2015-1-1 00:00:00 1 1 1 1 2 2015-1-1 00:25:00 1 2 1 1 3 2015-1-1 00:40:00 1 1 3. 5 ... ......... ... ... ... ... ... ......... ... ... ... ... n 2015-1-1 00:59:00 1 1 3. 4 n+1 2015-1-2 00:00:00 1 1 1 1 n+2 2015-1-2 00:25:00 1 2 1 1 n+3 2015-1-2 00:40:00 1 1 3. 5 ... ......... ... ... ... ... ... ......... ... ... ... ... ... 2015-1-2 00:59:00 112 1 3. 4 ... 2015-1-3 00:00:00 .... ... ... ... ... ......... ... ... ... ... ... 2015-1-3 23:59:00 ... ... ... ... ... ......... ... ... ... ... ... 2015-1-4 00:00:00 .... ... ... ... ... ......... ... ... ... ... ... 2015-1-4 23:59:00 ... ... ... ... ... ......... ... ... ... ... ... ......... ... ... ... ... ... ......... ... ... ... ... ... ......... ... ... ... ... ... 2015-1-31 00:00:00 .... ... ... ... ... ......... ... ... ... ... ... 2015-1-31 23:59:00 ... ... ... .. //************************************************************************** 如何设计一sql过程计算这个月里每一天的 值1之和、值2之和、值3之和、值3这和 返回数据集格式为: //***************************************************************************** **序号*************时间**************值1和******值2和******值3和******值3和 1 2015-1-1 12557 11011 4871 1041 2 2015-1-2 11111 35982 981 11247 3 2015-1-3 875621 9854 3000. 54687 ... ......... ... . .. ... ... ... ......... ... ... ... ... 31 2015-1-31 178954 9187 3047. 456978 //*************************************************************************************** 请问 如何把每一天的“序号”也统计一下变为“每日假如次数” 谢谢赐教
thisisdell 2015-01-28
  • 打赏
  • 举报
回复
哦,我没看前面的,只看到最后一楼,这样改:

select row_number()over(order by convert(varchar(10),lng_time,120) asc) as 序号
        ,convert(varchar(10),lng_time,120) as lng_time,sum(lng_weight) mweight
        ,sum(amount) amount 
from lng_lng_record
group by convert(varchar(10),lng_time,120)
「已注销」 2015-01-28
  • 打赏
  • 举报
回复
引用 8 楼 thisisdell 的回复:

select row_number()over(order by lng_time asc) as 序号
        ,convert(varchar(10),lng_time,120) as lng_time,sum(lng_weight) mweight
        ,sum(amount) amount 
from lng_lng_record
group by  lng_time

可以执行了 但是 lng_time 有重复啊? 应该一天一条啊? 谢谢
thisisdell 2015-01-28
  • 打赏
  • 举报
回复

select row_number()over(order by lng_time asc) as 序号
        ,convert(varchar(10),lng_time,120) as lng_time,sum(lng_weight) mweight
        ,sum(amount) amount 
from lng_lng_record
group by lng_time

「已注销」 2015-01-28
  • 打赏
  • 举报
回复
引用 6 楼 thisisdell 的回复:
[quote=引用 5 楼 u011105813 的回复:] [quote=引用 1 楼 hepe00 的回复:]
select row_number()over(order by 时间 asc) as 序号
		,convert(varchar(10),时间,120) as 时间,sum(值1) 值1和
		,sum(值2) 值2和,sum(值3) 值3和,sum(值3和1) 值3和1
from Tb
group by convert(varchar(10),时间,120)
我带入我的表:
select row_number()over(order by lng_time asc) as 序号
		,convert(varchar(10),lng_time,120) as lng_time,sum(lng_weight) mweight
		,sum(amount) amount 
from lng_lng_record
group by convert(varchar(10),lng_time,120)
执行后。出现信息: //----------------------------------------------------------------------- 消息 8120,级别 16,状态 1,第 2 行 选择列表中的列 'lng_lng_record.lng_time' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。 消息 8120,级别 16,状态 1,第 2 行 选择列表中的列 'lng_lng_record.lng_time' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。 //--------------------------------------------------------------------------- 我错在哪??谢谢[/quote] group by convert(varchar(10),lng_time,120) 改为:group by lng_time [/quote] 那我应该咋写?? 谢谢
thisisdell 2015-01-28
  • 打赏
  • 举报
回复
引用 5 楼 u011105813 的回复:
[quote=引用 1 楼 hepe00 的回复:]
select row_number()over(order by 时间 asc) as 序号
		,convert(varchar(10),时间,120) as 时间,sum(值1) 值1和
		,sum(值2) 值2和,sum(值3) 值3和,sum(值3和1) 值3和1
from Tb
group by convert(varchar(10),时间,120)
我带入我的表:
select row_number()over(order by lng_time asc) as 序号
		,convert(varchar(10),lng_time,120) as lng_time,sum(lng_weight) mweight
		,sum(amount) amount 
from lng_lng_record
group by convert(varchar(10),lng_time,120)
执行后。出现信息: //----------------------------------------------------------------------- 消息 8120,级别 16,状态 1,第 2 行 选择列表中的列 'lng_lng_record.lng_time' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。 消息 8120,级别 16,状态 1,第 2 行 选择列表中的列 'lng_lng_record.lng_time' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。 //--------------------------------------------------------------------------- 我错在哪??谢谢[/quote] group by convert(varchar(10),lng_time,120) 改为:group by lng_time
「已注销」 2015-01-28
  • 打赏
  • 举报
回复
引用 1 楼 hepe00 的回复:
select row_number()over(order by 时间 asc) as 序号
		,convert(varchar(10),时间,120) as 时间,sum(值1) 值1和
		,sum(值2) 值2和,sum(值3) 值3和,sum(值3和1) 值3和1
from Tb
group by convert(varchar(10),时间,120)
我带入我的表:
select row_number()over(order by lng_time asc) as 序号
		,convert(varchar(10),lng_time,120) as lng_time,sum(lng_weight) mweight
		,sum(amount) amount 
from lng_lng_record
group by convert(varchar(10),lng_time,120)
执行后。出现信息: //----------------------------------------------------------------------- 消息 8120,级别 16,状态 1,第 2 行 选择列表中的列 'lng_lng_record.lng_time' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。 消息 8120,级别 16,状态 1,第 2 行 选择列表中的列 'lng_lng_record.lng_time' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。 //--------------------------------------------------------------------------- 我错在哪??谢谢
hepe00 2015-01-28
  • 打赏
  • 举报
回复
select row_number()over(order by 时间 asc) as 序号
		,convert(varchar(10),时间,120) as 时间,sum(值1) 值1和
		,sum(值2) 值2和,sum(值3) 值3和,sum(值3和1) 值3和1
from Tb
group by convert(varchar(10),时间,120)
「已注销」 2015-01-28
  • 打赏
  • 举报
回复
如何写成一个完整的查询视图呢??? 谢谢
wtujedp 2015-01-28
  • 打赏
  • 举报
回复
引用 2 楼 u011105813 的回复:
那如果时间动态指定时间段,比如开始时间2014-12-1.结束时间2015-01-31,sql语句咋写???
from 表后面直接加个 where 日期 between 2014-12-1 and 2015-01-31????
「已注销」 2015-01-28
  • 打赏
  • 举报
回复
那如果时间动态指定时间段,比如开始时间2014-12-1.结束时间2015-01-31,sql语句咋写???

27,580

社区成员

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

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