Skip to content

Instantly share code, notes, and snippets.

@monmonmon
Last active August 8, 2021 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monmonmon/931bbbf12578761155fb95dfb857dbf3 to your computer and use it in GitHub Desktop.
Save monmonmon/931bbbf12578761155fb95dfb857dbf3 to your computer and use it in GitHub Desktop.
-- SQLクイズへの回答 https://github.com/neumann-tokyo/sql-quiz/blob/main/store/question.md
SELECT r.item_name,
r.weeknum,
COALESCE(s.price, 0) AS lastweek_price,
r.price,
CASE
WHEN COALESCE(s.price, 0) < r.price THEN '/'
WHEN COALESCE(s.price, 0) > r.price THEN '\'
ELSE '='
END AS mark
FROM reports r
LEFT JOIN reports s
ON r.item_name = s.item_name
AND r.weeknum - 1 = s.weeknum
ORDER BY r.item_name,
r.weeknum;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment