SQL返回每个参数销售的10个产品通过的日期

时间:2014-06-25 13:25:41

标签: sql sql-server-2008 select

您好我需要知道何时(日期)10(第十)项已售出,每个参数传递ID项, 这是销售表:

Sales table and Sales Line table

事先提出

1 个答案:

答案 0 :(得分:1)

请尝试以下(我相信*我使用了正确的列):

// This example finds the date that the 10th occurrence of Part #1001 was sold.

// @idArtigo is the placeholder for your incoming parameter
DECLARE @idArtigo int;
SET @idArtigo = 1001;

WITH Artigos AS (
    SELECT
        Row_Number() OVER(ORDER BY v.[DataHora] ASC, lv.[IdLinhaVenda] ASC) AS RowNumber,
        v.[DataHora],
        v.[IdVenda],
        lv.[IdLinhaVenda]
    FROM
        [Vendas] AS v
        INNER JOIN [LinhasVenda] AS lv
            ON ( v.[IdVenda] = lv.[IdVenda] )
    WHERE
        lv.[IdArtigo] = @idArtigo
)
SELECT
    [DataHora],
    [IdVenda],
    [IdLinhaVenda]
FROM
    Artigos
WHERE
    RowNumber = 10;

*注意:我不懂葡萄牙语(除了谷歌翻译),所以我做了一些有关使用哪些列的有根据的猜测(&假设)。