我有一张包含以下示例数据的表格。
col1 col2 col3
4 6 9
7 1 5
我希望能够获取具有最大值匹配的列的索引,如果它们相等,则忽略后面的列。
例如,结果应该返回:
3 (because col3 has maximum value 9)
1 (because col1 has maximum value 7)
请注意,列数是未定义的,因此我需要一个通用解决方案。 谢谢。
我有一张包含以下示例数据的表格。
col1 col2 col3
4 6 9
7 1 5
3 (because col3 has maximum value 9)
1 (because col1 has maximum value 7)
select case
when col1 >= col2 and col1 >= col3 then 1
when col2 >= col1 and col2 >= col3 then 2
when col3 >= col1 and col3 >= col2 then 3
end as ColIndex
from table
when
,可以写成 else
:) - Rahul TripathiIIF
:) - ughaiWITH MyTableWithRowId AS
(
SELECT newId() AS Id, *
FROM MyTable
),
Unpivoted AS
(
SELECT Ndx, Id, col, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY col DESC) AS Rnk
FROM
MyTableWithRowId tbl
UNPIVOT
(
col for Ndx in(col1, col2, col3)
) p
)
SELECT Ndx
FROM Unpivoted
WHERE Rnk = 1
编辑,只需使用“1、2、3”而不是列名(col1、col2、col3)
根据 @Giorgi 的评论,如果您真的想要每行中列的基于一的顺序位置,可以加入到 DMV 中,例如 INFORMATION_SCHEMA.COLUMNS
来查找顺序,但我认为这将是非常脆弱的策略。
WITH MyTableWithRowId AS
(
SELECT newId() AS Id, col1, col2, col3
FROM MyTable
),
TheOrdinalPositionOfColumns AS
(
SELECT COLUMN_NAME, ORDINAL_POSITION
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'MyTable'
),
Unpivoted AS
(
SELECT Ndx, Id, col, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY col DESC) AS Rnk
FROM
MyTableWithRowId tbl
UNPIVOT
(
col for Ndx in(col1, col2, col3)
) p
)
SELECT topoc.ORDINAL_POSITION AS ColumnOrdinalPosition
FROM Unpivoted
JOIN TheOrdinalPositionOfColumns topoc ON Unpivoted.Ndx = topoc.COLUMN_NAME
WHERE Rnk = 1;
SomeCol OtherCol WhatCol
,则此方法无效。http://sqlfiddle.com/#!6/4b7bc/1 - Giorgi Nakeuriselect
case when col1 >= col2 and col1 >= col3 then 1
when col2 >= col1 and col2 >= col3 then 2
else 3 end as [index]
from myquestion_table
查看一个 演示在这里
如果它们相等,只需忽略后一个
,您应该将>
更改为> =
以选择第一列。 - Giorgi Nakeuri尝试这个,不用枢轴。
-- 你可以添加任意数量的列。
CREATE TABLE Table1
([id] int primary key identity(1,1),[col1] int, [col2] int, [col3] int)
;
INSERT INTO Table1
([col1], [col2], [col3])
VALUES
(4, 6, 9),
(7, 1, 5)
;
DECLARE @tempTable as table(name varchar(50),maxValue int)
DECLARE @maxColumn int
SELECT @maxColumn = max(ordinal_position)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'Table1'
DECLARE @maxRow int
SELECT @maxRow = Count(col1) FROM Table1
DECLARE @rowCounter int = 1
DECLARE @colCounter int = 1
DECLARE @columnName varchar(max)
DECLARE @colValue varchar(max)
DECLARE @q nvarchar(max)
DECLARE @maxValue int
DECLARE @ParmDefinition nvarchar(500)
DECLARE @FinalResult table (id int, columnName nvarchar(max))
DECLARE @rowId int
WHILE(@rowCounter <= @maxRow)
BEGIN
WHILE (@colCounter <= @maxColumn)
BEGIN
SELECT @columnName = COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'Table1' and ordinal_position = @colCounter
--select @columnName,@rowCounter,@colCounter
SELECT @q = 'select @retvalOUT =' + @columnName + ' from Table1 where id = ' + cast(@rowCounter as NVARCHAR)
SET @ParmDefinition = N'@retvalOUT int OUTPUT';
EXEC sp_executesql @q,@ParmDefinition ,@retvalOUT = @maxValue OUT
--select '@maxValue' + @maxValue
INSERT INTO @tempTable VALUES (@columnName,@maxValue)
SET @colCounter = @colCounter + 1
END
SELECT @rowId = maxValue FROM @tempTable WHERE name LIKE 'id' -- Primary key column
INSERT INTO @FinalResult(id,columnName)
SELECT TOP 1 @id,name FROM @tempTable WHERE name not like 'id' ORDER BY maxvalue DESC
DELETE FROM @tempTable
--select * from @FinalResult
SET @colCounter = 1
SET @rowCounter = @rowCounter + 1
END
SELECT * FROM @FinalResult
这里有另一种数据透视解决方案,比其他数据透视解决方案稍微简洁一些:
DECLARE @t table
(col1 int, col2 int, col3 int)
INSERT @t
SELECT 4,8,9 union all SELECT 7,1,5
;WITH addrownumber AS
(
SELECT
rn = row_number() over (order by (select 1)),
*
FROM @t
)
, unpiv AS
(
SELECT
rn,
value,
colname,
ordinalposition = row_number() over (partition by rn order by (select 1)),
rn2 = row_number() over (partition by rn order by value DESC, rn)
FROM addrownumber as p
UNPIVOT
(value FOR colname IN
([col1], [col2], [col3])) AS unpvt
-- since you need all columns to be mentioned in pivot, you can set up
-- the ordinal order here, by putting in columns in the right order.
)
SELECT ordinalposition, value, colname
FROM unpiv
WHERE rn2 = 1
结果:
ordinalposition value colname
3 9 col3
1 7 col1
试试这个:
select case
when col1 >= col2 and col1 >= col3 then 1
when col2 >= col1 and col2 >= col3 then 2
else 3
end as ind
from mytable
这只是一个非常基本的例子,但它可能是这样的:
select case when col1 > col2 and col1 > col3 then col1
when col2> col3 then col2
else col3 end as greatestColumn
from table