Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SET value WHERE Product has > 2 Suppliers


SET value WHERE Product has > 2 Suppliers

Author
Message
2Tall
2Tall
Say Hey Kid
Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)

Group: General Forum Members
Points: 704 Visits: 1203
Hi. I have been asked to SET a value in a table WHERE the Product has > 2 Suppliers.

I have created a script to create the table in tempDB + INSERT 2000 rows of test data.

The field to update is 'SupplierSequence'. By default they are all set to '999'. Where a Product has more than 2 suppliers the Product with the latest 'DateLastQuoted' then the 'SupplierSequence' should be set to '500'.

However I think some Products have more than 2 suppliers + the DateLastQuoted are the same (do not know how to handle those, might not be that many).

Can anyone help on this?
Thanks,
Phil.

-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

Tommy Cooper

Attachments
Query.txt (8 views, 131.00 KB)
null-512060
null-512060
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 360
Please try (but if values in DateLastQuoted are the same it will update all records with the DateLastQuoted):

UPDATE dbo.Forum SET SupplierSequence = '500'
FROM dbo.Forum f1
INNER JOIN (SELECT Product, MAX(DateLastQuoted) DateLastQuoted
FROM dbo.Forum
GROUP BY Product
HAVING COUNT(1) > 1) f2
ON f1.Product = f2.Product
AND f1.DateLastQuoted = f2.DateLastQuoted


2Tall
2Tall
Say Hey Kid
Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)

Group: General Forum Members
Points: 704 Visits: 1203
Thanks NULL. Excuse the error in my script (lack of INSERT INTO......I deleted in error!).
Many Thanks,
Phil.

-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

Tommy Cooper

2Tall
2Tall
Say Hey Kid
Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)

Group: General Forum Members
Points: 704 Visits: 1203
Just a thought......after the update is there a way to filter WHERE Products appear more than once + WHERE the DateLatQuoted match AND SequenceNumber = 500.

On the 'Live' data 3000 records were updated.

There should not be that many, I can then decide which value to reverse back to 999 manually.
Thanks,
Phil.

-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

Tommy Cooper

null-512060
null-512060
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 360
Try:

SELECT dbo.forum.Product
,dbo.forum.Supplier
,dbo.forum.SupplierCrossReference
,dbo.forum.DateLastQuoted
FROM dbo.forum
WHERE SupplierSequence = '500'
AND Product IN (SELECT Product
FROM dbo.Forum
WHERE SupplierSequence = '500'
GROUP BY Product
HAVING COUNT(1) > 1)


or
               
SELECT dbo.forum.Product
,dbo.forum.Supplier
,dbo.forum.SupplierCrossReference
,dbo.forum.DateLastQuoted
FROM dbo.forum
INNER JOIN (SELECT Product
,MAX(DateLastQuoted) DateLastQuoted
FROM dbo.Forum
WHERE SupplierSequence = '500'
GROUP BY Product
HAVING COUNT(1) > 1) p
ON p.Product = dbo.forum.Product
AND p.DateLastQuoted = dbo.forum.DateLastQuoted
WHERE dbo.forum.SupplierSequence = '500'


Ramesh Saive
Ramesh Saive
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2564 Visits: 2643
I think the OP wants to update products where suppliers for that product > 2. So, should the rows be filtered by COUNT(1) > 2?

--Ramesh


null-512060
null-512060
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 360
Yes, Ramesh is right, it should be > 2
2Tall
2Tall
Say Hey Kid
Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)

Group: General Forum Members
Points: 704 Visits: 1203
Thanks Guys.

HAVING COUNT(1) > 1 appears to be correct (returned 106 rows), HAVING COUNT(1) > 2 returned 0.

Great Work,
Phil.

-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

Tommy Cooper

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search