update i.skucount column to be the value of i.sku count

these two queries work
query1

select i.sku, count(*) over ( partition by i.sku) cnt
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260)
order by i.sku desc

query2
select i.sku, count(*)cnt
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260)
group by i.sku
order by i.sku desc


both these queries find i.sku count
I want to update i.skucount column to be the value of i.sku count
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
This should do:
update t1
set skucount = t2.cnt
FROM orderitems t1 inner join (
select i.sku, count(*)cnt
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260)
group by i.sku) t2
on t1.sku = t2.sku

Open in new window

0
rgb192Author Commented:
the two queries in the question (done by rrjegan17)

had 109 and 39 results

but this new query had 2300 results

which makes me think that it is affecting all the orderitems... not just the paid for orderitems in statusid  (9,10,90,180,260)


0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Try this:
update t1
set skucount = t2.cnt
FROM orderitems t1 inner join (
select i.sku, i.productid, count(*) over ( partition by i.sku) cnt
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260)
group by i.sku) t2
on t1.sku = t2.sku
and t1.productid = t2.productid

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.