如何仅查询将超过最大数量的行

时间:2016-03-04 06:48:32

标签: sql sql-server sql-server-2008

所以这是表A

   +------------+--------+----------+
   | LineNumber | Pallet | Location |
   +------------+--------+----------+
   | 1          | a      | X        |
   +------------+--------+----------+
   | 2          | a      | X        |
   +------------+--------+----------+
   | 3          | b      | Y        |
   +------------+--------+----------+
   | 4          | b      | Y        |
   +------------+--------+----------+
   | 5          | b      | Y        |
   +------------+--------+----------+
   | 6          | c      | Z        |
   +------------+--------+----------+
   | 7          | c      | Z        |
   +------------+--------+----------+
   | 8          | c      | Z        |
   +------------+--------+----------+
   | 9          | d      | Q        |
   +------------+--------+----------+
   | 10         | d      | Q        |
   +------------+--------+----------+

这是表b

   +-------------+----------+
   | MaxPalCount | Location |
   +-------------+----------+
   | 2           | X        |
   +-------------+----------+
   | 2           | Y        |
   +-------------+----------+
   | 2           | Z        |
   +-------------+----------+
   | 2           | Q        |
   +-------------+----------+

我想要展示的是每个位置超过maxpalcount的亚麻布数。因此,例如,第3 4 5行将转到位置Y,但正如您所看到的,该位置的最大平均数仅为2.同样如第6 7 7行所示。

所以我想要的查询结果就是这样。

   +------------+--------+----------+
   | LineNumber | Pallet | Location |
   +------------+--------+----------+
   | 5          | b      | Y        |
   +------------+--------+----------+
   | 8          | c      | Z        |
   +------------+--------+----------+

将显示将超出的最后一行。我知道可以通过每行获取来执行此操作。但是不使用任何循环方法是否可能?

3 个答案:

答案 0 :(得分:5)

DECLARE @Test1 TABLE (
    LineNumber int,
    Pallet nvarchar(10),
    Location nvarchar(10)
)

DECLARE @Tsst2 TABLE (
    MaxPalCount int,
    Location nvarchar(10)
)

INSERT INTO @Test1
(LineNumber,Pallet,Location)
VALUES
(1, 'a', 'X'),
(2, 'a', 'X'),
(3, 'b', 'Y'),
(4, 'b', 'Y'),
(5, 'b', 'Y'),
(6, 'c', 'Z'),
(7, 'c', 'Z'),
(8, 'c', 'Z'),
(9, 'd', 'Q'),
(10, 'd', 'Q')

INSERT INTO @Tsst2
(MaxPalCount, Location)
VALUES
(2, 'X'),
(2, 'Y'),
(2, 'Z'),
(2, 'Q')


SELECT *
FROM
(
    SELECT 
        *,
        ROW_NUMBER()OVER (PARTITION BY t.Pallet ORDER BY t.LineNumber) AS RowId
    FROM @Test1 t
) AS t
INNER JOIN @Tsst2 t2 ON t.Location = t2.Location
WHERE t.RowId > t2.MaxPalCount

答案 1 :(得分:1)

我的虚拟数据与@Backs相同。

我认为它应该在Location上进行分区。

我使用exists子句而不是内连接。

;WITH Cte
AS (
    SELECT LineNumber
        ,Pallet
        ,Location
        ,ROW_NUMBER() OVER (
            PARTITION BY t.Location ORDER BY t.LineNumber
            ) AS RowId
    FROM @Test1 t
    )
SELECT *
FROM CTE C
WHERE EXISTS (
        SELECT MaxPalCount
        FROM @Tsst2 t1
        WHERE t1.location = c.Location
            AND c.rowid > t1.MaxPalCount
        )

答案 2 :(得分:0)

与@Backs相同的数据

;WITH cte AS 
(
    SELECT 
        *,
        ROW_NUMBER()OVER (PARTITION BY t.Location ORDER BY t.LineNumber) AS RowId
    FROM @Test1 t
)
SELECT t.LineNumber, t.Pallet, t.Location FROM cte t
INNER JOIN @Tsst2 t2 ON t.Location = t2.Location
WHERE t.RowId = t2.MaxPalCount + 1