1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

anyone decent with ms-sql?

Discussion in 'BBS Hangout' started by Nikos, Oct 14, 2004.

Tags:
  1. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    Im trying to update a table based on a count from another table (using a join).

    Anyone good with sql that could help me understand how to do this?

    thanks
     
  2. noemi

    noemi Member

    Joined:
    May 14, 1999
    Messages:
    32
    Likes Received:
    0
    SQL> CREATE TABLE X (KNT NUMBER);

    Table created

    SQL> SELECT COUNT(*) FROM Y;

    COUNT(*)
    ----------
    4781

    SQL>
    SQL> INSERT INTO X
    2 SELECT COUNT(*) FROM Y;

    1 row inserted

    SQL> SQL>
    SQL> SELECT * FROM X;

    KNT
    ---
    4781


    I am not sure if this what you want happen
     
  3. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    Can I email you the example? Or something close to it?
     
  4. noemi

    noemi Member

    Joined:
    May 14, 1999
    Messages:
    32
    Likes Received:
    0
    sorry ... I did not read your question very well, this is an example of SQL updating count based on the result of two tables join together ...



    SQL> update x
    2 set knt = (select count(*) from
    3 a , b
    4 where a_pidm = b_pidm);

    1 row updated

    SQL> commit;

    Commit complete

    SQL> select * from x;

    KNT
    -----------
    34065
     
  5. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    The setup of my code is suppose to go something like this

    update groups g1
    set NumMems = (select count(...)
    from groups as g2, members as m
    where ...)

    groups is gid, gname, and Num Mems

    members table has member information and groupid # which is what I am trying to count. There are like 10 groups and each has a certain amount of people in each. I just want to be able to join the two tables, and insert the counts for each group into NumMems
     
  6. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    I donno if I'm understanding you correctly, this work?

    update groups g1
    set g1.NumMems = (select count(*)
    from members as m
    where m.groupid = g1.groupid
    group by groupID)
     
  7. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    gid the unique definition of a group in the groups table

    in the members table it is called groupid and it shows that for each member.

    Im trying to count those in the members table while joining it to the groups.gid = members.groupid
     
  8. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    Yeah, this should work. Ignore my last post, it is incorrect syntax

    Update groups
    set NumMems = (select count(*)
    from members
    where members.groupid = groups.groupid
    group by groupid )

    This should work. It will get a count of each groupid in the members table and insert it inthe groups table for each groupid
     
  9. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    dont you mean 'members.gid'

    As I mentioned GID is the name of the field in the members table, so in that case do i just change it to that and it should be OK?
     
  10. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    actually I meant groups.gid b/c groups.gid = members.groupid right?


    Update groups
    set NumMems = (select count(*)
    from members
    where members.groupid = groups.gid
    group by groupid )
     
  11. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    Update groups
    set NumMems = (select count(*)
    from members
    where members.groupid = groups.gid
    group by groupid)

    sorry gid is actually in groups, and groupid is in members, so i did it that way!
     
  12. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    Glad it worked for you. If this messings up production data.. um you don't know me.
     
  13. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    do you have an email :D

    No its just practice examples.....

    I been having crazy troubles with this.....
     
  14. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    you familiar with triggers -- so that the groupid column can maintain the correct values on update....?
     
  15. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    I know a little about triggers...

    What is that that you want to do, update the memebers.groupid column when an update occurs?
     
  16. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    I want to write a trigger that maintains the correct values of NumMems on update (of the groupid column in members table), or on insert or delete in the members table.

    After updating, deleting or inserting a command it may affect more than one row. I need to test a trigger by inserting rows in the members table, deleting rows from the members table, updating a row by changing the groupid.
     
  17. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    Since it's doing a count, you'll only have to recalc the NumMems after an insert occured in the members table. An update shouldn't affect the count.

    You can create the following trigger

    CREATE TRIGGER UpdateNumMems ON members
    After Insert
    AS
    Update groups
    set NumMems = (Select count(*)
    from members
    where groupid= Inserted.groupid)
    from Inserted
    where groups.gid= Inserted.groupid
     
  18. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    Is the update and deleting factored in?

    I ran that code and it said it did it succesfully, but what did do exactly, do I need to TEST by trying to add members?
     
  19. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    An update statement may change the groupid in this case.

    So would this still be ok?
     
  20. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    My bad, i forgot about delete, as that would affect the count as well.

    Since the update will change the groupid, then you'll also have to create a update trigger. I wrongly assumed it was part of a key, so it wouldn't change.

    I personally like to create an seperate trigger for update/delete/insert.

    CREATE TRIGGER MembersDelete ON members
    After Delete
    AS
    Update groups
    set NumMems = (Select count(*)
    from members
    where groupid= Deleted.groupid)
    from Deleted
    where groups.gid= Deleted.groupid

    The update trigger would be the same, except with the update key word. I think it uses the inserted alias like an insert.

    You can see if it actually works by inserting/updating/deleting records in the member table. Then check the groups table to see if there is an accurate count.
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now