【问题标题】:SQL geting Top X number of rowsSQL获取前X行
【发布时间】:2012-02-23 09:22:34
【问题描述】:

我只想获取查询的前 x 个数字。 x 将通过参数发送到哪里我该怎么做?

我知道我可以把它当作字符串稍后执行,但我觉得它很麻烦。

这是我的查询,@ArticleNo 是我想要作为 x 的参数。

Create proc [dbo].[GW2_Report_SlowFastMovingArticle]    

@ArtKey bigint = null,
@ArtCatKey int= null,
@ArtGroupKey int= null,
@ArtTypeKey int= null,
@MaterialKey int= null,
@ColorKey int= null,
@VendorTypeKey int = null,
@VendorKey bigint = null,
@FromDate datetime = null,
@ToDate datetime = null,
@MovingType int = 0,
@PerformanceType int = 0,
@ArticleNo int = 10,
@OutletKey int = null
AS    
BEGIN  

SELECT 
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode, 
dbo.ArtWithVendor.ArtName,
Sum(isnull(dbo.Sal_POSDet.Qty,0))as Pair,
Sum(isnull(dbo.Sal_POSDet.Total,0)) as TurnOver
into #temp FROM 
dbo.Sal_POS INNER JOIN
dbo.Sal_POSDet ON 
dbo.Sal_POS.SalesKey = dbo.Sal_POSDet.SalesKey INNER JOIN
dbo.ArtWithVendor ON 
dbo.Sal_POSDet.ArtKey = dbo.ArtWithVendor.ArtKey

WHERE

Sal_POS.IsHold=0 and
Sal_POS.SalesDate between @FromDate  and @ToDate  and
        CASE WHEN  @ArtKey  is null THEN 1 WHEN ArtWithVendor.ArtKey =@ArtKey THEN 1 ELSE 0 END = 1   
        and CASE WHEN @ArtCatKey  is null THEN 1 WHEN ArtWithVendor.ArtCatKey =@ArtCatKey THEN 1 ELSE 0 END = 1     
        and CASE WHEN @ArtGroupKey is null THEN 1 WHEN ArtWithVendor.ArtGroupKey = @ArtGroupKey THEN 1 ELSE 0 END = 1     
        and CASE WHEN @ArtTypeKey  is null  THEN 1 WHEN ArtWithVendor.ArtTypeKey = @ArtTypeKey THEN 1 ELSE 0 END = 1     
        and CASE WHEN @MaterialKey is null THEN 1 WHEN ArtWithVendor.MaterialKey = @MaterialKey  THEN 1 ELSE 0 END = 1     
        and CASE WHEN @ColorKey  is null THEN 1 WHEN ArtWithVendor.ColorKey = @ColorKey  THEN 1 ELSE 0 END = 1     
        and CASE WHEN @VendorKey  is null  THEN 1 WHEN ArtWithVendor.VendorKey = @VendorKey THEN 1 ELSE 0 END = 1     
        and CASE WHEN @VendorTypeKey  is null  THEN 1 WHEN ArtWithVendor.VendorTypeKey = @VendorTypeKey THEN 1 ELSE 0 END = 1     
        and CASE WHEN @OutLetKey  is null  THEN 1 WHEN Sal_POS.OutLetKey = @OutLetKey THEN 1 ELSE 0 END = 1     

Group by 

dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode, 
dbo.ArtWithVendor.ArtName



if(@PerformanceType=0 and @MovingType=0)
begin
select * from #temp 
order by Pair asc
end
if(@PerformanceType=0 and @MovingType=1)
begin
select  * from #temp 
order by Pair desc
end
if(@PerformanceType=1 and @MovingType=0)
begin
select * from #temp 
order by turnover asc
end
if(@PerformanceType=1 and @MovingType=1)
begin
select * from #temp 
order by turnover desc
end

END

【问题讨论】:

标签: sql rows


【解决方案1】:

试试这个...

select TOP(@ArticleNo) * 
from #temp 

【讨论】:

    【解决方案2】:

    用途:

    SELECT TOP(@ArticleNo)
    

    因此:

    SELECT TOP(@ArticleNo)
        dbo.Sal_POSDet.ArtKey,
        dbo.ArtWithVendor.ArtCode, 
        dbo.ArtWithVendor.ArtName,
        Sum(isnull(dbo.Sal_POSDet.Qty,0))as Pair,
        Sum(isnull(dbo.Sal_POSDet.Total,0)) as TurnOver
        into #temp FROM 
        dbo.Sal_POS INNER JOIN
        dbo.Sal_POSDet ON 
        dbo.Sal_POS.SalesKey = dbo.Sal_POSDet.SalesKey INNER JOIN
        dbo.ArtWithVendor ON 
        dbo.Sal_POSDet.ArtKey = dbo.ArtWithVendor.ArtKey
    
        WHERE
    
        Sal_POS.IsHold=0 and
        Sal_POS.SalesDate between @FromDate  and @ToDate  and
                CASE WHEN  @ArtKey  is null THEN 1 WHEN ArtWithVendor.ArtKey =@ArtKey THEN 1 ELSE 0 END = 1   
                and CASE WHEN @ArtCatKey  is null THEN 1 WHEN ArtWithVendor.ArtCatKey =@ArtCatKey THEN 1 ELSE 0 END = 1     
                and CASE WHEN @ArtGroupKey is null THEN 1 WHEN ArtWithVendor.ArtGroupKey = @ArtGroupKey THEN 1 ELSE 0 END = 1     
                and CASE WHEN @ArtTypeKey  is null  THEN 1 WHEN ArtWithVendor.ArtTypeKey = @ArtTypeKey THEN 1 ELSE 0 END = 1     
                and CASE WHEN @MaterialKey is null THEN 1 WHEN ArtWithVendor.MaterialKey = @MaterialKey  THEN 1 ELSE 0 END = 1     
                and CASE WHEN @ColorKey  is null THEN 1 WHEN ArtWithVendor.ColorKey = @ColorKey  THEN 1 ELSE 0 END = 1     
                and CASE WHEN @VendorKey  is null  THEN 1 WHEN ArtWithVendor.VendorKey = @VendorKey THEN 1 ELSE 0 END = 1     
                and CASE WHEN @VendorTypeKey  is null  THEN 1 WHEN ArtWithVendor.VendorTypeKey = @VendorTypeKey THEN 1 ELSE 0 END = 1     
                and CASE WHEN @OutLetKey  is null  THEN 1 WHEN Sal_POS.OutLetKey = @OutLetKey THEN 1 ELSE 0 END = 1     
    
        Group by 
    
        dbo.Sal_POSDet.ArtKey,
        dbo.ArtWithVendor.ArtCode, 
        dbo.ArtWithVendor.ArtName
    

    或者,在您的 SELECT 查询之前添加以下内容:

    IF @ArticleNo IS NOT NULL
    BEGIN
       SET ROWCOUNT @ArticleNo
    END
    

    然后在您的SELECT 查询之后,您需要通过以下方式重置ROWCOUNT

    IF @ArticleNo IS NOT NULL
     BEGIN
        SET ROWCOUNT 0
     END
    

    因此,总的来说,它会是这样的:

       IF @ArticleNo IS NOT NULL
        BEGIN
           SET ROWCOUNT @ArticleNo
        END
    
    
    SELECT 
    dbo.Sal_POSDet.ArtKey,
    dbo.ArtWithVendor.ArtCode, 
    dbo.ArtWithVendor.ArtName,
    Sum(isnull(dbo.Sal_POSDet.Qty,0))as Pair,
    Sum(isnull(dbo.Sal_POSDet.Total,0)) as TurnOver
    into #temp FROM 
    dbo.Sal_POS INNER JOIN
    dbo.Sal_POSDet ON 
    dbo.Sal_POS.SalesKey = dbo.Sal_POSDet.SalesKey INNER JOIN
    dbo.ArtWithVendor ON 
    dbo.Sal_POSDet.ArtKey = dbo.ArtWithVendor.ArtKey
    
    WHERE
    
    Sal_POS.IsHold=0 and
    Sal_POS.SalesDate between @FromDate  and @ToDate  and
            CASE WHEN  @ArtKey  is null THEN 1 WHEN ArtWithVendor.ArtKey =@ArtKey THEN 1 ELSE 0 END = 1   
            and CASE WHEN @ArtCatKey  is null THEN 1 WHEN ArtWithVendor.ArtCatKey =@ArtCatKey THEN 1 ELSE 0 END = 1     
            and CASE WHEN @ArtGroupKey is null THEN 1 WHEN ArtWithVendor.ArtGroupKey = @ArtGroupKey THEN 1 ELSE 0 END = 1     
            and CASE WHEN @ArtTypeKey  is null  THEN 1 WHEN ArtWithVendor.ArtTypeKey = @ArtTypeKey THEN 1 ELSE 0 END = 1     
            and CASE WHEN @MaterialKey is null THEN 1 WHEN ArtWithVendor.MaterialKey = @MaterialKey  THEN 1 ELSE 0 END = 1     
            and CASE WHEN @ColorKey  is null THEN 1 WHEN ArtWithVendor.ColorKey = @ColorKey  THEN 1 ELSE 0 END = 1     
            and CASE WHEN @VendorKey  is null  THEN 1 WHEN ArtWithVendor.VendorKey = @VendorKey THEN 1 ELSE 0 END = 1     
            and CASE WHEN @VendorTypeKey  is null  THEN 1 WHEN ArtWithVendor.VendorTypeKey = @VendorTypeKey THEN 1 ELSE 0 END = 1     
            and CASE WHEN @OutLetKey  is null  THEN 1 WHEN Sal_POS.OutLetKey = @OutLetKey THEN 1 ELSE 0 END = 1     
    
    Group by 
    
    dbo.Sal_POSDet.ArtKey,
    dbo.ArtWithVendor.ArtCode, 
    dbo.ArtWithVendor.ArtName
    
    IF @ArticleNo IS NOT NULL
     BEGIN
        SET ROWCOUNT 0
     END
    

    但是使用ROWCOUNT 并不理想,因为它会弄乱您的子查询结果。

    【讨论】:

    • 你误解我的事情我想查询返回一些顶行,其中一些数字通过我的查询@ArticleNo 中的参数发送
    • @Taufiq 我已修改我的答案以使用@ArticleNo 参数
    • -1 用于建议 ROWCOUNT。它会干扰您的中间查询和子查询结果
    • 干杯@gbn - 我从来没有想过这一点。在这种情况下,没有子查询,所以我认为可以吗?但显然它不能作为未来的证据。 SELECT TOP(@ArticleNo)SELECT TOP @ArticleNo 不同吗?过去我在第二个查询中遇到过错误。
    • @gbn 再次干杯,我从来不是ROWCOUNT的粉丝,我只是不知道TOP()!我会修改我的答案。
    猜你喜欢
    • 2013-06-09
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 2019-04-13
    • 1970-01-01
    相关资源
    最近更新 更多