SELECT @@IDENTITY
臨時表
內嵌視圖
LEFT JOIN 消耗的資源非常之多
加快使用 LEFT JOIN 的查詢速度的一項技術涉及創建一個 TABLE 數據類型
DECLARE @tblMonths TABLE (sMonth VARCHAR(
對於此技巧
其中一段示例代碼
DECLARE @tblMonths TABLE (sMonth VARCHAR(
DECLARE @tblCustomers TABLE ( CustomerID CHAR(
CompanyName VARCHAR(
ContactName VARCHAR(
DECLARE @tblFinal TABLE ( sMonth VARCHAR(
CustomerID CHAR(
CompanyName VARCHAR(
ContactName VARCHAR(
mSales MONEY)
DECLARE @dtStartDate DATETIME
@dtEndDate DATETIME
@dtDate DATETIME
@i INTEGER
SET @dtEndDate =
SET @dtEndDate = DATEADD(DD
VARCHAR(
SET @dtStartDate = DATEADD(MM
SET @i =
WHILE (@i <
BEGIN
SET @dtDate = DATEADD(mm
INSERT INTO @tblMonths SELECT CAST(YEAR(@dtDate) AS VARCHAR(
CASE
WHEN MONTH(@dtDate) <
THEN
ELSE CAST(MONTH(@dtDate) AS VARCHAR(
END AS sMonth
SET @i = @i +
END
INSERT INTO @tblCustomers
SELECT DISTINCT
c
c
c
FROM Customers c
INNER JOIN Orders o ON c
WHERE o
INSERT INTO @tblFinal
SELECT m
c
c
c
FROM @tblMonths m CROSS JOIN @tblCustomers c
UPDATE @tblFinal SET
mSales = mydata
FROM @tblFinal f INNER JOIN
(
SELECT c
CAST(YEAR(o
CASE WHEN MONTH(o
THEN
ELSE CAST(MONTH(o
END AS sMonth
SUM(od
FROM Customers c
INNER JOIN Orders o ON c
INNER JOIN [Order Details] od ON o
WHERE o
GROUP BY
c
CAST(YEAR(o
CASE WHEN MONTH(o
THEN
ELSE CAST(MONTH(o
END
) mydata on f
mydata
SELECT f
f
f
f
f
FROM @tblFinal f
ORDER BY
f
f
另一個提高效率的技巧是使用 DISTINCT 關鍵字查找數據行的單獨報表
From:http://tw.wingwit.com/Article/program/Java/Javascript/201311/25338.html