我选择了哪种功能完美。
update ClientS set StatusID=4
where ClientID= (select P.ClientID
,LastName+' '+FirstName as Name
,Address
,max(p.PickupDate)as 'Last Pickup'
,DATEDIFF(month,max(p.PickupDate),GETDATE())as'last pickup was months ago'
from Pickup P
join Clients C on P.ClientID= C.ClientID
where WIC=0 and C.StatusID = 1 or WIC=0 and C.StatusID = 2
group by p.clientid, lastname + ' ' + firstname,address
order by 4)
我需要根据此选择中的一个last pickup was months ago
更新声明。我需要在StatusID
表中更新客户端Clients
,并将其设置为4,仅适用于此选择中超过2个月前最后一次提取的客户。
到目前为止我有这个
create procedure pr_UpdateStatusHwoPickupMoreThanTwoM
AS
update ClientS set StatusID=4
where ClientID= (select P.ClientID
,LastName+' '+FirstName as Name
,Address
,max(p.PickupDate)as 'Last Pickup'
,DATEDIFF(month,max(p.PickupDate),GETDATE())as'last pickup was months ago'
from Pickup P
join Clients C on P.ClientID= C.ClientID
where WIC=0 and C.StatusID = 1 or WIC=0 and C.StatusID = 2
group by p.clientid, lastname + ' ' + firstname,address
order by 4)
我知道这是不对的,我需要以某种方式指明最后一次提货超过2个月之前,但无法确定如何更改StatusID
仅针对那些ClientID
。任何想法怎么做?
答案 0 :(得分:1)
尝试此查询
对于SQL SERVER
update ClientS set StatusID=4
where ClientID= (select P.ClientID
from Pickup P
join Clients C on P.ClientID= C.ClientID
where WIC=0 and C.StatusID = 1 or WIC=0 and C.StatusID = 2
group by p.clientid
having max(p.PickupDate) < DATEADD(month,-2,GETDATE()));
您的内部查询返回1行的多个列,您只与clientId进行比较,因为您没有获得所需的结果。
答案 1 :(得分:1)
试试这个
update C
set StatusID=4
from (SELECT P.ClientID, MAX(p.PickupDate) MaxPickupDate
FROM @Pickup P
group by P.Clientid) P
join @Clients C on P.ClientID= C.ClientID
where C.WIC=0 and C.StatusID in (1, 2)
AND P.MaxPickupDate<DATEADD(month,-2,GETDATE())
另外,你错了吗
where WIC=0 and C.StatusID = 1 or WIC=0 and C.StatusID = 2
对于WIC = 0的任何行,这种情况都是正确的,你可以这样纠正
WHERE WIC=0 AND C.StatusID in (1,2)