對應昨天的游標改成子表查詢

2021-05-21 21:59:41 字數 3164 閱讀 2767

set ansi_nulls on

set quoted_identifier on

goalter procedure [dbo].[tradezone_review_plan]

asbegin

declare @temprowid nvarchar(32);

--declare @reviewtime nvarchar(10);

--declare @reviewyear nvarchar(4);

--declare @reviewmonth nvarchar(2);

--declare @tempreviewtime nvarchar(10);

set @temprowid = replace(newid(),'-','');

with d(

row_id,

brand,

review_date

)as(

select a.row_id,a.brand,b.review_date

from t_nwp_tradezone_info a

left join t_nwp_tradezone_review b

on a.row_id = b.fk_row_id

where b.review_date is null

),x as(

select e.row_id,e.brand,f.review_date from t_nwp_tradezone_info e

join t_nwp_tradezone_review f

on e.row_id = f.fk_row_id

where f.row_id = (

select top 1 g.row_id from t_nwp_tradezone_review g where e.row_id = g.fk_row_id order by g.review_date desc

)union all

select d.row_id,d.brand,convert(nvarchar(10), h.create_time,20) as review_date from d

join h_nwp_trade_plan_report h

on d.row_id = h.fk_row_id

where h.row_id = (

select top 1 i.row_id from h_nwp_trade_plan_report i where i.fk_row_id = d.row_id order by i.create_time))

insert into t_nwp_tradezone_review_test_plan

(row_id,

tradezone_id,

group_id,   

sort_id,

plan_type,

brand,

year,

month,

test_plan_date,

year_plan_flag,

month_plan_flag,

no_review_flag,

no_review_reason,

audit_flag,

change_flag,

creator_id,

create_time,

updator_id,

update_time,

delete_flag,

test_plan_bak_date       

)select replace(newid(),'-',''),

x.row_id,

replace(newid(),'-',''),

'0',

'm1121',

x.brand,               

case when (convert(nvarchar(4), getdate(),20) - cast(convert(nvarchar(4),x.review_date) as int)) > 0

then (convert(nvarchar(4), getdate(),20) + 1 )

else (convert(nvarchar(4), x.review_date,20) + 1 )

end,

case when (convert(nvarchar(4), getdate(),20) - cast(convert(nvarchar(4),x.review_date) as int)) > 0

then '01'

else substring(x.review_date,6,2)

end,

case when (convert(nvarchar(4), getdate(),20) - cast(convert(nvarchar(4),x.review_date) as int)) > 0

then cast((convert(nvarchar(4), getdate(),20) + 1 ) as nvarchar(4))+ '-01-01'

else replace(x.review_date,substring(x.review_date,1,4),substring(x.review_date,1,4)+1)

end,

'm0009',

'm0009',

'm0817',

'm1170',

'','',

'admin',

getdate(),

'admin',

getdate(),

'1',

case when (convert(nvarchar(4), getdate(),20) - cast(convert(nvarchar(4),x.review_date) as int)) > 0

then cast((convert(nvarchar(4), getdate(),20) + 1 ) as nvarchar(4))+ '-01-01'

else replace(x.review_date,substring(x.review_date,1,4),substring(x.review_date,1,4)+1)

end                      

from x                                       

end

postgresql 查詢昨天的資料

pgsql查詢今天的資料 select from 表名 as n where n.create date current date pg查詢昨天的資料 方法1 select from 表名 as n where age current date,to timestamp substring to c...

原創 把SQL的動態查詢改成SQL查詢

在我們使用儲存過程的時候,有時為了組合查詢條件,不得不使用動態查詢。比如下面的 create proc usp search city intas begin declare sql varchar 8000 set sql n select from testtable where 1 1 if ...

查詢對應的括號

c c 練習題 查詢對應的括號。如 在位置3出的左括號其右括號所在的位置為4 查詢對應的括號。如 在位置3出的左括號其右括號所在的位置為4 include include include include using std cin using std cout using std endl enum...