以下问题的最佳解决方案是什么?
所以我有一个存储过程插入临时表信息,如:
Id | Name | Date | Age | Bin |Status| Description | Warn1
-----------------------------------------------------------------------
01 | abcd |2016-01-01 15:00:00| 17 | 0 | 40 | Done | 0
01 | abcd |2016-01-01 11:00:00| 17 | 0 | 30 | Waiting | 0
01 | abcd |2016-01-01 10:00:00| 17 | 0 | 10 | Started | 0
02 | zxcv |2016-01-02 11:30:00| 18 | 0 | 35 | Error | 0
02 | zxcv |2016-01-02 11:00:00| 18 | 0 | 30 | Waiting | 0
02 | zxcv |2016-01-02 10:00:00| 18 | 0 | 10 | Started | 0
03 | yttr |2016-01-02 12:30:00| 16 | 0 | 30 | Waiting | 0
03 | yttr |2016-01-02 10:00:00| 16 | 0 | 10 | No desc | 0
04 | huuo |2016-01-02 11:30:00| 17 | 0 | 40 | Done | 0
04 | huuo |2016-01-02 09:00:00| 17 | 0 | 30 | Waiting | 0
04 | huuo |2016-01-02 08:00:00| 17 | 0 | 10 | Started | 0
05 | test |2016-01-03 10:00:00| 10 | 0 | 11 | Error | 0
05 | test |2016-01-03 09:00:00| 10 | 0 | 10 | Started | 0
我有2个变量@maxStatus和@description,我想保留所有相同id的所有信息Status = @maxStatus和Description = @description。
例如,如果@maxStatus = 40且@description ='完成'结果表应该是:
Id | Name | Date | Age | Bin |Status| Description | Warn1
-----------------------------------------------------------------------
01 | abcd |2016-01-01 15:00:00| 17 | 0 | 40 | Done | 0
01 | abcd |2016-01-01 11:00:00| 17 | 0 | 30 | Waiting | 0
01 | abcd |2016-01-01 10:00:00| 17 | 0 | 10 | Started | 0
04 | huuo |2016-01-02 11:30:00| 17 | 0 | 40 | Done | 0
04 | huuo |2016-01-02 09:00:00| 17 | 0 | 30 | Waiting | 0
04 | huuo |2016-01-02 08:00:00| 17 | 0 | 10 | Started | 0
如果@maxStatus = 11且@description ='错误'结果表应该是:
Id | Name | Date | Age | Bin |Status| Description | Warn1
-----------------------------------------------------------------------
05 | test |2016-01-03 10:00:00| 10 | 0 | 11 | Error | 0
05 | test |2016-01-03 09:00:00| 10 | 0 | 10 | Started | 0
编辑:澄清:如果具有最大状态= @maxStatus
,则sp应仅返回ID的行例如@maxStatus = 30和@description =' Waiting' ,maxium状态为30且等待描述的唯一ID是' 03' ;所有其他人的最大状态与@maxStatus
不同 Id | Name | Date | Age | Bin |Status| Description | Warn1
-----------------------------------------------------------------------
03 | yttr |2016-01-02 12:30:00| 16 | 0 | 30 | Waiting | 0
03 | yttr |2016-01-02 10:00:00| 16 | 0 | 10 | No desc | 0
答案 0 :(得分:0)
您可以使用exists
:
select t.*
from #tmp t
where exists (select 1
from #tmp t2
where t2.id = t.id and
t2.Status = @maxStatus and
t2.Description = @description
);
如果你的表很大,那么(id, status, description)
上的索引会有所帮助。
编辑:
您似乎希望状态的最大值匹配。我不确定description
与问题的真正关系,因为每个状态的描述都是相同的。所以,你可以这样做:
select t.*
from (select t.*, max(status) over (partition by id) as maxstatus
from #tmp t
) t
where maxStatus = @maxStatus
答案 1 :(得分:0)
您可以如下所示:
SELECT
*
FROM
Tbl A
WHERE
A.Id IN (SELECT B.Id FROM Tbl B WHERE B.Description = @description) AND
Status <= @maxStatus
<强>更新强>
SELECT
*
FROM
Tbl A
WHERE
A.Id IN (SELECT B.Id FROM Tbl B WHERE B.Description = @description) AND
A.Id IN (SELECT C.Id FROM (SELECT B.Id, MAX(Status) MaxStatus FROM Tbl B GROUP BY B.Id) C WHERE C.MaxStatus <= @maxStatus )
答案 2 :(得分:0)
试试这个,
SELECT id, name, date, age, bin, status, description, warn1
FROM TABLE_NAME
WHERE id IN
(
SELECT id
FROM TABLE_NAME
WHERE status=@maxStatus
AND description=@description
)