標準SQLのままではエラーになる問題と解決策の例
以下の問題ではSQLServerの場合、解答例の標準SQLのままではエラーになります。
それぞれの問題に対する解決策の例を記してみましたので参考にしてください。
その1 SQLをSELECT
文で作成する
SELECT
'INSERT INTO Pref_Back VALUES ('
+ CAST( PrefecturalID AS VARCHAR )
+ ','''
+ PrefecturalName
+ ''');' AS 都道府県のINSERT文
FROM
Prefecturals
;
その2 月別販売額一覧の作成
SELECT
年月
, SUM( 販売金額 ) AS 販売合計金額
FROM
(
SELECT
CONVERT( VARCHAR, DATEPART( yyyy, SaleDate ) ) + '-'
+ CONVERT( VARCHAR, DATEPART( mm, SaleDate ) ) AS 年月
, s.Quantity * p.Price AS 販売金額
FROM
Sales AS s
JOIN
Products AS p
ON s.ProductID = p.ProductID
) AS x
GROUP BY
年月
ORDER BY
年月
;
その3 社員別・月別販売額一覧の作成
SELECT
e.EmployeeID
, e.EmployeeName
, 年月
, SUM(
CASE WHEN 販売金額 IS NULL THEN 0
ELSE 販売金額
END
) AS 販売合計金額
FROM
Employees AS e
LEFT OUTER JOIN
(
SELECT
s.EmployeeID
, CONVERT( VARCHAR, DATEPART( yyyy, SaleDate )) + '-'
+ CONVERT( VARCHAR, DATEPART( mm, SaleDate )) AS 年月
, s.Quantity * p.Price AS 販売金額
FROM
Sales AS s
JOIN
Products AS p
ON s.ProductID = p.ProductID
) AS x
ON e.EmployeeID = x.EmployeeID
GROUP BY
e.EmployeeID
, e.EmployeeName
, 年月
ORDER BY
e.EmployeeID
, e.EmployeeName
, 年月
;
その4 商品別・月別販売額一覧の作成
SELECT
p.ProductID
, p.ProductName
, 年月
, SUM( s.Quantity * p.Price )
FROM
(
SELECT
ProductID
, CONVERT(VARCHAR, DATEPART( yyyy, SaleDate )) + '-'
+ CONVERT(VARCHAR, DATEPART( mm, SaleDate )) AS 年月
, Quantity
FROM
Sales
) AS s
JOIN
Products AS p
ON s.ProductID = p.ProductID
WHERE
p.CategoryID IN (1, 3, 9)
GROUP BY
p.ProductID
, p.ProductName
, 年月
HAVING
SUM( s.Quantity * p.Price ) > 5000
ORDER BY
p.ProductID
, p.ProductName
, 年月 DESC
;
その7 部門別・月別平均給与一覧の作成
SELECT
d.DepartmentID
, d.DepartmentName
, 年月
, AVG( Amount ) AS 平均給与
FROM
(
SELECT
EmployeeID
, CONVERT( VARCHAR, DATEPART( yyyy, PayDate )) + '-'
+ CONVERT( VARCHAR, DATEPART( mm, PayDate )) AS 年月
, Amount
FROM
Salary
WHERE
CONVERT( VARCHAR, DATEPART( yyyy, PayDate )) = '2007'
) AS s
JOIN
BelongTo AS b
ON s.EmployeeID = b.EmployeeID
JOIN
Departments AS d
ON b.DepartmentID = d.DepartmentID
GROUP BY
d.DepartmentID
, d.DepartmentName
, 年月
ORDER BY
d.DepartmentID
, d.DepartmentName
, 年月
;
その8 月別・カテゴリ別販売一覧の作成
SELECT
年月
, SUM(
CASE WHEN p.CategoryID = 1 THEN s.Quantity * p.Price
ELSE 0
END
) AS Ct1
, SUM(
CASE WHEN p.CategoryID = 2 THEN s.Quantity * p.Price
ELSE 0
END
) AS Ct2
, SUM(
CASE WHEN p.CategoryID = 3 THEN s.Quantity * p.Price
ELSE 0
END
) AS Ct3
, SUM(
CASE WHEN p.CategoryID = 4 THEN s.Quantity * p.Price
ELSE 0
END
) AS Ct4
, SUM(
CASE WHEN p.CategoryID = 5 THEN s.Quantity * p.Price
ELSE 0
END
) AS Ct5
, SUM(
CASE WHEN p.CategoryID = 6 THEN s.Quantity * p.Price
ELSE 0
END
) AS Ct6
, SUM(
CASE WHEN p.CategoryID = 7 THEN s.Quantity * p.Price
ELSE 0
END
) AS Ct7
, SUM(
CASE WHEN p.CategoryID = 8 THEN s.Quantity * p.Price
ELSE 0
END
) AS Ct8
, SUM(
CASE WHEN p.CategoryID = 9 THEN s.Quantity * p.Price
ELSE 0
END
) AS Ct9
, SUM(
CASE WHEN p.CategoryID = 10 THEN s.Quantity * p.Price
ELSE 0
END
) AS Ct10
FROM
(
SELECT
ProductID
, CONVERT( VARCHAR, DATEPART(yyyy, SaleDate)) + '-'
+ CONVERT( VARCHAR, DATEPART(mm, SaleDate)) AS 年月
, Quantity
FROM
Sales
) AS s
JOIN
Products AS p
ON s.ProductID = p.ProductID
GROUP BY
年月
;
その9 商品別3ヶ月販売推移表の作成
SELECT
ProductID
, ProductName
, Amount06 AS "6月販売金額"
, Amount07 AS "7月販売金額"
, CASE WHEN Amount06 < Amount07 THEN '↑'
WHEN Amount06 = Amount07 THEN '→'
ELSE '↓'
END AS "対6月増減"
, Amount08 AS "8月販売金額"
, CASE WHEN Amount07 < Amount08 THEN '↑'
WHEN Amount07 = Amount08 THEN '→'
ELSE '↓'
END AS "対7月増減"
FROM
(
SELECT
p.ProductID
, p.ProductName
, SUM(
CASE WHEN s.SaleDate IS NULL THEN 0
WHEN CONVERT( VARCHAR, DATEPART( yyyy, s.SaleDate )) + '-'
+ CONVERT( VARCHAR, DATEPART( mm, s.SaleDate )) = '2007-6' THEN s.Quantity * p.Price
ELSE 0
END
) AS Amount06
, SUM(
CASE WHEN s.Quantity IS NULL THEN 0
WHEN CONVERT( VARCHAR, DATEPART( yyyy, s.SaleDate )) + '-'
+ CONVERT( VARCHAR, DATEPART( mm, s.SaleDate )) = '2007-7' THEN s.Quantity * p.Price
ELSE 0
END
) AS Amount07
, SUM(
CASE WHEN s.Quantity IS NULL THEN 0
WHEN CONVERT( VARCHAR, DATEPART( yyyy, s.SaleDate )) + '-'
+ CONVERT( VARCHAR, DATEPART( mm, s.SaleDate )) = '2007-8' THEN s.Quantity * p.Price
ELSE 0
END
) AS Amount08
FROM
Products AS p
LEFT OUTER JOIN
Sales s
ON p.ProductID = s.ProductID
GROUP BY
p.ProductID
, p.ProductName
) AS x
ORDER BY
ProductID
;