用于显示总计数的SQL查询

时间:2012-11-01 20:11:22

标签: sql tsql ssms

我有一个非常复杂的查询,它连接了八(8)个不同的表。

但是,为了这个问题,我将用这种结构简化它:

create table table1(PacketID int, RequestID int, EmpID int, PartNo varchar(20))

insert into table1 values
 (1 , 1, 132, 'abc123'),
 (1 , 2, 132, 'abc456'),
 (1 , 3, 132, 'def123'),
 (1 , 4, 132, 'def456'),
 (2 , 5, 228, 'xyz123'),
 (3 , 6, 239, 'xyz321'),
 (3 , 7, 239, 'aaa000')

此类表格会创建以下输出:

|_P_|_R_|_Emp_|_PartNo_|
|_1_|_1_|_132_|_abc123_|
|_1_|_2_|_132_|_abc465_|
|_1_|_3_|_132_|_def123_|
|_1_|_4_|_132_|_def456_|
|_2_|_5_|_228_|_xyz123_|
|_3_|_6_|_239_|_xyz321_|
|_3_|_7_|_239_|_aaa000_|

我在这里放了一个小提琴:http://www.sqlfiddle.com/#!3/a3ce4/1

我被告知他们确实不需要这个PacketIDRequestID,但他们需要的是显示数据包中有多少请求以及该数据包需要注意的请求数量

我仍然需要PacketIDRequestID值来更改数据。

所以,我想在上面的表中添加一个列,如下所示:

|_P_|_R_|_ReadAs_|_Emp_|_PartNo_|
|_1_|_1_|_1 of 4_|_132_|_abc123_|
|_1_|_2_|_2 of 4_|_132_|_abc465_|
|_1_|_3_|_3 of 4_|_132_|_def123_|
|_1_|_4_|_4 of 4_|_132_|_def456_|
|_2_|_5_|_1 of 1_|_228_|_xyz123_|
|_3_|_6_|_1 of 2_|_239_|_xyz321_|
|_3_|_7_|_2 of 2_|_239_|_aaa000_|

我到底该怎么做?

如果您只是想感受我的痛苦,并且看到我的SQL Server上定义的完整视图,那么它就是:

SELECT P.ID AS PacketID, R.ID AS RequestID, A.ID AS ActionID,
  EI.FIRSTNAME + ' ' + EI.LASTNAME AS Employee, P.DateStamp, 
  RQ.Description AS RequestType, L.Description AS Line, R.PartNo,
  R.Workorder, R.Qty, RZ.Description AS ReasonType, R.MTF,
  A.StatusID, S.Description AS Status, A.EmpID AS Stator, 
  A.DateStamp AS Stated
FROM dbo.Packet AS P
  LEFT OUTER JOIN dbo.Request AS R ON R.PacketID = P.ID
  INNER JOIN dbo.Action AS A ON R.ID = A.RequestID
  LEFT OUTER JOIN dbo.Action AS A2 ON A.RequestID = A2.RequestID
         AND (A.DateStamp < A2.DateStamp OR
         A.DateStamp = A2.DateStamp AND A.RequestID < A2.RequestID)
  INNER JOIN CPAPP.AIO_Test_Results.dbo.EmployeeInfo AS EI ON A.EmpID = EI.COUNT
  INNER JOIN dbo.RequestType AS RQ ON R.RequestTypeID = RQ.ID
  INNER JOIN dbo.Line AS L ON R.LineID = L.ID
  INNER JOIN dbo.ReasonType AS RZ ON R.ReasonTypeID = RZ.ID
  INNER JOIN dbo.Status AS S ON A.StatusID = S.ID
WHERE (A2.RequestID IS NULL)

screenshot

2 个答案:

答案 0 :(得分:3)

我会尝试解释如何自己创建答案,而不仅仅是给出完整的答案。

要获得连续的序列号,请查看ROW_NUMBER。您可以通过(PacketID)提供分区,并通过(RequestId)提供订单。这可以为您提供所需的第一个号码。

要获取计数,如果您使用的是SQL Server 2012,则可以将COUNT与分区一起使用。在旧版本的SQL Server上,您必须使用JOIN和GROUP BY或子选择。

此查询使用subselect方法演示了原则:

SELECT
    PacketID,
    RequestID,
    EmpID,
    PartNo,
    ROW_NUMBER() OVER (PARTITION BY PacketID ORDER BY RequestID),
    (SELECT COUNT(*) FROM table1 AS T2 WHERE T1.PacketId = T2.PacketId)
FROM table1 AS T1

http://www.sqlfiddle.com/#!3/a3ce4/8

要获得您请求的确切格式,您只需将整数转换为字符串并将它们连接起来。我会留下这个给你做,但这里有一些提示。

答案 1 :(得分:2)

正如@MarkByers所提到的,row_number()在这里会特别有用,我已经添加了一个相关的子查询来获取每个数据包的计数:

select PacketID, RequestID
    , cast(RowNumber as varchar(11)) + ' of ' + cast(PacketCount as varchar(11)) as ReadAs
    , EmpID, PartNo
from (
    select PacketID, RequestID, EmpID, PartNo
        , row_number() over (partition by PacketID order by RequestID) as RowNumber
        , (select count(*) from table1 where PacketID = t.PacketID) as PacketCount
    from table1 as t
) as z