在SQL Server中对表的3列中的值进行排序

在SQL Server中对表的3列中的值进行排序,sql,sql-server-2008,tsql,Sql,Sql Server 2008,Tsql,在SQL Server 2008中,我有一个包含4列的表: Item_Code, Length, Width, Height 我需要将这个表转换为一种格式,其中长度是三个表中的最大维度,宽度是第二个,高度是第三个 例如: Item_Code | Length | Width | Height ----------+--------+-------+------- 123445 | 42.50 | 52.63 | 82.00 应该转变为 Item_Code | Length | Wid

在SQL Server 2008中,我有一个包含4列的表:

Item_Code, Length, Width, Height
我需要将这个表转换为一种格式,其中长度是三个表中的最大维度,宽度是第二个,高度是第三个

例如:

Item_Code | Length | Width | Height
----------+--------+-------+-------
123445    | 42.50  | 52.63 | 82.00
应该转变为

Item_Code | Length | Width | Height
----------+--------+-------+-------
123445    | 82.00  | 52.63 | 42.50

有人能帮我吗?这在SQL Server 2008中可能吗?

您可以尝试使用
CASE

SELECT 
  CASE 
    WHEN length >= width AND length >= height THEN length
    WHEN width >= length AND width >= height THEN width
    WHEN height >= length AND height >= width THEN height
  END AS [Length],
  CASE 
    WHEN (length >= width AND length <= height) OR (length <= width AND length >= height)  THEN length
    WHEN (width >= length AND width <= height) OR (width <= length AND width >= height)  THEN width
    WHEN (height >= length AND height <= width) OR (height <= length AND height >= width) THEN height
  END AS [Width],
  CASE 
    WHEN length <= width AND length <= height THEN length
    WHEN width <= length AND width <= height THEN width
    WHEN height <= length AND height <= width THEN height
END AS [Height]
FROM YourTable
选择
案例
当长度>=宽度和长度>=高度时,则为长度
当宽度>=长度和宽度>=高度,然后是宽度
当高度>=长度和高度>=宽度,然后是高度
结束为[长度],
案例

当(length>=width AND length=length AND width=length AND height时,@SQLChao这样的CASE语句是我的第一选择

-- Sample data
DECLARE @table TABLE(Item_Code int, Length decimal(6,2), Width decimal(6,2), Height decimal(6,2))
INSERT @table VALUES (123445,42.50,52.63,82.00),(123999,20.50,20.50,10.00),(123000,22.50,22.50,90.00),
                     (123444,22.50,90.00,90.00),(123555,90.00,90.00,90.00),(123222,100,90.00,90.00);

-- solution
SELECT 
  Item_Code,
  Length = fx.mx,
  Width  = CASE WHEN t.Width  = fx.mx THEN Length ELSE Width  END,
  Height = CASE WHEN t.Height = fx.mx THEN Length ELSE Height END
  FROM @table t
CROSS APPLY 
(
  SELECT MAX(x), MIN(x)
    FROM (VALUES (t.Length),(t.Width), (t.Height)) f(x)
) fx(mx,mn);

有点不同的方法

DECLARE @table TABLE(Item_Code int, Length decimal(6,2), Width decimal(6,2), Height decimal(6,2))
INSERT @table VALUES (123445,42.50,52.63,82.00),(123999,20.50,20.50,10.00),(123000,22.50,22.50,90.00),
                     (123444,22.50,90.00,90.00),(123555,90.00,90.00,90.00),(123222,100,90.00,90.00); 
--select * from @table;

with cte1 as 
(
    select Item_Code, Length as dimension from @table
    union all 
    select Item_Code, Width from @table
    union all 
    select Item_Code, Height from @table
)
, cte2 as 
(
   select * 
        , ROW_NUMBER() over (partition by Item_Code order by dimension desc) as rn
   from cte1
)
select l.Item_Code, l.dimension as Length, w.dimension as Width, h.dimension as height 
from cte2 as l 
join cte2 as w
  on w.Item_Code = l.Item_Code 
 and l.rn = 1 
 and w.rn = 2
join cte2 as h
  on h.Item_Code = l.Item_Code 
 and h.rn = 3 
order by Item_Code

我会使用
交叉应用

select t.item_code, v.*
from t cross apply
     (select max(case when seqnum = 1 then val end) as length,
             max(case when seqnum = 2 then val end) as width,
             max(case when seqnum = 3 then val end) as height
      from (select val, row_number() over (order by val desc) as seqnum
            from (values (t.length), (t.width), (t.height)) v(val)
           ) v
     ) v;

这很好,但是当你在最高值上有联系时,它会返回NULL。可能需要添加一些>=逻辑谢谢你的建议。我添加了
=
。我相信它应该会成功,因为如果它们相等,返回哪个值无关紧要。最好在宽度=长度时,宽度仍然为NULL,这是两个最高值。我在下面更新了我的答案,加入了几个不同的示例数据场景进行测试。项目代码是唯一的吗?