Results 1 to 7 of 7

Thread: [RESOLVED] All records based on aggregate function

  1. #1

    Thread Starter
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843

    Resolved [RESOLVED] All records based on aggregate function

    My SQL is a bit rusty and I'm trying to clean up some data.

    What i need is to do is UPDATE some of the records in a table where
    the COUNT for [field1] = 2.

    My initial select query gives me the set to look through, but now what?
    I need to change some [tbl_item].itemvalue in this set, where itemvalue = 'baddata' make it 'gooddata'


    select itemcode,count(itemcode) from tbl_item
    group by itemcode
    having count(itemcode)=2


    in other words, there are some records where itemvalue='baddata' and we do not want to change them, unless that itemcode is in the table exactly twice.
    Merry Christmas

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: All records based on aggregate function

    Do you want to change both imtecodes or only one of them?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843

    Re: All records based on aggregate function

    I want to change itemvalue, when itemvalue is 'baddata' only in records where that record's itemcode occurs exactly 2 times in the table.
    Merry Christmas

  4. #4

    Thread Starter
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843

    Re: All records based on aggregate function

    Got it.

    I have to use "WHERE [] in"

    Select * FROM tbl_item
    Where itemcode in
    (select itemcode,count(itemcode) from tbl_item
    group by itemcode
    having count(itemcode)=2)

    This give me the set of records that is eligible for updating.
    Now, I'll just put an update around this, check for condition, and I'll be fine.


    I'd change the title to [resolved] but my edit function does not allow me to.
    Merry Christmas

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: All records based on aggregate function

    The easy way is to click on "Thread tools" just above the first post in this thread, then "Mark thread resolved".

  6. #6

    Thread Starter
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843

    Re: [RESOLVED] All records based on aggregate function

    Another easy way would be to give me access to "my" own thread titles.



    RESOLVED! thanks
    Merry Christmas

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] All records based on aggregate function

    You should have that already - by clicking on "Edit" in the first post, then "Go Advanced".

    However, it is easier (and doesn't leave an "edited by" message) to use the feature provided.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width