我的实际表格:
--------------------------------------------------
Col1 | Col2 | Col3 |
--------------------------------------------------
1 A 1
6 F 2
3 C 4
2 B 1
5 E 3
4 D 2
预期结果:
------------------
|Columns Should Be|
------------------
A B F D C E
我需要对这个分层逻辑进行SQL查询。我必须使用col 1和col3获得结果逻辑.Kindly帮我解决这个问题。
答案 0 :(得分:0)
我刚看了你的回复,我相信这就是你所追求的;
CREATE TABLE #TempData (Col1 int, Col2 varchar(1), Col3 int)
INSERT INTO #TempData (Col1, Col2, Col3)
VALUES
('1','A','1')
,('6','F','4')
,('6','C','2')
,('2','B','1')
,('5','E','3')
,('4','D','5')
SELECT
Col2
FROM #TempData
GROUP BY Col2
ORDER BY SUM(Col1)+SUM(Col3)
DROP TABLE #TempData
答案 1 :(得分:0)
declare @table table (col1 int, col2 varchar(1), col3 int)
insert into @table (col1,col3)
values
(1, 1),
(6, 4),
(3, 2),
(2, 1),
(5, 3),
(4, 2)
/*
Col1 | Col2 | Col3 |
--------------------------------------------------
1 A 1
6 F 4
3 C 2
2 B 1
5 E 3
4 D 5
*/
select col1,
case
when col1 > col3 then char(col1 + 64)
else char(col3 + 64)
end as col2,
col3
from @table
答案 2 :(得分:0)
在Sql server中你可以试试这个,
declare @t table (Col1 int, Col2 varchar(1), Col3 int)
INSERT INTO @t (Col1, Col2, Col3)
VALUES
('1','A','1')
,('6','F','2')
,('3','C','4')
,('2','B','1')
,('5','E','3')
,('4','D','2')
declare @str as varchar(100)=''
;with t as(select top 1 col1,col2,col3 from @t union all select t1.col1, t1.col2,t1.col3 from @t t1,t
where (t1.Col1!=t.Col1 and t1.Col3=t.Col1) )
select @str=@str+' '+col2 from t
select @str as col