Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    [MySql] Errore update complessa sql mysql

    Premetto che è la prima volta che scrivo sul vostro forum.

    Ho creato questa SQL:

    codice:
    select tc.Curr, sum(tc.Sep_StdY), sum(tc.Jan_CY)
    from tbl_tmp_conversion c, 
    kmsexport.tbl_cta_kms_data cd,
    kmsexport.tbl_kmsrefcosts rc, 
    kmsexport.tbl_kms k, 
    kmsexport.tbl_causalscategories cc, 
    tbl_kmstargetcosts tc 
    WHERE c.idUtente = 'F04305A' and 
    c.IdKeyModel = tc.idKeyModel and
    c.Year = 2011 and 
    cd.CostType = 'TVMC' and 
    cd.`Plant Desc` = 'All Plants' and 
    cd.RowType = 'D' and rc.Plant = 'All' and 
    rc.Product = cd.`Product Code` and 
    tc.Product = cd.`Product Code` and 
    cd.KMS_Year = 2011 and rc.Year = cd.KMS_Year and 
    tc.Year = cd.KMS_Year and 
    k.ProductCode = cd.`Product Code` and 
    cc.IDCausalCategory = cd.CausalID and 
    tc.IdCasual = cd.CausalID and tc.RowType = 'D' and 
    tc.Conditions = 'TARGET 2011' and 
    tc.Plant = 'All Plants' and 
    tc.CostType = 'TVMC' 
    group by cd.`Product Code`;
    Questo codice è giusto e mi tira fuori 3 colonne per 2 righe:
    CURR usd usd
    sum(tc.Sep_StdY) 0 0
    sum(tc.Jan_CY) 18,40174 56,0092

    Problema:
    i valori ritornati dalla select devono essere messi in un'altra tabella con una update; ed io ho creato questa update:

    codice:
    UPDATE tbl_tmp_conversion c, 
    kmsexport.tbl_cta_kms_data cd,
    kmsexport.tbl_kmsrefcosts rc, 
    kmsexport.tbl_kms k, 
    kmsexport.tbl_causalscategories cc, 
    tbl_kmstargetcosts tc 
    SET c.CurrTarget = tc.Curr, 
    c.TarReference =  sum(tc.Sep_StdY) , 
    c.TarActual =  sum(tc.Apr_CY)  
    WHERE c.idUtente = 'F04305A' and 
    c.IdKeyModel = tc.IdKeyModel and
    c.Year = 2011 and 
    cd.CostType = 'TVMC' and 
    cd.`Plant Desc` = 'All Plants' and 
    cd.RowType = 'D' and rc.Plant = 'All' and 
    rc.Product = cd.`Product Code` and 
    tc.Product = cd.`Product Code` and 
    cd.KMS_Year = 2011 and rc.Year = cd.KMS_Year and 
    tc.Year = cd.KMS_Year and 
    k.ProductCode = cd.`Product Code` and 
    cc.IDCausalCategory = cd.CausalID and 
    tc.IdCasual = cd.CausalID and tc.RowType = 'D' and 
    tc.Conditions = 'TARGET 2011' and 
    tc.Plant = 'All Plants' and 
    tc.CostType = 'TVMC'  
    group by cd.`Product Code`;
    mi da questo errore: Error:24/05/2011 15.56.40 0:00:00.000: MySQL Database Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group by cd.`Product Code`' at line 26

    E' abbastanza urgente, ci sono sopra da almeno 7 ore..
    Ringrazio anticipatamente!

    Anche se mi da errore di sintassi, sono sicuro che tutta la sitnassi è corretta e, se tolgo l'istruzione "group by" dalla query UPDATE (Che comunque sarebbe sbagliato perchè mi tornerebbe una sola riga con dati sbagliati) da il seguente errore:

    Error:24/05/2011 17.06.28 0:00:00.000: MySQL Database Error: Invalid use of group function

    AIUTOOOOOOOO

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2002
    Messaggi
    1,326

    Re: [MySql] Errore update complessa sql mysql

    Originariamente inviato da italdosin
    mi da questo errore: Error:24/05/2011 15.56.40 0:00:00.000: MySQL Database Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group by cd.`Product Code`' at line 26

    E' abbastanza urgente, ci sono sopra da almeno 7 ore..
    Ringrazio anticipatamente!

    Anche se mi da errore di sintassi, sono sicuro che tutta la sitnassi è corretta e, se tolgo l'istruzione "group by" dalla query UPDATE (Che comunque sarebbe sbagliato perchè mi tornerebbe una sola riga con dati sbagliati) da il seguente errore:

    Error:24/05/2011 17.06.28 0:00:00.000: MySQL Database Error: Invalid use of group function

    AIUTOOOOOOOO
    come da indicazione di errore mi sembra chiaro che l'errore sia di sintassi.

    riguardati con attenzione la sintassi del comando update

    http://www.w3schools.com/sql/sql_update.asp

    codice:
    UPDATE table_name
    SET column1=value, column2=value2,...
    WHERE some_column=some_value
    ciò che hai scritto (a mio parere) è completamente diverso

    ciao

  3. #3
    un UPDATE con il GROUP BY e' la prima volta che la vedo e spero sia pure l'ultima. Piuttosto dovresti estrarre gli id dei vari record ed usare quelli per puntare a dei record specifici da aggiornare.

    ma non mi fiderei troppo di dati estratti con la select ed il group by. Ti rende la prima riga (random per definizione) che risponde alla condizione, id incluso. Condizione peraltro complessa che potrebbe anche determinare l'unicita' del result set stesso.

    Quindi l'UPDATE dovra' limitarsi ad aggiornare i dati richiesti basandosi solo sulla condizione where e senza id se altri record rispondono al where verranno tutti aggiornati allo stesso modo. Si potrebbe usare il LIMIT 1 nell'UPDATE ma quale sara' il primo record trovato? .

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  4. #4
    La questione è che l'estrazione va bene così com'è.

    Una volta trovati i valori mi serve la group by cosicchè i dati vengano sommati per product code e non uniti insieme.

    Infatti la select mi estrae i dati correttamente.

    Non è la prima update con la group by che faccio, e l'altra è andata a buon fine, solo questa fa capricci..

    sono aperto anche a prove di modifica per l'update se vi viene in mente qualcosa.

  5. #5
    Dopo ore di duro lavoro sono riuscito a trovare un sistema ottimale:

    codice:
    update 
    tbl_tmp_conversion c ,
    (select tc.idKeyModel, 
    tc.Curr, 
    sum(tc.Sep_StdY) as TarReference, 
    sum(tc.Jan_CY) as TarActual
    from kmsexport.tbl_tmp_conversion c, 
    kmsexport.tbl_causalscategories cc, 
    tbl_kmstargetcosts tc
    where tc.RowType = 'D' and 
    tc.Conditions = 'TARGET 2011' and 
    tc.Plant = 'All Plants' and 
    tc.CostType = 'TVMC' and
    tc.idKeyModel = c.IdKeyModel and
    tc.Year = '2011' and
    cc.IDCausalCategory = tc.IdCasual
    group by tc.idKeyModel) as i
    set c.CurrTarget = i.Curr,
        c.TarReference = i.TarReference,
        c.TarActual = i.TarActual
    where c.idKeyModel =i.idKeyModel;
    RISOLTO!

  6. #6
    Originariamente inviato da italdosin
    Dopo ore di duro lavoro sono riuscito a trovare un sistema ottimale:

    RISOLTO!
    Gia'. Infatti il GROUP BY e' nella SUBQUERY SELECT e non nell'UPDATE.

    Un UPDATE condizionato dal GROUP BY oltre a generare un errore non avrebbe proprio alcun senso ne logico e ne pratico.

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  7. #7
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    1,469
    non mi intendo tanto di queste cose, ma ti suggerirei di fare attenzione alla priorità degli operatori (mettere le () è in generale cosa buona e giusta), e soprattutto perchè il campo year è a stringa?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.