求一复杂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
相关问答:
现在有两张表:文章主表A(articleId,articleTitle),文章评论表B(commentId,articleId,commentTitle)
现在我想实现这样的功能:列出文章列表,其中每篇文章标题下面列出此文章的前2个文章评论,请问sql语句怎么写啊 ......
tab1 字段:billdate,goodsid,incount,inmoney,outcount,outmoney,endprice,endcount,endamt
tab2 字段:goodsid,goodskind(商品类型)
tab3 字段:goodskind(商品类型),kindname
结果:
得到商品类型在一段时间 ......
下面是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 ......
请教高手:
以下是数据库中的三条记录,英文为字段名称
id planname TaskBeginTime Status
329 2010年03 ......
需求如下:
学院 academy(aid,aname)
班级 class(cid,cname,aid)
学生 stu(sid,sname,aid,cid)
住宿区 region(rid,rname)
宿舍楼 build(bid,rid,bnote) bnote是‘男’/‘女’
宿舍 dorm(did,rid,bid,bedn ......