如果其中一列是相同的 Sql-server,则删除重复的行
removing duplicate rows if one of the columns are the same Sql-server
我有一个查询,它链接 table 一二 table 二并获取 table 中结果的最后一条评论,这有效但是当有来自 table 例如,具有相同 ID 的结果再次显示为示例
查询结果
ID | Machine | description | createdtime | product code | work order | qty | comment | Reson
No
129 |1 | A name | 2015-01-08 07:38:41.427 | A code | 12/14/0038 | 4000 | comment | Reason
143 |1 | A name | 2015-01-08 13:30:39.403 | A code | 12/14/0038 | 4000 | comment | Reson
130 |4 | A name | 2015-01-08 07:38:46.540 | A code | 12/14/0045 | 12000 | comment | Reason
131 |5 | A name | 2015-01-08 07:38:50.243 | A code | 01/15/0001 | 4000 | comment | Reason
如您所见,有两条机器号为 1 的记录,一条在早上创建,一条在中午创建,但我只想显示最新的一条,这是我的查询。
SELECT Qualitycontrol.ID, Qualitycontrol.MachineNo, Qualitycontrol.Description, Qualitycontrol.CreatedTime, Qualitycontrol.ProductCode, Qualitycontrol.WorkOrder,
Qualitycontrol.Quantity, Qc.Comment, Qc.Reason
FROM Qualitycontrol
OUTER APPLY (
SELECT TOP 1 *
FROM QualityControl_Comments
WHERE Qualitycontrol.ID = QCUID
ORDER BY Qualitycontrol.ID DESC -- whatevet defines order in QualityControl_Comments
) AS Qc
WHERE (Qualitycontrol.CreatedTime BETWEEN CAST(GETDATE() AS DATE) AND DATEADD(DAY, 1, CAST(GETDATE() AS DATE)))
ORDER BY Qualitycontrol.MachineNo
你可以使用这样的东西。
使用 row_number 您可以按机器 ID 对数据进行分区,并按日期对数字进行排序。比你可以只使用一个简单的 where clouse select 你想要的
;WITH CTE
as
(
SELECT Qualitycontrol.ID, Qualitycontrol.MachineNo, Qualitycontrol.Description, Qualitycontrol.CreatedTime, Qualitycontrol.ProductCode, Qualitycontrol.WorkOrder,
Qualitycontrol.Quantity, Qc.Comment, Qc.Reason
FROM Qualitycontrol
OUTER APPLY (
SELECT TOP 1 *
FROM QualityControl_Comments
WHERE Qualitycontrol.ID = QCUID
) AS Qc
WHERE (Qualitycontrol.CreatedTime BETWEEN CAST(GETDATE() AS DATE) AND DATEADD(DAY, 1, CAST(GETDATE() AS DATE)))
), CTE2 as
(
select *, row_number() over(partition by MachineNo order by CreatedTime desc) as 'row_index' from cte
)
select * from cte2
where row_index = 1
order by MachineNo
也许这样的事情会奏效。
SELECT Qualitycontrol.ID, Qualitycontrol.MachineNo, Qualitycontrol.Description, Qualitycontrol.CreatedTime, Qualitycontrol.ProductCode, Qualitycontrol.WorkOrder,
Qualitycontrol.Quantity, Qc.Comment, Qc.Reason
FROM Qualitycontrol
OUTER APPLY (
SELECT TOP 1 *
FROM QualityControl_Comments
WHERE Qualitycontrol.ID = QualityControl_Comments.QCUID
ORDER BY Qualitycontrol.ID DESC -- whatevet defines order in QualityControl_Comments
) AS Qc
INNER JOIN
(
SELECT Qualitycontrol.MachineNo, MAX(Qualitycontrol.ID) MID
FROM Qualitycontrol
GROUP BY Qualitycontrol.MachineNo
) UNQ ON UNQ.MID = Qualitycontrol.ID
WHERE (Qualitycontrol.CreatedTime BETWEEN CAST(GETDATE() AS DATE) AND DATEADD(DAY, 1, CAST(GETDATE() AS DATE)))
ORDER BY Qualitycontrol.MachineNo
我有一个查询,它链接 table 一二 table 二并获取 table 中结果的最后一条评论,这有效但是当有来自 table 例如,具有相同 ID 的结果再次显示为示例
查询结果
ID | Machine | description | createdtime | product code | work order | qty | comment | Reson
No
129 |1 | A name | 2015-01-08 07:38:41.427 | A code | 12/14/0038 | 4000 | comment | Reason
143 |1 | A name | 2015-01-08 13:30:39.403 | A code | 12/14/0038 | 4000 | comment | Reson
130 |4 | A name | 2015-01-08 07:38:46.540 | A code | 12/14/0045 | 12000 | comment | Reason
131 |5 | A name | 2015-01-08 07:38:50.243 | A code | 01/15/0001 | 4000 | comment | Reason
如您所见,有两条机器号为 1 的记录,一条在早上创建,一条在中午创建,但我只想显示最新的一条,这是我的查询。
SELECT Qualitycontrol.ID, Qualitycontrol.MachineNo, Qualitycontrol.Description, Qualitycontrol.CreatedTime, Qualitycontrol.ProductCode, Qualitycontrol.WorkOrder,
Qualitycontrol.Quantity, Qc.Comment, Qc.Reason
FROM Qualitycontrol
OUTER APPLY (
SELECT TOP 1 *
FROM QualityControl_Comments
WHERE Qualitycontrol.ID = QCUID
ORDER BY Qualitycontrol.ID DESC -- whatevet defines order in QualityControl_Comments
) AS Qc
WHERE (Qualitycontrol.CreatedTime BETWEEN CAST(GETDATE() AS DATE) AND DATEADD(DAY, 1, CAST(GETDATE() AS DATE)))
ORDER BY Qualitycontrol.MachineNo
你可以使用这样的东西。 使用 row_number 您可以按机器 ID 对数据进行分区,并按日期对数字进行排序。比你可以只使用一个简单的 where clouse select 你想要的
;WITH CTE
as
(
SELECT Qualitycontrol.ID, Qualitycontrol.MachineNo, Qualitycontrol.Description, Qualitycontrol.CreatedTime, Qualitycontrol.ProductCode, Qualitycontrol.WorkOrder,
Qualitycontrol.Quantity, Qc.Comment, Qc.Reason
FROM Qualitycontrol
OUTER APPLY (
SELECT TOP 1 *
FROM QualityControl_Comments
WHERE Qualitycontrol.ID = QCUID
) AS Qc
WHERE (Qualitycontrol.CreatedTime BETWEEN CAST(GETDATE() AS DATE) AND DATEADD(DAY, 1, CAST(GETDATE() AS DATE)))
), CTE2 as
(
select *, row_number() over(partition by MachineNo order by CreatedTime desc) as 'row_index' from cte
)
select * from cte2
where row_index = 1
order by MachineNo
也许这样的事情会奏效。
SELECT Qualitycontrol.ID, Qualitycontrol.MachineNo, Qualitycontrol.Description, Qualitycontrol.CreatedTime, Qualitycontrol.ProductCode, Qualitycontrol.WorkOrder,
Qualitycontrol.Quantity, Qc.Comment, Qc.Reason
FROM Qualitycontrol
OUTER APPLY (
SELECT TOP 1 *
FROM QualityControl_Comments
WHERE Qualitycontrol.ID = QualityControl_Comments.QCUID
ORDER BY Qualitycontrol.ID DESC -- whatevet defines order in QualityControl_Comments
) AS Qc
INNER JOIN
(
SELECT Qualitycontrol.MachineNo, MAX(Qualitycontrol.ID) MID
FROM Qualitycontrol
GROUP BY Qualitycontrol.MachineNo
) UNQ ON UNQ.MID = Qualitycontrol.ID
WHERE (Qualitycontrol.CreatedTime BETWEEN CAST(GETDATE() AS DATE) AND DATEADD(DAY, 1, CAST(GETDATE() AS DATE)))
ORDER BY Qualitycontrol.MachineNo