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
*/
[解决办法]
oracle的
DB Block Gets和Consistent Gets怎么理解?
网上通用的解释越看越糊涂
1、DB Block Gets(当前请求的块数目)当前模式块意思就是在操作中正好提取的块数目,而不是在一致性读的情况下而产生的块数。正常的情况下,一个查询提取的块是在查询开始的那个时间点上存在的数据块,当前块是在这个时刻存在的数据块,而不是在这个时间点之前或者之后的数据块数目。
2、Consistent Gets(数据请求总数在回滚段Buffer中的数据一致性读所需要的数据块)这里的概念是在处理你这个操作的时候需要在一致性读状态上处理多少个块,这些块产生的主要原因是因为由于在你查询的过程中,由于其他会话对数据块进行操作,而对所要查询的块有了修改,但是由于我们的查询是在这些修改之前调用的,所以需要对回滚段中的数据块的前映像进行查询,以保证数据的一致性。这样就产 生了一致性读。
这个怎么和sqlserver的逻辑读做对比,
就拿已提交读这个隔离级别来说,哪有什么一致不一致读的问题,读到的数据都是已提交的数据
假如一个事物正在修改某条数据,
这时另一个查询去做select,那么在前一个事物提交之前,他也查询不到吧?
若是能查询到,哪不是违反了已提交读的要求?
这里关于这两个概念的解释怎么也想不通,往求解
[解决办法]
bbsanswerbatchid 相同的没有列在一起
我修改了一下,现在可以了,你试试:
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
*/
[解决办法]
呵呵,改了一下后面的order by:
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
*/
[解决办法]
另外,我把列的类型改成了uniqueidentifier了,测试了也没有报错:
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
*/