テーブル Table1 に Col1 と Col2 の 2 つの列があります。Col1 の 2 行の値が "C,B" & "A,B" で、Col2 の 2 行の値が "ASC" & "DES" の場合、この順序で 2 行の出力が必要です」 ADES,B" & "CASC,B" (つまり、最初の値に基づいてソートする必要があります)。
Col1 の値はカンマで区切り、Col2 の値と組み合わせて、最初の値に基づいて出力をソートする必要があります。
次のように文字列分割関数を使用しています。
ALTER FUNCTION [dbo].[SplitString](@String nvarchar(4000), @Delimiter char(1))
returns @temptable TABLE (items nvarchar(4000))
as
begin
declare @idx int
declare @slice nvarchar(4000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
入力列のサンプル値:
Col1 Col2
C,B ASC
A,B DES
望ましい出力:
Output
ADES,B
CASC,B
誰もが望ましい出力を得るのを手伝ってください。