Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Query assistance
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01108620
Message ID:
01108621
Views:
10
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--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform