Go Back   DeveloperBarn Forums > Databases > SQL Development

Sponsored Links

Discuss "Danged Update Query" in the SQL Development forum.

SQL Development - Structured Query Language (SQL) is the talk of databases. Discuss topics such as joins, triggers and other advanced SQL topics.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1  
Old July 17th, 2008, 05:02 PM
Wolffy's Avatar
Slaprentice of Wolves

 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default Danged Update Query

OK, so this is one query I can never seem to wrap my head around. I have a table with 3 fields (that are interesting), none unique.
- ProfileID uniqueidentifier
- DateAdded datetime
- PrimaryLvl int

Right now for all records PrimaryLvl is NULL. I need to run an update query to set PrimaryLvl = 1 the earliest record (defined by DateAdded) for each unique ProfileID. I've done this before, but can never seem to quite remember how.

The query I would use to return the records to be updated is:
Code:
Select ProfileId, min(DateAdded)
         From aReallyBigButIndexedTable
         Group By ProfileId
But now how to update?
__________________
Wolffy
------------------------
Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.
Reply With Quote
Sponsored Links
  #2  
Old July 17th, 2008, 05:15 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

I'm not 100% on this, but maybe something like:

Code:
 UPDATE aReallyBigButIndexedTable SET primaryLvl = 1 IN (Select ProfileId, min(DateAdded)
         From aReallyBigButIndexedTable
         Group By ProfileId)
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

Join our Folding team: DeveloperBarn Folding
Reply With Quote
  #3  
Old July 17th, 2008, 05:21 PM
Wolffy's Avatar
Slaprentice of Wolves

 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

Just after I posted, I found the answer. I'll put it here so I know where to look the next time;
Code:
UPDATE   tempInterests
SET      PrimaryLvl = #tmp.One
FROM     tempInterests
JOIN     (
             SELECT   ot.ProfileId, min(ot.DateAdded) [DateAdded], 1 as One
             FROM     tempInterests ot
             GROUP BY ot.ProfileId
         ) #tmp
ON       #tmp.ProfileId = tempInterests.ProfileId
AND         #tmp.DateAdded = tempInterests.DateAdded
There are, no doubt, a few other ways to do it, but this got the job done, and updated about 4.5M records in less the 5 minutes.

(FWIW, I prolly coulda put PrimaryLvl = 1 and it would have worked, but I haven't tried that yet)
Reply With Quote
Reply

  DeveloperBarn Forums > Databases > SQL Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2nd UpdatePanel won't update Shem .Net Development 19 July 10th, 2008 09:48 AM
Update Panel Shem .Net Development 3 July 10th, 2008 04:37 AM
update gridview dynamically peebman2000 .Net Development 27 May 8th, 2008 10:03 PM
Update sql connection string in web config peebman2000 .Net Development 5 April 18th, 2008 12:23 PM


All times are GMT -4. The time now is 08:10 PM.



Content Relevant URLs by vBSEO 3.2.0