Solved

Lowest Data Value within a sql row

Posted on 2014-07-21
2
173 Views
Last Modified: 2014-07-21
I have a table defined as

OfficeLocID int
DistanceHeadquarters int
DistanceOffice1 int
DistanceOffice2 int
DistanceOffice3 int
DistanceOffice4 int
DistanceOffice5 int

I need to write a query that for each row, will return which field has the lowest value and what field it is

(Note: No 2 distances would have same value)

For example if the data was

OfficeLocID DistanceHeadquarters DistanceOffice1 DistanceOffice2 DistancetOffice3 DistanceOffice4 DistanceOffice5
1                       23                                      30                            40                       50                              20                    32
2                       12                                      50                            40                       51                              20                    32

I would want results to be

OfficeLocID     ClosestLocation                             ClosestLocationDistance
1                        DistanceOffice4                                        20  
2                        DistanceHeadquarters                           12
0
Comment
Question by:johnnyg123
2 Comments
 
LVL 6

Accepted Solution

by:
ksrsrinivasan earned 500 total points
Comment Utility
One possible solution is to unpivot and select the minimum distance and do a lookup

DECLARE @tbl TABLE
(
    OfficeLocID int,
    DistanceHeadquarters int,
    DistanceOffice1 int,
    DistanceOffice2 int,
    DistanceOffice3 int,
    DistanceOffice4 int,
    DistanceOffice5 int
)

INSERT INTO @tbl
(
    OfficeLocID,
    DistanceHeadquarters,
    DistanceOffice1,
    DistanceOffice2,
    DistanceOffice3,
    DistanceOffice4,
    DistanceOffice5
)
VALUES
(
    1,
    23,
    30,
    40,
    50,
    20,
    32
),
(
    2,
    12,
    50,
    40,
    51,
    20,
    32
)

SELECT ref.OfficeLocID, MIN(un.value) [ClosestLocationDistance],
CASE MIN(un.value)
    WHEN DistanceHeadquarters THEN N'DistanceHeadquarters'
    WHEN DistanceOffice1 THEN N'DistanceOffice1'
    WHEN DistanceOffice2 THEN N'DistanceOffice2'
    WHEN DistanceOffice3 THEN N'DistanceOffice3'
    WHEN DistanceOffice4 THEN N'DistanceOffice4'
    WHEN DistanceOffice5 THEN N'DistanceOffice5'
END AS [ClosestLocation]
FROM @tbl unpivot(value for DistanceDesc in (DistanceHeadquarters,
    DistanceOffice1,
    DistanceOffice2,
    DistanceOffice3,
    DistanceOffice4,
    DistanceOffice5)) un
INNER JOIN
    @tbl ref
ON
    ref.OfficeLocID = un.OfficeLocID
GROUP BY
    ref.OfficeLocID,
    ref.DistanceHeadquarters,
    ref.DistanceOffice1,
    ref.DistanceOffice2,
    ref.DistanceOffice3,
    ref.DistanceOffice4,
    ref.DistanceOffice5

Open in new window

0
 

Author Closing Comment

by:johnnyg123
Comment Utility
Excellent!!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now