サブクエリを含むSQLを以下のように書き、データが取得できるとこを確認しました。
しかし、Laravelのクエリビルダに置き換えたいのですが、それができないのでご教示いただきたいです。
SELECT *, (NOW.投稿 / PREV.投稿 * 100) AS 金額前年比, (NOW.投稿数 / PREV.投稿数 * 100) AS 投稿数前年比, (NOW.投稿単価 / PREV.投稿単価 * 100) AS 投稿単価前年比, (NOW.点数 / PREV.点数 * 100) AS 点数前年比 FROM ( SELECT DATE_FORMAT(posts.created_at, "%Y%m%d") as 日付, DATE_FORMAT(posts.created_at, "%m%d") as 日, SUM(posts.total_amt) AS 投稿, COUNT(items.post_id) AS 投稿数, SUM(posts.total_amt)/COUNT(items.post_id) AS 投稿単価, SUM(posts.qty) AS 点数 FROM posts inner join items on posts.post_id = items.post_id where posts.created_at >= $start and posts.created_at <= $end GROUP by DATE_FORMAT(posts.created_at, "%Y%m%d") )AS NOW LEFT JOIN ( SELECT DATE_FORMAT(posts.created_at, "%Y%m%d") as 日付, DATE_FORMAT(posts.created_at, "%m%d") as 日, SUM(posts.total_amt) AS 投稿, COUNT(items.post_id) AS 投稿数, SUM(posts.total_amt)/COUNT(items.post_id) AS 投稿単価, SUM(posts.qty) AS 点数 FROM posts inner join items on posts.post_id = items.post_id where posts.created_at >= DATE_ADD($start,INTERVAL -1 YEAR) and posts.created_at <= DATE_ADD($end,INTERVAL -1 YEAR) GROUP by DATE_FORMAT(posts.created_at, "%Y%m%d") )AS PREV ON NOW.日= PREV.日
あなたの回答
tips
プレビュー