1

我有下表,每一行都有逗号分隔的值:

ID
-----------------------------------------------------------------------------
10031,10042
10064,10023,10060,10065,10003,10011,10009,10012,10027,10004,10037,10039
10009
20011,10027,10032,10063,10023,10033,20060,10012,10020,10031,10011,20036,10041

我需要为每个ID(分组)计数。

我只是想避免游标的实现,并为如何在没有游标的情况下做到这一点感到困惑。

任何帮助,将不胜感激 !

4

3 回答 3

6

您将需要使用拆分功能:

create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))       
returns @temptable TABLE (items varchar(MAX))       
as       
begin      
    declare @idx int       
    declare @slice varchar(8000)       

    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;

然后您可以通过以下方式查询数据:

select items, count(items)
from table1 t1
cross apply dbo.split(t1.id, ',')
group by items

请参阅带有演示的 SQL Fiddle

于 2012-10-16T17:21:51.043 回答
0

在重申上面关于不要将多个值放入单个列的评论之后(使用单独的子表,每行一个值!),

不过,一种可能的方法是:使用 UDF 将分隔字符串转换为表格。将所有值转换为表格后,将所有表格合并到一个表格中,然后对该表格进行分组。

Create Function dbo.ParseTextString (@S Text, @delim VarChar(5))
Returns @tOut Table 
(ValNum Integer Identity Primary Key, 
 sVal VarChar(8000))
As
Begin 
Declare @dlLen TinyInt      -- Length of delimiter
Declare @wind VarChar(8000) -- Will Contain Window into text string
Declare @winLen  Integer    -- Length of Window
Declare @isLastWin TinyInt  -- Boolean to indicate processing Last Window
Declare @wPos  Integer      -- Start Position of Window within Text String
Declare @roVal VarChar(8000)-- String Data to insert into output Table
Declare @BtchSiz Integer    -- Maximum Size of Window
Set @BtchSiz = 7900     -- (Reset to smaller values to test routine)
Declare @dlPos Integer      -- Position within Window of next Delimiter
Declare @Strt Integer       -- Start Position of each data value within Window
-- -------------------------------------------------------------------------

-- ---------------------------
If @delim is Null Set @delim = '|'
If DataLength(@S) = 0 Or
    Substring(@S, 1, @BtchSiz) = @delim Return
-- --------------------------------------------
Select @dlLen = DataLength(@delim),
        @Strt = 1, @wPos = 1,
        @wind = Substring(@S, 1, @BtchSiz)
Select @winLen = DataLength(@wind),
    @isLastWin = Case When DataLength(@wind) = @BtchSiz
                      Then 0 Else 1 End,
        @dlPos = CharIndex(@delim, @wind, @Strt)
-- --------------------------------------------
While @Strt <= @winLen
    Begin
        If @dlPos = 0 Begin    -- No More delimiters in window
            If @isLastWin = 1 Set @dlPos = @winLen + 1 
            Else Begin
                Set @wPos = @wPos + @Strt - 1
                Set @wind = Substring(@S, @wPos, @BtchSiz)
                -- ----------------------------------------
                Select @winLen = DataLength(@wind), @Strt = 1,
                       @isLastWin = Case When DataLength(@wind) = @BtchSiz
                                Then 0 Else 1 End,
                       @dlPos = CharIndex(@delim, @wind, 1)
                If @dlPos = 0 Set @dlPos = @winLen + 1 
            End
        End
        -- -------------------------------
        Insert @tOut (sVal) 
        Select LTrim(Substring(@wind, 
                @Strt, @dlPos - @Strt))
        -- -------------------------------
        -- Move @Strt to char after last delimiter
        Set @Strt = @dlPos + @dlLen 
        Set @dlPos = CharIndex(@delim, @wind, @Strt)
    End
Return
End

然后写,(使用你的表模式),

 Declare @AllVals VarChar(8000)
 Select @AllVals = Coalesce(@allVals + ',', '') + ID 
 From Table Where ID Is Not null
 -- -----------------------------------------
 Select sVal, Count(*)
 From dbo.ParseTextString(@AllVals, ',')
 Group By sval
于 2012-10-16T17:19:43.390 回答
0

好吧,我一直使用的解决方案,可能还有更好的方法,就是使用一个可以拆分所有内容的函数。没有使用游标,只是一个while循环。

if OBJECT_ID('splitValueByDelimiter') is not null
begin
  drop function splitValueByDelimiter
end
go
create function splitValueByDelimiter (
  @inputValue varchar(max)
, @delimiter varchar(1)
)       
returns @results table (value varchar(max))       
as       
begin       

declare @delimeterIndex int       
      , @tempValue varchar(max)

set @delimeterIndex = 1
while @delimeterIndex > 0  and len(isnull(@inputValue, '')) > 0    
begin       
  set @delimeterIndex = charindex(@delimiter, @inputValue)       
  if @delimeterIndex > 0       
    set @tempValue = left(@inputValue, @delimeterIndex - 1)       
  else       
      set @tempValue = @inputValue       

  if(len(@tempValue)>0)  
  begin
    insert 
    into    @results 
    select  @tempValue
  end

  set @inputValue = right(@inputValue, len(@inputValue) - @delimeterIndex)       

end   

return       
end  

之后,您可以像这样调用输出:

if object_id('test') is not null
begin
  drop table test
end
go
create table test (
  Id varchar(max)
)

insert 
into test
          select '10031,10042'
union all select     '10064,10023,10060,10065,10003,10011,10009,10012,10027,10004,10037,10039'
union all select '10009'
union all select     '20011,10027,10032,10063,10023,10033,20060,10012,10020,10031,10011,20036,10041'

select  value
from    test
        cross apply splitValueByDelimiter(Id, ',')

希望它有所帮助,尽管我仍在遍历所有内容

于 2012-10-16T17:26:53.947 回答