问题
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.
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
回答1:
Use:
SELECT TOP(@ArticleNo)
Therefore:
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
Alternatively, add the following before your SELECT
query:
IF @ArticleNo IS NOT NULL
BEGIN
SET ROWCOUNT @ArticleNo
END
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
However using ROWCOUNT
is not ideal as it will mess with your sub-query results.
回答2:
Try this...
select TOP(@ArticleNo) *
from #temp
来源:https://stackoverflow.com/questions/9410126/sql-geting-top-x-number-of-rows