我有一個以前創建的SQL視圖,其中包含我期待得到的數據。它有Case時,它使我難以運行MAX(日期)...我試圖運行基於它的視圖,它只是保持超時...我可以創建一個新的視圖與數據我需要但我仍然需要數據操縱,必須在一些案例中,當我們離開......SQL查看MAX(日期)瘋狂
這種觀點目前生產這種信息
THDATE ComponentItemNumber Issued Quantity
12/23/2013 12:00:00 AM TTMS-12.7-9 19
12/23/2013 12:00:00 AM TTMS-4.8-9 8.34
12/23/2013 12:00:00 AM TTMS-4.8-9 10.66
12/23/2013 12:00:00 AM 44476-3112 2
12/23/2013 12:00:00 AM M80-0130005 NULL
我想要做的就是簡單地獲取最大日期爲每項目# 這裏是當前視圖...有人可以幫我創建一個SQL語句來做到這一點嗎?請...我正在使用SQL 2000
SELECT THDATE
, CASE WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2 + THBODY3 + THBODY4,
7) = 'C' THEN NULL
ELSE dbo.udf_GetHistoryVarchar(THBODY + THBODY2 + THBODY3
+ THBODY4, 10)
END AS ComponentItemNumber
, CASE WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2 + THBODY3 + THBODY4,
3) = 'C' THEN NULL
WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2 + THBODY3 + THBODY4,
2) = 'D' THEN NULL
ELSE dbo.udf_GetHistoryFloat(THBODY + THBODY2 + THBODY3 + THBODY4,
17)
END AS IssuedQuantity
FROM dbo.Mfg_TRANHIS AS T1
WHERE (THTRID = 'PICK')
AND (THFLAG = '1')
AND (dbo.udf_GetHistoryChar(THBODY + THBODY2 + THBODY3 + THBODY4, 1) = 'C')
AND (NOT ((CASE WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2
+ THBODY3 + THBODY4, 7) = 'C'
THEN NULL
ELSE dbo.udf_GetHistoryVarchar(THBODY + THBODY2
+ THBODY3 + THBODY4,
10)
END) LIKE 'CU%')
)
AND (NOT ((CASE WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2
+ THBODY3 + THBODY4, 2) LIKE '[MP]'
THEN dbo.udf_GetHistoryVarchar(THBODY + THBODY2
+ THBODY3 + THBODY4,
4)
WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2
+ THBODY3 + THBODY4, 3) = 'C'
THEN dbo.udf_GetHistoryVarchar(THBODY + THBODY2
+ THBODY3 + THBODY4,
5)
WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2
+ THBODY3 + THBODY4, 2) = 'D'
THEN dbo.udf_GetHistoryVarchar(THBODY + THBODY2
+ THBODY3 + THBODY4,
5)
WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2
+ THBODY3 + THBODY4, 7) = 'C'
THEN dbo.udf_GetHistoryVarchar(THBODY + THBODY2
+ THBODY3 + THBODY4,
4)
ELSE dbo.udf_GetHistoryVarchar(THBODY + THBODY2
+ THBODY3 + THBODY4,
4)
END) LIKE 'VM%')
)
請格式化您的問題,並將其減少到非常嚴重的問題。 –
所有這些udf's,CASE語句,而不是?在嘗試保釋水之前,您可能應該修補漏洞(即修復您的數據模型)。 –
標量值函數在SQL Server中聲名狼借。這不太好,但使用表值函數可以極大地提高性能。 – 2014-01-06 17:20:40