Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 20
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2003
    Messaggi
    78

    ORDINARE due SELECT unite da UNION

    Ciao a tutti,

    ho una tabella e una query. Voglio ottenere tutti i record della tabella ordinati con ID crescente fino all'ID numero 104 e con ID decrescente dal 104 in poi...

    L'unica soluzione che mi è venuta è stata questa

    codice:
    " SELECT * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID > 104 " &_
    " ORDER BY ID DESC " &_
    " UNION ALL" &_
    " SELECT * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID <= 104 " &_
    " ORDER BY ID asc "
    ma non funziona! Ordina tutto in modo crescente!

    Come posso fare?

    Grazie,
    Ale

  2. #2
    nella union se non ricordo male vince solo 1 dei due ordinamenti

    edit: fai due query
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

  3. #3
    prova con

    codice:
    " SELECT ID AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID <= 104 " &_
    " ORDER BY 1 " &_
    " UNION ALL" &_
    " SELECT 1000000-ID AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID > 104 " &_
    " ORDER BY 1 "

  4. #4
    Ciao,
    queste sono due possibili soluzioni scritte per Sql Server
    ma che suppogo si possono adattare anche ad altri db.


    codice:
    --crezione e popolamento tabella di prova
    
    create table tord(ID int)
    go
    
    Insert into tord values(1)
    Insert into tord values(2)
    Insert into tord values(3)
    Insert into tord values(4)
    Insert into tord values(5)
    go
    
    -- soluzione 1 
    select  1 as ord1, id  as ord2 ,  *
    from tord where id<=2  
    Union all
    select  2 as ord1, (-1*id ) as ord2 ,  *
    from tord where id>2  
    Order by ord1, ord2
    codice:
    -- soluzione 2
    Select q1.* from
    (
     select top 99.999999999 percent * from tord
     where id<=2   order by id asc
    ) as q1 
    Union all
    Select q2.* from
    (
     select top 99.999999999 percent * from tord
     where id>2  order by id desc
    ) as q2

  5. #5
    Utente di HTML.it
    Registrato dal
    Nov 2003
    Messaggi
    78
    Originariamente inviato da optime
    prova con

    codice:
    " SELECT ID AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID <= 104 " &_
    " ORDER BY 1 " &_
    " UNION ALL" &_
    " SELECT 1000000-ID AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID > 104 " &_
    " ORDER BY 1 "

    Mi esce questo errore:

    Microsoft OLE DB Provider for ODBC Drivers error '80004005'

    [Microsoft][Driver ODBC Microsoft Access] Numero di colonne non corrispondente nelle due tabelle o query selezionate di una query di unione.


  6. #6
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427
    il nome dei campi devono essere uguali nelle query con Union.

    codice:
    " SELECT ID AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID <= 104 " &_
    " ORDER BY 1 " &_
    " UNION ALL" &_
    " SELECT 1000000-ID   AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID > 104 " &_
    " ORDER BY 1 "

  7. #7
    Utente di HTML.it
    Registrato dal
    Nov 2003
    Messaggi
    78
    Quindi?? Nessuna soluzione?

    In caso, se dovessi fare due query, è possibile creare un recordset con il risultato di entrambe?

    Grazie

  8. #8
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427
    " SELECT ID AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID <= 104 " &_
    " ORDER BY 1 " &_
    " UNION ALL" &_
    " SELECT ID AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID > 104 " &_
    " ORDER BY 1 "

  9. #9
    Utente di HTML.it
    Registrato dal
    Nov 2003
    Messaggi
    78
    Originariamente inviato da agenti
    " SELECT ID AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID <= 104 " &_
    " ORDER BY 1 " &_
    " UNION ALL" &_
    " SELECT ID AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID > 104 " &_
    " ORDER BY 1 "
    ancora errore

    ADODB.Recordset error '800a0cc1'
    Impossibile trovare l'oggetto nell'insieme corrispondente al nome o al numero richiesto.


  10. #10
    ringraziare la simpatia di access...

    codice:
    " SELECT (ID + 0) AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID <= 104 " &_
    " ORDER BY 1 " &_
    " UNION ALL" &_
    " SELECT (1000000 - ID) AS myID, * " & _
    " FROM Sottoprodotti" &_
    " WHERE ID > 104 " &_
    " ORDER BY 1 "

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 © 2026 vBulletin Solutions, Inc. All rights reserved.