mssql 先按某列分组 然后按时间排序 急啊
需要达到的目的是 bbsanswerbatchid 这列值相同的排在一起 然后按时间升序排序
需要达到的目的是 bbsanswerbatchid 这列值相同的排在一起 然后按时间升序排序
你要的是这样的吧,如果bbsanswerbatchid 是相同的,先排前面,如果是单个的,这些单个的都按照时间来排序:
select bbsanswerbatchid,bbsanswersendtime
from
(
SELECT *,
case when (count(*) over(partition by bbsanswerbatchid)) > 1
then 1
else 0
end as bbsanswerbatchid_flag
FROM tabl
)t
ORDER BY bbsanswerbatchid_flag desc,bbsanswersendtime
;with tabl(bbsanswerbatchid,bbsanswersendtime)
as
(
select 'abc','2013-01-09' union all
select 'abc','2013-01-10' union all
select 'def','2013-09-01' union all
select 'fgh','2013-09-10'
)
select bbsanswerbatchid,bbsanswersendtime
from
(
SELECT *,
case when (count(*) over(partition by bbsanswerbatchid)) > 1
then 1
else 0
end as bbsanswerbatchid_flag
FROM tabl
)t
ORDER BY bbsanswerbatchid_flag desc,bbsanswersendtime
/*
bbsanswerbatchidbbsanswersendtime
abc 2013-01-09
abc 2013-01-10
def 2013-09-01
fgh 2013-09-10
*/
这时另一个查询去做select,那么在前一个事物提交之前,他也查询不到吧?
若是能查询到,哪不是违反了已提交读的要求?
这里关于这两个概念的解释怎么也想不通,往求解
[解决办法]
if object_id('tb') is not null
drop table tb
go
create table tb
(
bbsanswerbatchid varchar(100),
bbsanswersendtime datetime
)
insert into tb
select '423B32E3-CD7C-4EA5-83CB-333261678EAD','2013-10-22 21:55:30.533' union all
select '47EE3E0B-0164-4725-B217-394A6C802940','2013-10-22 21:53:22.673' union all
select '4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD','2013-10-22 22:14:18.970' union all
select '47EE3E0B-0164-4725-B217-394A6C802940','2013-10-22 22:13:05.270' union all
select '964AE1FE-D62D-4117-A4C9-6AB1E3979DE9','2013-10-22 21:53:38.153' union all
select '4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD','2013-10-22 21:54:58.447'
select bbsanswerbatchid,bbsanswersendtime
from
(
SELECT *,
case when (count(*) over(partition by bbsanswerbatchid)) > 1
then 1
else 0
end as bbsanswerbatchid_flag
FROM tb
)t
ORDER BY bbsanswerbatchid_flag desc,
bbsanswerbatchid, --增加了这一列来排序
bbsanswersendtime
/*
bbsanswerbatchid bbsanswersendtime
4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD2013-10-22 21:54:58.447
4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD2013-10-22 22:14:18.970
47EE3E0B-0164-4725-B217-394A6C8029402013-10-22 21:53:22.673
47EE3E0B-0164-4725-B217-394A6C8029402013-10-22 22:13:05.270
423B32E3-CD7C-4EA5-83CB-333261678EAD2013-10-22 21:55:30.533
964AE1FE-D62D-4117-A4C9-6AB1E3979DE92013-10-22 21:53:38.153
*/
if object_id('tb') is not null
drop table tb
go
create table tb
(
bbsanswerbatchid varchar(100),
bbsanswersendtime datetime
)
insert into tb
select '423B32E3-CD7C-4EA5-83CB-333261678EAD','2013-10-22 21:55:30.533' union all
select '47EE3E0B-0164-4725-B217-394A6C802940','2013-10-22 21:53:22.673' union all
select '4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD','2013-10-22 22:14:18.970' union all
select '47EE3E0B-0164-4725-B217-394A6C802940','2013-10-22 22:13:05.270' union all
select '964AE1FE-D62D-4117-A4C9-6AB1E3979DE9','2013-10-22 21:53:38.153' union all
select '4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD','2013-10-22 21:54:58.447'
--下面order by 修改了,
--如果是多条一样的,按bbsanswerbatchid和bbsanswersendtime排序
--如果是只有一条的,只按照bbsanswersendtime来排序
select bbsanswerbatchid,bbsanswersendtime
from
(
SELECT *,
case when (count(*) over(partition by bbsanswerbatchid)) > 1
then 1
else 0
end as bbsanswerbatchid_flag
FROM tb
)t
ORDER BY bbsanswerbatchid_flag desc,
case when bbsanswerbatchid_flag = 1
then bbsanswerbatchid
else convert(varchar(23),bbsanswersendtime,120)
end,
bbsanswersendtime
/*
bbsanswerbatchid bbsanswersendtime
4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD2013-10-22 21:54:58.447
4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD2013-10-22 22:14:18.970
47EE3E0B-0164-4725-B217-394A6C8029402013-10-22 21:53:22.673
47EE3E0B-0164-4725-B217-394A6C8029402013-10-22 22:13:05.270
964AE1FE-D62D-4117-A4C9-6AB1E3979DE92013-10-22 21:53:38.153
423B32E3-CD7C-4EA5-83CB-333261678EAD2013-10-22 21:55:30.533
*/
if object_id('tb') is not null
drop table tb
go
create table tb
(
bbsanswerbatchid uniqueidentifier,
bbsanswersendtime datetime
)
insert into tb
select '423B32E3-CD7C-4EA5-83CB-333261678EAD','2013-10-22 21:55:30.533' union all
select '47EE3E0B-0164-4725-B217-394A6C802940','2013-10-22 21:53:22.673' union all
select '4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD','2013-10-22 22:14:18.970' union all
select '47EE3E0B-0164-4725-B217-394A6C802940','2013-10-22 22:13:05.270' union all
select '964AE1FE-D62D-4117-A4C9-6AB1E3979DE9','2013-10-22 21:53:38.153' union all
select '4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD','2013-10-22 21:54:58.447'
--下面order by 修改了,
--如果是多条一样的,按bbsanswerbatchid和bbsanswersendtime排序
--如果是只有一条的,只按照bbsanswersendtime来排序
select bbsanswerbatchid,bbsanswersendtime
from
(
SELECT *,
case when (count(*) over(partition by bbsanswerbatchid)) > 1
then 1
else 0
end as bbsanswerbatchid_flag
FROM tb
)t
ORDER BY bbsanswerbatchid_flag desc,
case when bbsanswerbatchid_flag = 1
then cast(bbsanswerbatchid as varchar(36))
else convert(varchar(23),bbsanswersendtime,120)
end desc,
bbsanswersendtime
/*
bbsanswerbatchidbbsanswersendtime
47EE3E0B-0164-4725-B217-394A6C8029402013-10-22 21:53:22.673
47EE3E0B-0164-4725-B217-394A6C8029402013-10-22 22:13:05.270
4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD2013-10-22 21:54:58.447
4473D704-F1FB-4DDA-B5E4-C1E7BFFD94BD2013-10-22 22:14:18.970
423B32E3-CD7C-4EA5-83CB-333261678EAD2013-10-22 21:55:30.533
964AE1FE-D62D-4117-A4C9-6AB1E3979DE92013-10-22 21:53:38.153
*/