stored procedures - Sql Server: Compare multiple dates, remove duplicates and add new column all in one -
what best solution following problem?
so have stored procedure inserts temporary table information so:
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
i have 2 variables @maxstatus , @description, keep information of same id's have status = @maxstatus , description = @description.
for example if @maxstatus = 40 , @description='done' resulting table should be:
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
if @maxstatus = 11 , @description='error' resulting table should be:
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
edit: clarification: sp should return rows of id if has maximum status = @maxstatus
if example @maxstatus = 30 , @description='waiting' , id has maxium status of 30 , description of waiting '03' ; others have maximum status different @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
you can below:
select * tbl a.id in (select b.id tbl b b.description = @description) , status <= @maxstatus
updated
select * tbl a.id in (select b.id tbl b b.description = @description) , a.id in (select c.id (select b.id, max(status) maxstatus tbl b group b.id) c c.maxstatus <= @maxstatus )
Comments
Post a Comment