我正在努力实现动态Highcharts图 - 基本列,我需要帮助在MySQL中创建SQL。我需要过去12个月的结果(无论本月的任何数据(可以是0 - 但是所有12个月的记录都应该被提取))从当前月份显示有多少成员(4种类型的用户)已注册特定月份的网站。
有4种类型的用户:
对于Months列,它应该从当前月份检索最近12个月 - 8月,9月,10月,11月,12月,1月,2月,3月,4月,5月,6月,7月。
我尝试过以下查询:
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
希望找到有利的解决方案,谢谢。
答案 0 :(得分: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')
请查看此link
答案 1 :(得分:0)
我认为你错过了u。userTypeID
专栏的group by子句。尝试将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
答案 2 :(得分: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' )