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

  • Home
  • SEARCH
  • 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 102625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:59:43+00:00 2026-05-11T00:59:43+00:00

Given the following table in SQL Server 2005: ID Col1 Col2 Col3 — —-

  • 0

Given the following table in SQL Server 2005:

ID   Col1   Col2   Col3 --   ----   ----   ---- 1       3     34     76   2      32    976     24 3       7    235      3 4     245      1    792 

What is the best way to write the query that yields the following result (i.e. one that yields the final column – a column containing the minium values out of Col1, Col2, and Col 3 for each row)?

ID   Col1   Col2   Col3  TheMin --   ----   ----   ----  ------ 1       3     34     76       3 2      32    976     24      24 3       7    235      3       3 4     245      1    792       1 

UPDATE:

For clarification (as I have said in the coments) in the real scenario the database is properly normalized. These ‘array’ columns are not in an actual table but are in a result set that is required in a report. And the new requirement is that the report also needs this MinValue column. I can’t change the underlying result set and therefore I was looking to T-SQL for a handy ‘get out of jail card’.

I tried the CASE approach mentioned below and it works, although it is a bit cumbersome. It is also more complicated than stated in the answers because you need to cater for the fact that there are two min values in the same row.

Anyway, I thought I’d post my current solution which, given my constraints, works pretty well. It uses the UNPIVOT operator:

with cte (ID, Col1, Col2, Col3) as (     select ID, Col1, Col2, Col3     from TestTable ) select cte.ID, Col1, Col2, Col3, TheMin from cte join (     select         ID, min(Amount) as TheMin     from          cte          UNPIVOT (Amount for AmountCol in (Col1, Col2, Col3)) as unpvt     group by ID ) as minValues on cte.ID = minValues.ID 

I’ll say upfront that I don’t expect this to offer the best performance, but given the circumstances (I can’t redesign all the queries just for the new MinValue column requirement), it is a pretty elegant ‘get out of jail card’.

  • 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. 2026-05-11T00:59:43+00:00Added an answer on May 11, 2026 at 12:59 am

    There are likely to be many ways to accomplish this. My suggestion is to use Case/When to do it. With 3 columns, it’s not too bad.

    Select Id,        Case When Col1 < Col2 And Col1 < Col3 Then Col1             When Col2 < Col1 And Col2 < Col3 Then Col2              Else Col3             End As TheMin From   YourTableNameHere 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 97k
  • Answers 97k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why would the proxy encode anything? Is this service described… May 11, 2026 at 7:26 pm
  • Editorial Team
    Editorial Team added an answer As Lance Richardson said, the first problem is that you… May 11, 2026 at 7:26 pm
  • Editorial Team
    Editorial Team added an answer You can add to the crontab as follows: #write out… May 11, 2026 at 7:26 pm

Related Questions

Given the following table in SQL Server 2005: ID Col1 Col2 Col3 -- ----
I have the following Query and i need the query to fetch data from
How can I monitor an SQL Server database for changes to a table without
I am trying to create an ANI lookup table from 2 separate tables, one
I have a table which contains around 400 000 records and which gets called

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.