• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Reply to: SQL question

Collapse

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "SQL question"

Collapse

  • lilelvis2000
    replied
    Originally posted by oraclesmith View Post
    Update table2 t2 set nolines = (select count(*) from table1 t1 where t1.id=t2.id and t1.somecode=2)

    It won't be too efficient, I haven't tested it and some versions of SQL aren't happy with it.

    You could just re-create table2 to get all the counts; create table2 as select id, desc, count(*) nolines from table1 group by id, desc


    Note; not sure this is going to work in the way you want, because you may not want to update all the rows in t2.

    Sql server update statment syntax is a bit goofy: I think it should be like

    update table t2 set nolines = t3.numlines from (select id, count(*) numlines from table t1 group by id) t3 where t2.id=t3.id

    which might work...I haven't done T-SQL in a loooong time.

    Leave a comment:


  • oraclesmith
    replied
    In Oracle SQL you can dynamically create a 'table', which only exists for the duration of the query. It's tough on resources but it might work in other SQL's too.

    eg.

    Update table2 t2 set nolines = (select t3.numlines from
    table1 t1, (select id, count(*) numlines from table1 group by id) t3
    where t1.id = t2.id
    and t1.id = t3.id
    and t1.somecode = 2)


    Usual caveats apply.

    Leave a comment:


  • EternalOptimist
    replied
    Originally posted by Pogle View Post
    do it in SSIS if you're using 2005 - and you get to add a new skill to your CV
    I'm under pressure, dont have time to do it properly.

    byeeeee

    Leave a comment:


  • Pogle
    replied
    Originally posted by EternalOptimist View Post
    Fails in sql 2005
    cannot use aggregate in update row


    Indeedy, I have had to create a third table, which as you say, is Sh 1 te



    do it in SSIS if you're using 2005 - and you get to add a new skill to your CV

    Leave a comment:


  • EternalOptimist
    replied
    Originally posted by oraclesmith View Post
    Update table2 t2 set nolines = (select count(*) from table1 t1 where t1.id=t2.id and t1.somecode=2)

    It won't be too efficient, I haven't tested it and some versions of SQL aren't happy with it.

    You could just re-create table2 to get all the counts; create table2 as select id, desc, count(*) nolines from table1 group by id, desc


    Note; not sure this is going to work in the way you want, because you may not want to update all the rows in t2.
    Fails in sql 2005
    cannot use aggregate in update row


    Indeedy, I have had to create a third table, which as you say, is Sh 1 te



    Leave a comment:


  • oraclesmith
    replied
    Update table2 t2 set nolines = (select count(*) from table1 t1 where t1.id=t2.id and t1.somecode=2)

    It won't be too efficient, I haven't tested it and some versions of SQL aren't happy with it.

    You could just re-create table2 to get all the counts; create table2 as select id, desc, count(*) nolines from table1 group by id, desc


    Note; not sure this is going to work in the way you want, because you may not want to update all the rows in t2.
    Last edited by oraclesmith; 14 May 2009, 10:17.

    Leave a comment:


  • EternalOptimist
    started a topic SQL question

    SQL question

    syntax quessie

    i have a table ID,description,somecode (1,000 records)



    I create a second table grouped on ID, descr,0 as noLines from table 1 (50 records)


    I want to update table2.noLines withe the count from table1 where table1.somecode = 2, joined on id



Working...
X