2015-01-08 59 views
0

我有一個查詢鏈接表一個兩個表二,並獲得結果在表一最後的評論,這工作,但是當有多個記錄從表一個與例如,它再次說明,因此這裏的同一個ID就是一個例子刪除重複的行,如果其中一列是相同的Sql服務器

的查詢結果,

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 
+0

嘗試完全限定QCUID - 即在外部應用範圍中使用** QualityControl_Comments.QCUID **。否則我沒有看到有什麼問題 – Madison

+0

問題是,這是拉的所有記錄的一天,但是我只想要最新的一天,關於評論表的部分是好的 –

+0

啊,我的壞。這可能會幫助你:http://dba.stackexchange.com/questions/17217/returning-a-result-set-with-multiple-rows-based-on-max-date – Madison

回答

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 = 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 
+0

嘿,謝謝你,這對我需要的東西完美的作品 –

1

您可以使用類似這樣的東西。 使用row_number你可以通過machnine id對數據進行分區並按日期對數字進行排序。比你可以只使用一個簡單的地方選擇你想要的東西

;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 
+0

嘿,我得到這些錯誤信息4104,級別16,狀態1,行17 無法綁定多部分標識符「Qualitycontrol.MachineNo」。 Msg 4104,Level 16,State 1,Line 17 無法綁定多部分標識符「Qualitycontrol.CreatedTime」。 –

+0

請立即嘗試,我無法測試該查詢,但請立即嘗試。我已經更新了答案 – sdrzymala

+0

是的,這是有效的,當我在Microsoft SQL服務器管理工​​作室中運行查詢,但是當我在PHP中使用它它不會返回任何東西,你知道爲什麼這將是 –

相關問題