Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2004
    Messaggi
    175

    Sql Server query evitare Union

    Scusate ma non c'è un metodo + semplice ed elegante per ottenere questo risultato:

    select top 2 city, name, description from tabella where city = 'roma'
    union all
    select top 2 city, name, description from tabella where city = 'milano'
    union all
    select top 2 city, name, description from tabella where city = 'venezia'
    union all
    select top 2 city, name, description from tabella where city = 'bologna'
    union all
    select top 2 city, name, description from tabella where city = 'napoli'
    union all
    select top 2 city, name, description from tabella where city = 'amsterdam'
    union all
    select top 2 city, name, description from tabella where city = 'vienna'
    union all
    select top 2 city, name, description from tabella where city = 'lisbona'
    union all
    select top 2 city, name, description from tabella where city = 'madrid'
    union all
    select top 2 city, name, description from tabella where city = 'pisa'

    il tempo di esecuzione non è neanche troppo alto ma magari si puo fare di meglio!

  2. #2
    tu vuoi le prime due di ogni città?

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2004
    Messaggi
    175
    certo le prime due !

  4. #4
    Originariamente inviato da plumb
    certo le prime due !
    c'è la union perchè vuoi ordinarle in quella maniera particolare o non ti interessa?
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

  5. #5
    Originariamente inviato da plumb
    certo le prime due !
    penso che la UNION sia l'unica

  6. #6
    se c'è il campo id forse potrebbe funzionare:

    codice:
    select t1.city,t1.name,t1.description from tabella t1 where 
         t1.id in (select top 2 t2.id from tabella t2 where t2.city=t1.city order by t2.id asc)
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

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.