4

我正在努力实现动态 Highcharts 图表 - 基本列,我需要帮助在 MySQL 中制作 SQL。我需要从当前月份开始的过去 12 个月的结果(无论该月的任何数据(它可以是 0 - 但应该获取所有 12 个月的记录)),显示有多少成员(4 种类型的用户)注册了特定月份的网站。

有 4 种类型的用户:

  1. 代理
  2. 个人
  3. 建筑商
  4. 房地产公司

对于 Months 列,它应该检索当前月份的最后 12 个月 - 八月、九月、十月、十一月、十二月、一月、二月、三月、四月、五月、六月、七月。

我尝试过以下查询:

SELECT
CASE WHEN u.`userTypeID`=1 THEN COUNT(`userTypeID`) ELSE 0 END AS agent,
CASE WHEN u.`userTypeID`=2 THEN COUNT(`userTypeID`) ELSE 0 END AS individuals,
CASE WHEN u.`userTypeID`=3 THEN COUNT(`userTypeID`) ELSE 0 END AS builders,
CASE WHEN u.`userTypeID`=4 THEN COUNT(`userTypeID`) ELSE 0 END AS real_estate_companies,
 u.`userRegistredDate` AS 'timestamp'
FROM `dp_users` AS u
LEFT JOIN `dp_user_types` AS ut ON u.`userTypeID` = ut.`type_id`
WHERE u.`userRegistredDate` < Now( )
AND u.`userRegistredDate` > DATE_ADD( Now( ) , INTERVAL -12 MONTH )
GROUP BY DATE_FORMAT( u.`userRegistredDate`, '%b' )

输出(不正确):

| AGENT | INDIVIDUALS | BUILDERS | REAL_ESTATE_COMPANIES |                   TIMESTAMP |
----------------------------------------------------------------------------------------
|     0 |           0 |        9 |                     0 | July, 01 2013 17:14:35+0000 |
|     3 |           0 |        0 |                     0 |  May, 15 2013 14:14:26+0000 |

输出(必填:正确):

| AGENT | INDIVIDUALS | BUILDERS | REAL_ESTATE_COMPANIES |                   TIMESTAMP |
----------------------------------------------------------------------------------------
|     3 |           2 |        2 |                     2 | July, 01 2013 17:14:35+0000 |
|     1 |           2 |        0 |                     0 |  May, 15 2013 14:14:26+0000 |

我尝试的另一种方法是使用子查询,请在下面找到两个示例链接:

http://sqlfiddle.com/#!2/ed101/53 http://sqlfiddle.com/#!2/ed101/54

希望能找到好的解决办法,谢谢。

4

3 回答 3

1

尝试这个

    SELECT month(u.`userRegistredDate`),
sum(CASE WHEN u.`userTypeID`=1 THEN 1 ELSE 0 END) AS agent,
sum(CASE WHEN u.`userTypeID`=2 THEN 1 ELSE 0 END) AS individuals,
sum(CASE WHEN u.`userTypeID`=3 THEN 1 ELSE 0 END) AS builders,
sum(CASE WHEN u.`userTypeID`=4 THEN 1 ELSE 0 END) AS real_estate_companies,
 u.`userRegistredDate` AS 'timestamp',m.month
FROM (
SELECT 'January' AS
MONTH
UNION SELECT 'February' AS
MONTH
UNION SELECT 'March' AS
MONTH
UNION SELECT 'April' AS
MONTH
UNION SELECT 'May' AS
MONTH
UNION SELECT 'June' AS
MONTH
UNION SELECT 'July' AS
MONTH
UNION SELECT 'August' AS
MONTH
UNION SELECT 'September' AS
MONTH
UNION SELECT 'October' AS
MONTH
UNION SELECT 'November' AS
MONTH
UNION SELECT 'December' AS
MONTH
) AS m
left join `dp_users` AS u  ON m.month = MONTHNAME(u.`userRegistredDate`) and u.`userRegistredDate` < Now( )
AND u.`userRegistredDate` > DATE_ADD( Now( ) , INTERVAL -12 MONTH )
LEFT JOIN `dp_user_types` AS ut ON u.`userTypeID` = ut.`type_id`

GROUP BY  m.month
order by FIELD(m.month,'July','August','September','October','November','December','January','February','March','April','May','June')

请查看此链接

于 2013-07-31T08:47:27.863 回答
0

这就是你想要的:

SELECT
sum(CASE WHEN u.`userTypeID`=1 THEN 1 ELSE 0 END) AS agent,
sum(CASE WHEN u.`userTypeID`=2 THEN 1 ELSE 0 END) AS individuals,
sum(CASE WHEN u.`userTypeID`=3 THEN 1 ELSE 0 END) AS builders,
sum(CASE WHEN u.`userTypeID`=4 THEN 1 ELSE 0 END) AS real_estate_companies,
 u.`userRegistredDate` AS 'timestamp'
FROM `dp_users` AS u
LEFT JOIN `dp_user_types` AS ut ON u.`userTypeID` = ut.`type_id`
WHERE u.`userRegistredDate` < Now( )
AND u.`userRegistredDate` > DATE_ADD( Now( ) , INTERVAL -12 MONTH )
GROUP BY DATE_FORMAT( u.`userRegistredDate`, '%b' )

http://sqlfiddle.com/#!2/ed101/69

于 2013-07-31T08:52:18.683 回答
0

我认为你错过了你的 group by 子句。userTypeID柱子。尝试将 group by 子句放在特定列上。我认为您正在寻找这样的查询:

select max (A) A, max(B) B, max (C) C, max(D) D, max (E) date from
(
select distinct
        case when t1.usertype=1 then count(t2.username) else 0 end as "A",
        case when t1.usertype=2 then count(t2.username) else 0 end as "B",
        case when t1.usertype=3 then count(t2.username) else 0 end as "C",
        case when t1.usertype=4 then count(t2.username) else 0 end as "D",
        month(u.`userRegistredDate`) E
from
    #tab1 t1
left 
    join #tab2 t2 on t1.usertype = t2.usertype
group by  month(u.`userRegistredDate`), t1.usertype
) tab
group by E
于 2013-07-31T08:13:12.787 回答