MSSQL查询结果中重复记录

时间:2017-04-26 11:40:24

标签: php sql-server

我已经集成了与MSSQL的系统连接。系统根据寄售编号获取所有记录。编号是整数形式,如(12到15)数字。查询工作正常,但获取重复结果"处理源HUB"在与事务时间相关的行中(DATETIME列名)。下面是我的查询和截图。

SELECT ct.transactionTime,
       st.TrackingStatus Event,
       ct.consignmentNumber,
       ct.currentLocation AS LOCATION,
       CONVERT(NVARCHAR, c.bookingDate, 105) BookingDate,
       CASE
            WHEN ct.stateID = '18' THEN 'Consignment Arrived At ' + ct.currentLocation
                 + ' Main Office' +
                 ' on ' + CONVERT(NVARCHAR, transactionTime, 105)
            WHEN ct.stateID = '1' THEN ' Conginment has been Booked on ' +
                 CONVERT(NVARCHAR, c.bookingdate, 105)
                 + ' in ' + ct.currentLocation
            WHEN ct.stateID = '2' OR ct.stateID = '3' OR ct.stateID = '4' THEN
                 'Processing at Origin HUB'
            WHEN ct.stateID in ('5','6','7') THEN
                 'Reached at Destination HUB'
            WHEN ct.stateID = '8' THEN
                 'Shipment on route '
                 + CONVERT(NVARCHAR, transactionTime, 105)
                 + ' in ' + ct.currentLocation
            WHEN ct.stateID = '9' THEN
                 'Consignment has been MAWB - Manifested AWB Number: '
                 + ct.mawbnumber + ' on ' + CONVERT(NVARCHAR, transactionTime, 105)
                 + ' in ' + ct.currentLocation
            WHEN ct.stateID = '10' THEN (
                     SELECT 'Consignment has been "' + ct.reason
                        --
                        + ' '
                        + '" Received By "' + (
                            CASE
                                 WHEN rc.receivedBy IS NULL THEN ''
                                 ELSE rc.receivedBy
                            END
                        ) + '" Dated: ' + (
                            CASE
                                 WHEN ct.stateID = '10' THEN LEFT(rc.deliveryDate, 10)
                            END
                        ) + (
                            CASE
                                 WHEN rc.time IS NULL THEN ''
                                 ELSE RIGHT(rc.time, 8)
                            END
                        )
                        + '" '
                 FROM   runsheetconsignment rc
                 WHERE  ct.consignmentnumber = rc.consignmentnumber
                 AND ct.runsheetnumber = rc.runsheetnumber
             )
        WHEN ct.stateID = '15' THEN 'Consignment has been ' + ct.reason
             + ' ' + CONVERT(NVARCHAR, transactionTime, 105) +
             ' in ' + ct.currentLocation
   END Detail
    FROM Consignment_Tracking_View ct
    INNER JOIN Consignment c
        ON  ct.consignmentNumber = c.consignmentNumber
    INNER JOIN MNP_ConsginmentTrackingStatus st
        ON  ct.stateID = st.StatusID
    WHERE c.consignmentNumber = '591091000000051' and c.consignerAccountNo not in ('4B45','7240','4H86','4H87','4H88','4B87','7240','4B45','4H91','4H89','4H90','4F47')
    ORDER BY ct.transactionTime desc

请帮帮我。

imagetracking

1 个答案:

答案 0 :(得分:0)

记录不重复,Datetime列具有不同的值,将该列数据转换为仅日期为强制转换:CAST(column as Date)然后如果需要,则仅在select语句中应用不同的

要根据您的评论获得状态明智的单个记录,请使用PARTITION BY,如下所示:

;WITH T AS
(
    SELECT [DateTime],
    [Status],
    Location,
    ROW_NUMBER() OVER (PARTITION BY [Status],Location ORDER BY Location) AS PartNo
    From TableName
)

SELECT
    *
FROM T WHERE PartNo=1