选择使用过程

时间:2019-02-05 16:23:54

标签: sql-server tsql stored-procedures

我正在使用一个参数为@TOP和@Bottom的过程,这意味着当我在@TOP中输入例如50时,sql必须带给我50大销售额

ALTER PROCEDURE [dbo].[SELL] @TOP int, @BOTTOM int AS

SELECT E_REGION, E_LIBELLE, SUM(E_MONTANTTTC) as TOTAL VENTE,

FROM SALE

GROUP BY    
    e_region, e_LIBELLE

     ORDER BY  e_region, e_MONTANTTTC desc

 EXEC [SELL] @TOP='', @BOTTOM='';

在参数@TOP中输入N时,如何获得N个最高销售额。 例如:时间:@ top = 20 然后查询返回前20个销售额(e_MONTANTTTC)

2 个答案:

答案 0 :(得分:0)

在sql server中,您可以使用offset提取

#!/usr/bin/env python3

import pyvips
from glob import glob
from pathlib import Path
from multiprocessing import Pool

def doOne(f):
   img = pyvips.Image.new_from_file(f, access='sequential')
   webpname = Path(f).stem + ".webp"
   img.write_to_file(webpname)

if __name__ == '__main__':
    files = glob("*.jpg")
    with Pool(12) as pool:
        pool.map(doOne, files)

偏移量是当您开始并获取下一个寄存器号时的值,例如,如果您想获取前50个,则偏移量为0,而下一个获取的寄存器号为50,如示例所示。

答案 1 :(得分:0)

在注释中进行对话之后,可以使用条件排序和top完成查找的内容。我已经修改了您当前的步骤,并在其中添加了注释,以使其(希望)易于理解我所做的事情:

ALTER PROCEDURE [dbo].[SELL] 
(
    @TOP int, 
    @BOTTOM int 
)
AS
-- if both top and bottom have values, do nothing
IF @TOP IS NULL OR @BOTTOM IS NULL
BEGIN
    -- ABS: if @top or @bottom have a negative value, handle it as if it was positive
    -- COELASCE: if both @top and @bottom are null, get 0 rows back.
    SELECT TOP (ABS(COELASCE(@TOP, @BOTTOM, 0))) E_REGION, E_LIBELLE, SUM(E_MONTANTTTC) as TOTAL VENTE
    FROM SALE
    GROUP BY e_region, e_LIBELLE
    -- IIF: returns the second argument if the condition in the first argument is true, else the third argument
    ORDER BY IIF(@TOP IS NOT NULL, e_region, NULL), 
             IIF(@BOTTOM NOT IS NULL, e_region, NULL) DESC,
             IIF(@TOP IS NOT NULL, e_MONTANTTTC, NULL) DESC, 
             IIF(@BOTTOM IS NOT NULL, e_MONTANTTTC, NULL) 
END

但是,我认为不使用互斥的@top@bottom参数会更有意义,但是它使用一个参数来设置返回的行数,而使用一个参数来确定返回的行数。排序顺序-因此,我也为您编写了此版本(基本上,与第一个版本相比,改动很小,并删除了注释):

ALTER PROCEDURE [dbo].[SELL] 
(
    @COUNT int, 
    @ASC bit 
)
AS

SELECT TOP (ABS(ISNULL(@COUNT, 0))) E_REGION, E_LIBELLE, SUM(E_MONTANTTTC) as TOTAL VENTE
FROM SALE
GROUP BY e_region, e_LIBELLE
ORDER BY IIF(@ASC = 1, e_region, NULL), 
         IIF(ISNULL(@ASC, 0) = 0, e_region, NULL) DESC,
         IIF(@ASC = 1, e_MONTANTTTC, NULL) DESC, 
         IIF(ISNULL(@ASC, 0) = 0, e_MONTANTTTC, NULL)