求一复杂sql语句 - MS-SQL Server / 基础类
id stucode stuaddress sturoom stutime
1 22 35# n502 201005
2 22 35# n502 201007
3 12 45# n501 201005
4 22 35# n502 201005
5 22 35# n502 201005
6 12 45# n501 201009
7 12 65# n401 201010
8 22 35# n502 201005
9 22 34# n502 201005
10 32 35# n512 201005
11 22 34# n502 201007
12 32 55# n512 201009
13 33 55# n512 201009
求一sql语句得到结果如下:
code address room time
22 35# n502 201007
12 45# n501 201009
32 55# n512 201009
33 55# n512 201009
code为基础,然后对比address+room 把相同条目多的留下,最后只留下一条time最大的.
原则一:22的 35#+n502 有4条, 22的34#+n502 有2条 所以留下22的 35#+n502,然后再剩下时间最大的一条
22 35# n502 201007
原则二:32的55#+n512 有1条, 32的35#+n512有1条 留时间最大的一条,如果时间相同留id大的,id唯一
32 55# n512 201009
ms sql 在my sql中报错 mysql怎么写? 以下为语句
with cte as(
select *
,count(stuaddress+sturoom) over(partition by stucode,stuaddress+sturoom) as cnt
,max(stutime) over(partition by stucode,stuaddress+sturoom) as mx_time
from #test)
select stucode,stuaddress,sturoom,stutime from cte a where stutime=mx_time
and not exists(select 1 from cte
where stutime=mx_time
and(( a.stucode=stucode and a.cnt=cnt and a.stutime<stutime)
or ( a.stucode=stucode and
相关问答:
今天做了一个存储过程 环境是SQL2000数据库
大致如下
建立临时表
定义员工游标
循环员工(属于1个公司)
......
如何在SQL2005中设定定时作业,比如说定时清理某些表的数据,
或者是定时的将某些表的数据导出excel!
在线等待,急急急,最好是详细步骤!
之前我做的作业有点问题!
帮UP
参考:http://hi.baidu.com/toiota ......
1。怎样使xp_cmdshell能完整输出超过255个字符的字符串。
2。select 时,检索速度是与from后的 TABLE顺序有关,还是与where条件的顺序有关(TABLE数据多少 )
在系统属性设定里有个选项,可以修改单字段输出字数限制. ......
我想查询出每天数据的最大的一个值。表的格式如下
表名: hisdata
字段 编号 值 状态 时间
Id value state dattime
101 32.3 0 ......
下面是XML初始文件内容
XML code:
<upd:Update xmlns:lar="http://schemas.microsoft.com/msus/2002/12/LogicalApplicabilityRules" xmlns:cmd="http://schemas.microsoft.com/msus/2002/12/Up ......