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 523847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:30:17+00:00 2026-05-13T08:30:17+00:00

How do you return the max value of several columns (date and cost relationship)?

  • 0

How do you return the max value of several columns (date and cost relationship)?

TableName [ID, Date1, Cost1, Date2, Cost2, Date3, Cost3]

I need to return something like this: [ID, Most Recent Date, Cost]


I need to return something like this:

  • ID
  • Most Recent Date
  • the Cost related to the Date (if the most recent date is Date1 so the Cost is Cost1)

I need to use another table – BackupTableName:

  • ID
  • Date1
  • Cost1
  • Date2
  • Cost2
  • Date3
  • Cost3

…and use its’ data in the situation when Cost1, Cost2 and Cost3 from TableName are null.

  • 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-05-13T08:30:17+00:00Added an answer on May 13, 2026 at 8:30 am
    SELECT ID,
        CASE
            WHEN Date1>Date2 AND Date1>Date3 THEN Date1
            WHEN Date2>Date1 AND Date2>Date3 THEN Date2
            ELSE Date3
        END AS "Most Recent Date",
        CASE
            WHEN Date1>Date2 AND Date1>Date3 THEN Cost1
            WHEN Date2>Date1 AND Date2>Date3 THEN Cost2
            ELSE Cost3
        END AS Cost
    FROM TableName;
    

    ‘Date1’…‘Date2’…‘Date3’ is a schema smell. Consider normalising to a separate many-to-one table.

    ETA:

    in the situation when Cost1, Cost2 and Cost3 from TableName are null.

    You have nulls in there too? OK, this is getting really unpleasant. You’d need to protect against the nulls, like:

        CASE
            WHEN Date1>COALESCE(Date2, 0) AND Date1>COALESCE(Date3, 0) THEN Date1
            WHEN Date2>COALESCE(Date1, 0) AND Date2>COALESCE(Date3, 0) THEN Date2
            ELSE Date3
        END AS "Most Recent Date",
    

    (in both the date-selection bit and the cost-selection bit. This assumes a Cost will always normally be higher than 0.)

    I need to use another table: BackupTableName [ID, Date1, Cost1, Date2, Cost2, Date3, Cost3] and use its data

    Well… it’s possible but your model is now looking extremely precarious.

    SELECT t0.ID,
        CASE
            WHEN t0.Date1>COALESCE(t0.Date2, 0) AND t0.Date1>COALESCE(t0.Date3, 0) THEN t0.Date1
            WHEN t0.Date2>COALESCE(t0.Date1, 0) AND t0.Date2>COALESCE(t0.Date3, 0) THEN t0.Date2
            WHEN t0.Date3>COALESCE(t0.Date1, 0) AND t0.Date3>COALESCE(t0.Date1, 0) THEN t0.Date3
            WHEN t1.Date1>COALESCE(t1.Date2, 0) AND t1.Date1>COALESCE(t1.Date3, 0) THEN t1.Date1
            WHEN t1.Date2>COALESCE(t1.Date1, 0) AND t1.Date2>COALESCE(t1.Date3, 0) THEN t1.Date2
            WHEN t1.Date3>COALESCE(t1.Date1, 0) AND t1.Date3>COALESCE(t1.Date1, 0) THEN t1.Date3
        END AS "Most Recent Date",
        -- the same thing all over again for the cost selector
    FROM TableName AS t0
    LEFT JOIN BackupTableName AS t1 ON t0.ID=t1.ID;
    

    If it is at all within your power you need to normalise these tables, because the data model you have at the moment is essentially broken.

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

Sidebar

Related Questions

No related questions found

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.