我有一张包含样本数据的表格如下。
col1 col2 col3
4 6 9
7 1 5
我想得到列的索引,其值与该行的最大值匹配,如果它们相等,则忽略后者。
例如,结果应为return
3 (because col3 has maximum value 9)
1 (because col1 has maximum value 7)
请注意,列数未定义,因此我需要一般解决方案。
谢谢
答案 0 :(得分:4)
你可以这样做:
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
答案 1 :(得分:4)
对此更通用的解决方案(即N列)是将列展开为行,然后可以应用窗口函数以获得每组列“行”的组最大值。但是,您需要为每一行提供某种键,以便可以按行方式应用最大值(以允许重新组合原始行)。我通过Guid
添加代理newId()
来完成此操作。请注意,这会返回每行中具有最高值的NAME列:
WITH 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
来查看尽管这将是IMO非常脆弱的策略。
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;
答案 2 :(得分:3)
尝试这样的事情
select
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
答案 3 :(得分:2)
试试这个:
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
答案 4 :(得分:2)
这是一个非常基本的例子,但它可能是:
select case when col1 > col2 and col1 > col3 then col1
when col2> col3 then col2
else col3 end as greatestColumn
from table
答案 5 :(得分:2)
试试这个,没有支点。
- 您可以添加N个列。
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
答案 6 :(得分:2)
这是另一个透视解决方案,它比其他透视解决方案稍短:
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