查询结果中的重复记录

时间:2014-09-08 11:50:12

标签: sql-server tsql

以下是有效的查询:

SELECT  T_ActionTicketLog.ActionTicketID
     ,T_ActionTicketLog.BarCode
     ,T_ActionTicketLog.UserID
     ,T_TicketStatus.Name
     ,T_OrderTicket.OrderID
FROM T_ActionTicketLog 
INNER JOIN T_TicketStatus 
    ON T_ActionTicketLog.StatusID = T_TicketStatus.ID
LEFT OUTER JOIN T_OrderTicket 
    ON T_ActionTicketLog.TicketOrderID = T_OrderTicket.ID
where T_ActionTicketLog.ActionTicketID = 21780101

返回了27条记录,没问题。

但是,我想以这种方式向结果集添加一个字段:

SELECT  T_ActionTicketLog.ActionTicketID
     ,T_ActionTicketLog.BarCode
     ,T_ActionTicketLog.UserID
     ,T_TicketStatus.Name
     ,T_OrderTicket.OrderID
     ,T_TicketPrint.TicketBarCode
FROM T_ActionTicketLog 
INNER JOIN T_TicketStatus 
    ON T_ActionTicketLog.StatusID = T_TicketStatus.ID
LEFT OUTER JOIN T_OrderTicket 
    ON T_ActionTicketLog.TicketOrderID = T_OrderTicket.ID
LEFT OUTER JOIN T_TicketPrint 
    ON T_OrderTicket.ActionTicketID = T_TicketPrint.ActionTicketID
where   T_ActionTicketLog.ActionTicketID = 21780101

返回了165条记录,这是错误的。

额外的左外连接会产生问题。

表格:

CREATE TABLE [T_ActionTicketLog](
    [ID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [ActionTicketID] [bigint] NULL,
    [TicketOrderID] [bigint] NULL,
    [StatusID] [tinyint] NULL,
    [UserID] [int] NULL,
    [SalerID] [int] NULL,
    [FiscalID] [int] NULL,
    [BarCode] [bigint] NULL,
    [ReservDate] [datetime] NULL,
    [Created] [datetime] NULL,
    [Comments] [varchar](50) NULL,

CREATE TABLE [T_TicketStatus](
    [ID] [tinyint] NOT NULL,
    [Name] [varchar](50) NULL,
    [Created] [datetime] NULL,


CREATE TABLE [T_TicketPrint](
    [ID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [CashierID] [int] NULL,
    [BarCode] [bigint] NULL,
    [ControlDigit] [tinyint] NULL,
    [ActionTicketID] [bigint] NULL,
    [Created] [datetime] NULL,
    [CancelDate] [datetime] NULL,
    [TicketBarCode] [varchar](250) NULL,
    [OrderTicketID] [bigint] NULL,
    [SetId] [bigint] NULL,

CREATE TABLE [T_OrderTicket](
    [ID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [OrderID] [int] NULL,
    [ActionTicketID] [bigint] NULL,
    [Status] [smallint] NULL,
    [Created] [datetime] NULL,
    [UserID] [int] NULL,

如何在不重复记录的情况下添加附加字段?

3 个答案:

答案 0 :(得分:1)

我认为问题是你在T_TicketPrint中为T_OrderTicket中的每条记录都有多条记录,但是你只想显示其中一条记录,在这种情况下我会将LEFT JOIN更改为外部申请,只需选择前1条记录:

SELECT  T_ActionTicketLog.ActionTicketID, 
        T_ActionTicketLog.BarCode, 
        T_ActionTicketLog.UserID,
        T_TicketStatus.Name, 
        T_OrderTicket.OrderID, 
        tp.TicketBarCode
FROM    T_ActionTicketLog 
        INNER JOIN T_TicketStatus 
            ON T_ActionTicketLog.StatusID = T_TicketStatus.ID
        LEFT OUTER JOIN T_OrderTicket 
            ON T_ActionTicketLog.TicketOrderID = T_OrderTicket.ID
        OUTER APPLY
        (   SELECT  TOP 1 TicketBarCode
            FROM    T_TicketPrint 
            WHERE   T_OrderTicket.ActionTicketID = T_TicketPrint.ActionTicketID
            ORDER BY T_TicketPrint.Created DESC -- NEWEST RECORD
        ) AS tp
WHERE   T_ActionTicketLog.ActionTicketID = 21780101

由于你可以在T_TicketPrint中有多个记录,真正的问题就是你应该选择哪一个?在上面的示例中,我假设了最新的一个,但如果这不正确,只需更改OUTER APPLY中的order by子句。

答案 1 :(得分:1)

您的查询返回165记录,因为T_TicketPrint有多个单一票证条目。 因此,您需要从T_TicketPrint表中获取单个记录。

为此我在T_TicketPrint中使用ActionTicketID组并获取条形码。 您的查询看起来像并根据要求进行更改:

SELECT  T_ActionTicketLog.ActionTicketID, T_ActionTicketLog.BarCode, T_ActionTicketLog.UserID,
      T_TicketStatus.Name, T_OrderTicket.OrderID, A.TicketBarCode
FROM   T_ActionTicketLog INNER JOIN T_TicketStatus ON T_ActionTicketLog.StatusID = T_TicketStatus.ID
LEFT OUTER JOIN T_OrderTicket ON T_ActionTicketLog.TicketOrderID = T_OrderTicket.ID
LEFT OUTER JOIN 
(select TicketBarCode, ActionTicketID from T_TicketPrint 
group by ActionTicketID,TicketBarCode) as A ON T_OrderTicket.ActionTicketID = A.ActionTicketID
where   T_ActionTicketLog.ActionTicketID = 21780101

答案 2 :(得分:0)

有几种方法可以解决这个问题(分组,子查询等),这是分组路线:

SELECT  T_ActionTicketLog.ActionTicketID
    ,T_ActionTicketLog.BarCode
    ,T_ActionTicketLog.UserID
    ,T_TicketStatus.Name
    ,T_OrderTicket.OrderID
    ,MAX(T_TicketPrint.TicketBarCode)
FROM T_ActionTicketLog 
INNER JOIN T_TicketStatus
    ON T_ActionTicketLog.StatusID = T_TicketStatus.ID
LEFT OUTER JOIN T_OrderTicket 
    ON T_ActionTicketLog.TicketOrderID = T_OrderTicket.ID
LEFT OUTER JOIN T_TicketPrint 
    ON T_OrderTicket.ActionTicketID = T_TicketPrint.ActionTicketID
WHERE   T_ActionTicketLog.ActionTicketID = 21780101
GROUP BY T_ActionTicketLog.ActionTicketID
    ,T_ActionTicketLog.BarCode
    ,T_ActionTicketLog.UserID
    ,T_TicketStatus.Name
    ,T_OrderTicket.OrderID

您可能需要将MAX()聚合更改为更有意义的其他内容,具体取决于需要如何处理与OrderTicket关联的多个TicketPrints。