Sql Server 2008和从具有不同RowCounts的不同表计数行

时间:2013-11-25 19:34:43

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

enter image description here美好的一天。有谁知道如何从不同的表中提取不同行数的数据?

我从3个表中调用,在其中一个表中,每个相应的ID都有多个行。我需要选择每个ID的前6个,但我加入的表有多个具有相同ID的行。我需要这些行。所以我需要调用前6名,但实际上它可能超过6行。我的示例代码如下。我发送给VB和 它希望看到6批数据,但每批次将有不同的行数。这是我的程序,谢谢。

(注意**我遇到的问题是当我运行这个Queuery时它给了我6行但是如果来自RPG的Roastprofile ID已经让我说3个相同那么我会得到6行但是有2个配置文件ID我需要限制PK_RoasterQueueSequenceID有6个不同ID的行或其他东西,但每个可能有多行。所以有超过6行,但它限制为6个IDS按日期排序....如果这样有道理。)

注意*它就像我想要从该表中重复,但我不希望它们计入我的行数。

    USE [Product];


GO
SET ANSI_NULLS ON;


GO
SET QUOTED_IDENTIFIER ON;


GO
ALTER PROCEDURE [Controls].[spTestGETBatchToPLC]
@SuperSackID INT OUTPUT, @POINTER1 INT
AS
SET NOCOUNT OFF;
DECLARE @PublishedStartDateTime AS SMALLDATETIME, 
@Fk_RoasterQueueSequenceID AS INT, 
@Pk_RoasterQueueSequenceID AS INT, 
@RETURN AS INT;
IF @POINTER1 = 1
    BEGIN
        SELECT   TOP 6 PK_RoasterQueueSequenceID,
                       RoasterNumber,
                       RoastProfile,
                       RoastDescription,
                       ChargeWeight
        FROM     wip.RoasterQueueSequence AS TRQS
                 INNER JOIN
                 wip.schedule AS s
                 ON s.fk_roasterqueuesequenceid = TRQS.PK_RoasterQueueSequenceID
        -- inner join Specification.RoastProfileGreenBeanComponents RPG on (****This is where there my or maynot be more than one row with the same RoastProfileID) RPG.PK_RoastProfileID = trqs.RoastProfile 
        WHERE    Trqs.Status = 'Queued'
                 AND Trqs.PK_RoasterQueueSequenceID = s.FK_RoasterQueueSequenceID
                 AND Trqs.RoasterNumber = 6
        ORDER BY s.publishedstartdatetime ASC;
        SELECT   PublishedStartDateTime,
                 FK_RoasterQueueSequenceID,
                 wip.RoasterQueueSequence.PK_RoasterQueueSequenceID,
                 wip.RoasterQueueSequence.RoasterNumber,
                 wip.RoasterQueueSequence.RoastProfile,
                 wip.RoasterQueueSequence.RoastDescription,
                 wip.RoasterQueueSequence.ChargeWeight,
                 Specification.RoastProfileGreenBeanComponents.GreenBeanPercentOfCharge,
                 Specification.RoastProfileGreenBeanComponents.partno,
                 Specification.RoastProfileGreenBeanComponents.PK_RoastProfileID
        FROM     wip.Schedule, wip.RoasterQueueSequence, Specification.RoastProfileGreenBeanComponents
        WHERE    wip.RoasterQueueSequence.Status = 'Queued'
                 AND wip.roasterqueuesequence.RoastProfile = specification.RoastProfileGreenBeanComponents.PK_RoastProfileID
                 AND wip.RoasterQueueSequence.PK_RoasterQueueSequenceID = wip.Schedule.FK_RoasterQueueSequenceID
                 AND wip.RoasterQueueSequence.RoasterNumber = 6
        ORDER BY PublishedStartDateTime;
        SELECT @RETURN = @@rowcount
        FROM   WIP.RoasterQueueSequence;
    END

2 个答案:

答案 0 :(得分:1)

一种方法是使用CTE和ROW_NUMBER函数。基本方法是在主键上对FK表中的每个记录(一对多关系中的许多记录)进行编号,然后使用高于阈值的数字(在这种情况下为7)删除每个记录。您还可以使用GROUP BY来了解要包含的PK记录。例如,假设我们正在处理Orders和OrderItems,您的查询将如下所示:

;WITH myCTE AS (
    SELECT 
         OrderDetailID,
         OrderID,
         ROW_NUMBER() OVER (PARTITION BY OrderID ORDER BY OrderDetailID)
            as [OrderItemCount]
    FROM OrderDetails
) 
SELECT * 
FROM myCTE 
WHERE 
    --Get up to 7 details
    OrderItemCount < 8
    --Get up to 6 orders
    AND OrderID IN (
       SELECT TOP 6 OrderID FROM myCTE GROUP BY OrderID ORDER BY OrderID DESC
    )
ORDER BY
    --Most recent orders first
    OrderID DESC

答案 1 :(得分:0)

我认为您只需要使用子查询从RoasterQueueSequence获取前6个记录,并将结果连接到其他表。

select x.*,...
from
(SELECT TOP 6 PK_RoasterQueueSequenceID,
                       RoasterNumber,
                       RoastProfile,
                       RoastDescription,
                       ChargeWeight
        FROM     wip.RoasterQueueSequence AS TRQS
        INNER JOIN 
                 wip.schedule AS s
                 ON s.fk_roasterqueuesequenceid = TRQS.PK_RoasterQueueSequenceID
        WHERE    Trqs.Status = 'Queued'
                 AND Trqs.PK_RoasterQueueSequenceID = s.FK_RoasterQueueSequenceID
                 AND Trqs.RoasterNumber = 6
) as x
inner join ...