替代此游标

时间:2014-11-25 16:48:14

标签: sql-server group-by sql-order-by

  Id       groupId     LangId     Title         Category
------    --------- ---------  -------------  -----------
4230        4230        1       aaamrmtwna     srhrtiewuv
14230       4230        2       uphliibxil     ppanvtqwht
24230       4230        3       neutnlvgkt     xbhwwyjqnm
6023        6023        1       aacnjohrjk     ehmjptsgxd
16023       6023        2       mejrzfhgjv     lhieeslqgf
26023       6023        3       pibswsvfxh     pzzhgjtbyt
3338        3338        1       aaeidhaivc     pxbolmwjan
13338       3338        2       lsbeyvtrch     pdtyrcwkdd
23338       3338        3       huewmezblx     kpjzpfryki
5573        5573        1       aafqhvgltp     xzwmguqfro
15573       5573        2       clzywmldud     hdylrqtcka
25573       5573        3       rwkdahwpdq     syhysvkftx
9896        9896        1       aaiaxqdwho     mxdgtdaeqx
19896       9896        2       onufsjeaal     cjrsotvbqt
29896       9896        3       fowufxxujk     xztkjbctru
9040        9040        1       aajjamlqkf     wattqxerjh
19040       9040        2       nrkdzgourq     rmckjjpvfu
29040       9040        3       winkynkdkf     zhfmkqsyvc
5518        5518        1       aamdwlwwgl     kdzsxuzfdp
15518       5518        2       byzzhqqfsr     cdmxmcxyof
25518       5518        3       ylkffbmief     qildnvyqhi
5043        5043        1       aamqnboinl     awtmdhbiuq
15043       5043        2       nachemovnv     rdzlbxqkdv
25043       5043        3       twxyozpzra     hskmwnsbjc
501         501         1       aansttgfjk     bmirdqmpgb

我写了一个游标来获取此输出(我已经插入了随机字符串数据进行测试)。但是如果表中游标的行数超过1k,那么性能变得如此之慢。我需要编写查询备用此游标

这是我的光标:

 DECLARE @Id int
 DECLARE @groupId int
 DECLARE @LangId int
 DECLARE @Title nvarchar(50)
 DECLARE @Category nvarchar(50) 
 DECLARE @i int
 DECLARE @Result TABLE
 (
 Id int,
 groupId int,
 LangId int,
 Title nvarchar(50),
 Category nvarchar(50) 
 )
 DECLARE csr_group CURSOR FORWARD_ONLY FOR

 SELECT TOP 100 * FROM SortThis order by LangID, Title

 OPEN csr_group

 FETCH NEXT FROM csr_group INTO @Id , @groupId , @LangId , @Title , @Category 
 WHILE @@FETCH_STATUS =0         
    BEGIN 
    set @i = (select Id from @Result where Id = @Id)
        if @i is null
        begin
            INSERT INTO @Result
               (
                [ID]
               ,[GroupId]
               ,[LangId]
               ,[Title]
               ,[Category]
               )
               Select * from SortThis where GroupId = @groupId 
        FETCH NEXT FROM csr_group INTO @Id , @groupId , @LangId , @Title , @Category 
        end
    END 
CLOSE csr_group
DEALLOCATE csr_group

select * from @Result 
order by (Select 0)
OFFSET ((1 - 1) * 25) ROWS 
FETCH NEXT 25 ROWS ONLY;

当我对表格或类别进行排序时,该产品的每种语言都应该在subbuttom中看到并按langId排序。(像这样:http://i.imgur.com/QBffjBX.png

我没有找到类似于我的问题的例子。请帮助。

1 个答案:

答案 0 :(得分:0)

如果有兴趣,我找到了答案。

SELECT
Id, GroupId, LangId, Title, Category FROM
SortThis s2 Order BY (

SELECT TOP 1 s1.Category FROM SortThis s1 WHERE s1.LangId=(
SELECT MIN(LangId) FROM SortThis s3 WHERE s1.GroupId=s3.GroupId
) AND s1.GroupId=s2.GroupId
) , LangId

OFFSET (1-1) * 25 ROWS
FETCH NEXT 25 ROWS ONLY