在不同月份和总和值的同一表中加入两个查询

时间:2015-10-30 02:26:28

标签: mysql sql

我希望得到member_total_point列的MONTH(NOW())MONTH(NOW() INTERVAL 1 MONTH)列的总和。我该如何选择表格呢?

修改

我不应该在选择

上使用[..]

这是我的新查询:

SELECT la.member_created, la.member_api_domain, la.member_api_campaign, 
      la.total, lb.total_now
FROM (SELECT member_created, member_api_domain, member_api_campaign, SUM(member_total_point) AS 'total' 
     FROM lalights_member WHERE YEAR(member_created)=YEAR(NOW()) 
     AND MONTH(member_created)= MONTH(NOW()-INTERVAL 1 MONTH) 
     GROUP BY member_api_campaign ) AS la 
LEFT JOIN (SELECT member_created,SUM(member_total_point) 
     AS 'total_now' 
     FROM lalights_member WHERE YEAR(member_created)=YEAR(NOW()) 
     AND MONTH(member_created)= MONTH(NOW()) 
     ORDER BY member_api_campaign DESC) AS lb 
ON la.member_created = lb.member_created

但在SQLyog上出现错误:

  

'[total],lb。[total_now] FROM(在第1行选择member_created,member_api_domain,member_a'

我正在使用MySQL 5.1 ..

样本数据:

member_created       member_api_domain  member_api_campaign member_total_point
--------------       -----------------  ------------------- ------------------
2015-09-22 18:30:20  domain             campaign             4 (september)
2015-09-22 18:30:20  domain             campaign             7 (september)
2015-08-22 18:30:20  domain             campaign             2 (august)
2015-08-22 18:30:20  domain             campaign             7 (august)
2015-09-22 18:30:20  asdf               jujuju               4 (september)
2015-08-22 18:30:20  asdf               jujuju               9 (august)

样本输出:

member_api_campaign this_month last_1month last_2month
------------------- ---------- ----------- -----------
domain              campaign   11(september) 9 (august)
asdf                jujuju     4 (september) 9 (august)

提醒,现在是October

2 个答案:

答案 0 :(得分:0)

GROUP BY必须包含您选择的所有列。 在SQL Server 2005+中,您可以使用Latest Jackson integration improvements in Spring,如下所示:

SELECT member_created, member_api_domain, member_api_campaign,
     SUM(CASE WHEN MONTH(member_created)= MONTH(NOW()) THEN member_total_point) OVER(PARTITION BY member_api_campaign) AS 'total',
     SUM(CASE WHEN MONTH(member_created)= MONTH(NOW()-INTERVAL 1 MONTH) THEN member_total_point) OVER(PARTITION BY member_api_campaign) AS 'total now'
FROM lalights_member 
WHERE YEAR(member_created)=2014 
AND (MONTH(member_created)= MONTH(NOW()) OR MONTH(member_created)= MONTH(NOW()-INTERVAL 1 MONTH))
ORDER BY member_api_campaign DESC

答案 1 :(得分:0)

  • 更改单引号的单引号(平均字符串)或只删除字段别名的引号

    'total'     for `total`
    'total_now' for `total_now`
    
  • 删除ORDER BY member_api_campaign DESC

SELECT la.member_created
    ,la.member_api_domain
    ,la.member_api_campaign
    ,la.total
    ,lb.total_now
FROM (
    SELECT member_created
          ,member_api_domain
          ,member_api_campaign
          ,SUM(member_total_point) AS 'total'
    FROM lalights_member
    WHERE YEAR(member_created) = YEAR(NOW())
      AND MONTH(member_created) = MONTH(NOW() - INTERVAL 1 MONTH)
    GROUP BY member_api_campaign
    ) AS la
LEFT JOIN (
    SELECT member_created
          ,SUM(member_total_point) AS 'total_now'
    FROM lalights_member
    WHERE YEAR(member_created) = YEAR(NOW())
      AND MONTH(member_created) = MONTH(NOW())
    ) AS lb ON la.member_created = lb.member_created