0

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. Below is the select statement and the results.

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.

4
  • So you want to set fabccode to 'A' for all the rows from your resultset?
    – NickyvV
    Commented Jun 19, 2014 at 13:49
  • 2
    See stackoverflow.com/questions/2334712/…. Commented Jun 19, 2014 at 13:50
  • correct the select statement gives me the top 20 percent of parts based on their totals. Commented Jun 19, 2014 at 13:52
  • Joe thanks for the link but I am not using to tables so I don't believe a join would work here. Commented Jun 19, 2014 at 14:09

3 Answers 3

1

Updating top 20 percent is tricky... because you can't put an order by in an update.

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. If yes, then you can just remove the select line, and uncomment the update line.

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
2
  • Jiggs this is great I didn't know you could do a join on a table and a select statement. If nothing else I will have to play with that. It could open up a whole new world for me. Commented Jun 19, 2014 at 14:31
  • Glad to help. If you're learning, also check into WHERE EXISTS (...) -- it's great for filtering (and fast), but it doesn't work with TOP statements.
    – Dave C
    Commented Jun 19, 2014 at 15:05
0
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)
2
  • there is no total column, its a calculation, your order by will throw an error.
    – Dave C
    Commented Jun 19, 2014 at 14:09
  • 1
    Oh, i'm sorry. Edited my answer
    – MikkaRin
    Commented Jun 19, 2014 at 14:21
0

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).

;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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.