0

以下は、顧客をグループ化し、過去2年間に費やした合計金額を合計した私のクエリです。チャームのように機能します。ただし、SQLの知識を超えるもう少し必要です。

さらに3つの列を選択し、それらの各年(2010、2011、2012)に費やした金額を入力する方法はありますか?

SELECT  SUM(price) AS money_spent_total, co.customer_id, cu.first_name, cu.last_name, cu.email_primary, cu.phone_primary, co.date_paid
FROM customer_order   AS co
INNER  JOIN customer AS cu ON (cu.customer_id = co.customer_id) 
WHERE  cu.customer_id != 32518 AND co.date_paid > "2010-1-1" AND co.date_paid < "2013-1-1"
GROUP BY co.customer_id
4

1 に答える 1

2

GROUP BY YEAR(date_paid)に加えてする必要がありますcustomer_id。また、にない列を選択しないでくださいGROUP BY。さらに、日付範囲BETWEENの代わりに演算子>を使用できます。<

SELECT Year(co.date_paid), 
       co.customer_id, 
       Sum(price) AS money_spent_total 
FROM   customer_order AS co 
       INNER JOIN customer AS cu 
               ON ( cu.customer_id = co.customer_id ) 
WHERE  cu.customer_id != 32518 
       AND co.date_paid BETWEEN '2010-01-01' AND '2013-01-01' 
GROUP  BY Year(co.date_paid), 
          co.customer_id 

元の列を取得するには、次のSELECTようにします(テストされていません)。

SELECT a.date_year_paid, 
       a.customer_id, 
       a.money_spent_total, 
       cu.first_name, 
       cu.last_name, 
       cu.email_primary, 
       cu.phone_primary 
FROM   (SELECT Year(co.date_paid) AS date_year_paid, 
               co.customer_id, 
               Sum(price)         AS money_spent_total 
        FROM   customer_order AS co 
               INNER JOIN customer AS cu 
                       ON ( cu.customer_id = co.customer_id ) 
        WHERE  cu.customer_id != 32518 
               AND co.date_paid BETWEEN '2010-01-01' AND '2013-01-01' 
        GROUP  BY Year(co.date_paid), 
                  co.customer_id) a 
       LEFT JOIN customer cu 
              ON cu.customer_id = a.customer_id 

あなたがINNER JOIN顧客情報のためだけにやっていたなら、それは落とすことができます。

  SELECT a.customer_id, 
       a.date_year_paid, 
       a.money_spent_total, 
       cu.first_name, 
       cu.last_name, 
       cu.email_primary, 
       cu.phone_primary 
  FROM (SELECT customer_id, YEAR(date_paid) AS date_year_paid, SUM(price) AS money_spent_total
        FROM customer_order
        GROUP BY customer_id, YEAR(date_paid)) a
       LEFT JOIN customer cu 
              ON cu.customer_id = a.customer_id

最後に、年を列にグループ化する場合は、次のようにします。

SELECT a.customer_id, 
       a.y2010 AS '2010_money_paid', 
       a.y2011 AS '2011_money_paid', 
       a.y2012 AS '2012_money_paid', 
       cu.first_name, 
       cu.last_name, 
       cu.email_primary, 
       cu.phone_primary 
FROM   (SELECT customer_id, 
               Sum(CASE 
                     WHEN Year(date_paid) = 2010 THEN price 
                     ELSE 0 
                   end) AS 'y2010', 
               Sum(CASE 
                     WHEN Year(date_paid) = 2011 THEN price 
                     ELSE 0 
                   end) AS 'y2011', 
               Sum(CASE 
                     WHEN Year(date_paid) = 2012 THEN price 
                     ELSE 0 
                   end) AS 'y2012' 
        FROM   customer_order 
        GROUP  BY customer_id) a 
       LEFT JOIN customer cu 
              ON cu.customer_id = a.customer_id 

結果

| CUSTOMER_ID | 2010_MONEY_PAID | 2011_MONEY_PAID | 2012_MONEY_PAID | FIRST_NAME | LAST_NAME | EMAIL_PRIMARY | PHONE_PRIMARY |
-------------------------------------------------- -------------------------------------------------- --------------------------
| 1 | 9000 | 3000 | 2000 | ボブ| スミス| bob@smith.com | 1112223333 |
| 2 | 4000 | 5000 | 1000 | トム| ジョーンズ| tom@jones.com | 2223334444 |

デモを見る

于 2013-01-18T16:36:50.203 に答える