SQL获取前X行数 [英] SQL geting Top X number of rows
问题描述
我只想获取查询的前 x 个数字.x 将通过参数发送到哪里我该怎么做?
i want to get only the top x number of the query. where the x will be send by parameter how can i do this?
我知道我可以把它当作字符串然后再执行,但我觉得它很麻烦.
I know i can take it as string and exec it later but i find it very cumbersome.
这是我的查询,@ArticleNo 是我想作为 x 的参数.
This the query i have and @ArticleNo is the parameter i want to take as 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
推荐答案
使用:
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
查询之前添加以下内容:
Alternatively, add the following before your SELECT
query:
IF @ArticleNo IS NOT NULL
BEGIN
SET ROWCOUNT @ArticleNo
END
然后在您的 SELECT
查询之后,您需要通过执行以下操作来重置 ROWCOUNT
:
Then after your SELECT
query you need to reset the ROWCOUNT
by doing:
IF @ArticleNo IS NOT NULL
BEGIN
SET ROWCOUNT 0
END
<小时>
因此,总体而言,它将类似于:
Therefore, overall it will be something like:
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
并不理想,因为它会干扰您的子查询结果.
However using ROWCOUNT
is not ideal as it will mess with your sub-query results.
这篇关于SQL获取前X行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!