Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7988409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:26:32+00:00 2026-06-04T12:26:32+00:00

I have two tables: A lookup table ( tabOne ): KEY | Group |

  • 0

I have two tables:

A lookup table (tabOne):

    KEY  |  Group  |  Name  |  Desc  | Val_Key
    ----------------------------------------
     1   |     a   | NameA  | DescA  |  10
     2   |     b   | NameB  | DescB  |  20
     3   |     c   | NameC  | DescC  |  30
     4   |     d   | NameD  | DescD  |  40
     5   |     e   | NameE  | DescE  |  50
     6   |     f   | NameF  | DescF  |  60

A second table containing readings (tabTwo):

    KEY  |  Date  |  Reading  | Val_Key
    ----------------------------------------
     1   |  Date   | Read     |  10
     2   |  Date   | Read     |  20
     3   |  Date   | Read     |  40
     4   |  Date   | Read     |  40
     5   |  Date   | Read     |  30
     6   |  Date   | Read     |  20
     7   |  Date   | Read     |  40
     8   |  Date   | Read     |  20
     9   |  Date   | Read     |  10
     10  |  Date   | Read     |  20
     11  |  Date   | Read     |  50
     12  |  Date   | Read     |  60

What I need to do is join tabTwo with TabOne and create a column with the newest Reading and a column with the oldest reading for each item in the group column of TabOne.

At the end of the day I want a table that look as follow:

    KEY  |  Group  |  Name  |  Desc  | Val_Key | LastReading | FirstReading |
    -------------------------------------------------------------------------
     1   |     a   | NameA  | DescA  |  10     |             |              |   
     2   |     b   | NameB  | DescB  |  20     |             |              |
     3   |     c   | NameC  | DescC  |  30     |             |              |
     4   |     d   | NameD  | DescD  |  40     |             |              |
     5   |     e   | NameE  | DescE  |  50     |             |              |
     6   |     f   | NameF  | DescF  |  60     |             |              |

Thanks!
Freddie

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:26:34+00:00Added an answer on June 4, 2026 at 12:26 pm

    If this is Sql Server 2005 or newer, outer apply will help:

    select TabOne.*, 
           last.Reading LastReading, 
           first.Reading FirstReading
    from TabOne
    outer apply
    (
      select top 1
             Reading
        from TabTwo
       where TabTwo.Val_Key = TabOne.val_Key
      order by TabTwo.Date desc
    ) last
    outer apply
    (
      select top 1
             Reading
        from TabTwo
       where TabTwo.Val_Key = TabOne.val_Key
      order by TabTwo.Date asc
    ) first
    

    Live test is @ Sql Fiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, for example: Table A Table B ======= ======= Name |
So here's the setup. I have two tables: CREATE TABLE dbo.TmpFeesToRules1(Name varchar, LookupId int)
i have two tables both are related with primary-foreign key ralation and i have
I have two tables, the first has a primary key that is an identity,
I have two lookup/reference tables (Subscribed and Unsubscribed) in my Microsoft SQL Server 2008
I have two tables: CREATE TABLE [dbo].[Context] ( [Identity] int IDENTITY (1, 1) NOT
I have two tables with a Many-To-Many relationship like this: User( emailaddress , Name)
I have two database tables. One table stores data for a commitment that a
I have two static tables with about 500 records each which provide lookup material
I have two very simple tables. Product and ProductCategory (ProductCategory is like a 'lookup'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.