简体   繁体   English

基于选择语句SQL Server 2005的更新

[英]Update based on a select statement SQL Server 2005

I have a select statement that gives me the results I need for my update but I have no idea how to incorporate it into the update. 我有一条select语句,该语句可以为我提供更新所需的结果,但是我不知道如何将其合并到更新中。 Below is the select statement and the results. 下面是select语句和结果。

select top 20 percent
fpartno
,(fytdiss * fmatlcost) as 'total'
from inmast
where fpartno not like 'CRV%'
and fcstscode = 'A'
group by fpartno, fytdiss,fmatlcost,fabccode
order by total desc

fpartno                         total
---------------------------------------------------
1062-20-0244                172821.4800000000
B50602                      91600.7205800000
BM6031PQ                    82978.3200000000
LY2F-DC12                   74740.9500000000
BM6033SQ                    51640.4200000000
DTM06-6S-E007               49810.4700000000

My update looks like this 我的更新看起来像这样

update inmast
set fabccode = 'A'

I'm guessing my select would some how go into the where clause but I'm not sure how. 我猜我的选择将如何进入where子句,但我不确定如何。

Updating top 20 percent is tricky... because you can't put an order by in an update. 更新top 20 percent非常棘手...因为您无法在更新中下order by

I would do something like this: 我会做这样的事情:

select *
-- update t set fabccode='a'
from inmast t
where fpartno in (
    select top 20 percent fpartno
    from inmast t
    where fpartno not like 'CRV%'
    and fcstscode = 'A'
    group by fpartno, fytdiss,fmatlcost,fabccode
    order by (fytdiss * fmatlcost) desc)

Run this is a select and make sure it works for you as expected. select运行此select ,并确保它可以按预期运行。 If yes, then you can just remove the select line, and uncomment the update line. 如果是,则只需删除select行,然后取消注释update行。

Alternate solution: 替代解决方案:

select *
-- update t set fabccode='a'
from inmast t
join (select top 20 percent fpartno
      from inmast t
      where fpartno not like 'CRV%'
      and fcstscode = 'A'
      group by fpartno, fytdiss,fmatlcost,fabccode
      order by (fytdiss * fmatlcost) desc) x
on t.fpartno = x.fpartno
update inmast
set fabccode = 'A'
where fpartno in (
select top 20 percent
fpartno
from inmast
where fpartno not like 'CRV%'
and fcstscode = 'A'
group by fpartno, fytdiss,fmatlcost,fabccode
order by (fytdiss * fmatlcost) desc)

I like using CTEs as they tend to be more maintainable, they're (in my opinion) far more readable, and they seem to perform as well as nested SQL statements and oftentimes perform better (ymmv). 我喜欢使用CTE,因为它们往往更易于维护,(在我看来)它们更具可读性,并且它们的性能似乎与嵌套的SQL语句一样好,并且通常表现更好(ymmv)。

;WITH CTE_Updates AS
    (
    SELECT TOP 20 PERCENT
         fpartno
        ,(fytdiss * fmatlcost) AS 'total'
    FROM 
        inmast
    WHERE 
        fpartno NOT LIKE 'CRV%' AND 
        fcstscode = 'A'
    GROUP BY fpartno, fytdiss,fmatlcost,fabccode
    ORDER BY total DESC
    )
UPDATE CTE_Updates
SET fabccode = 'A'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM