【问题标题】:Add two columns for sum of money by year按年份添加两列资金总额
【发布时间】:2013-01-19 01:08:06
【问题描述】:

下面是我的查询,将我的客户分组在一起,并总结了他们在过去两年中花费的总金额。奇迹般有效。但是我需要更多的知识来了解我的 sql 知识。

有没有什么方法可以再选择三列,并用他们在这些年(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

【问题讨论】:

    标签: mysql


    【解决方案1】:

    除了customer_id,您还需要GROUP BY YEAR(date_paid)。您也不应该选择不在您的GROUP BY 中的任何列。此外,您可以使用BETWEEN 运算符代替&gt;&lt; 作为您的日期范围。

    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 
    

    结果

    |客户 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 |汤姆 |琼斯 |汤姆@jones.com | 2223334444 |

    See the demo

    【讨论】:

    • 感谢您的回答!我试图在我的原始查询中添加 3 列。无论如何要在 select 语句中进行求和和分组?
    • @jahrichie 查看我的更新。如果您只是提取客户信息,您也许可以摆脱 INNER JOIN
    • 更近了,非常感谢您的帮助!!!最后一个问题:是否有办法将每年的 date_year_paid 休息时间删除到他们自己的列中,然后计算那里的价格? IE 2010_money_paid 22222 2011_money_paid 3232 等等等等?
    猜你喜欢
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    相关资源
    最近更新 更多