Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Query assistance
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01108620
Message ID:
01108621
Vues:
8
This message has been marked as the solution to the initial question of the thread.
Try
SELECT * FROM mytable mt1
  WHERE  date = ( SELECT MIN(date) FROM mytable WHERE fk = mt1.fk) 
      OR date = ( SELECT MAX(date) FROM mytable WHERE fk = mt1.fk) 
>I need a little help with a query. Assume I have the following table:
>
>
>pk    fk     date
>--    --     ----
>1     1      1/1/1976
>2     2      1/2/1976
>3     2      1/1/1969
>4     1      1/15/1976
>5     1      1/15/2006
>6     1      8/10/1989
>7     2      12/25/2005
>.
>.
>.
>
>
>In my result set I would like 2 records per unique FK, one record is the min date and the other record is the max date. For the example above the result would look something like this:
>
>
>pk    fk     date
>--    --     ----
>1     1      1/1/1976
>3     2      1/1/1969
>5     1      1/15/2006
>7     2      12/25/2005
>
>
>Is what I am asking possible? Would I be able to use UNION (first get all the min dates and then all the max dates)?
>
>Thanks,
>Einar
--sb--
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform