mysql - Grouping in an SQL Query -
this question has answer here:
- complex sql query suggestions please 1 answer
i have 3 tables schema below:
please check sqlfiddle: http://sqlfiddle.com/#!2/55fc2
table: apps
| id (bigint) | userid (bigint)| start_time (datetime) | ------------------------------------------------------------- | 1 | 13 | 2013-05-03 04:42:55 | | 2 | 13 | 2013-05-12 06:22:45 | | 3 | 13 | 2013-06-12 08:44:24 | | 4 | 13 | 2013-06-24 04:20:56 | | 5 | 13 | 2013-06-26 08:20:26 | | 6 | 13 | 2013-09-12 05:48:27 | table: hosts
| id (bigint) | appid (bigint)| device_id (bigint) | ------------------------------------------------------------- | 1 | 1 | 1 | | 2 | 2 | 1 | | 3 | 1 | 1 | | 4 | 3 | 3 | | 5 | 1 | 4 | | 6 | 2 | 3 | table: usage
| id (bigint) | appid (bigint)| hostid (bigint) | factor (varchar) | ------------------------------------------------------------------------------------- | 1 | 1 | 1 | low | | 2 | 1 | 3 | high | | 3 | 2 | 2 | low | | 4 | 3 | 4 | medium | | 5 | 1 | 5 | low | | 6 | 2 | 2 | medium | now if put userid, want count of rows of table rows each month (of app) each "factor" month wise last 6 months.
if device_id appears more once in month (based on start_time, based on joining apps , hosts), latest rows of usage (based on combination of apps, hosts , usage) considered calculating count.
example output of query above example should be: (for input user id=13)
| month | usage_count | factor | ------------------------------------------------------------- | 5 | 0 | high | | 6 | 0 | high | | 7 | 0 | high | | 8 | 0 | high | | 9 | 0 | high | | 10 | 0 | high | | 5 | 2 | low | | 6 | 0 | low | | 7 | 0 | low | | 8 | 0 | low | | 9 | 0 | low | | 10 | 0 | low | | 5 | 1 | medium | | 6 | 1 | medium | | 7 | 0 | medium | | 8 | 0 | medium | | 9 | 0 | medium | | 10 | 0 | medium | how calculated?
- for month may 2013 (05-2013), there 2 apps table apps
- in table hosts , these apps associated device_id's 1,1,1,4,3
- for month (05-2013) device_id=1, latest value of start_time is: 2013-05-12 06:22:45 (from tables hosts,apps), in table usage, combination of appid=2&hostid=2 there 2 rows 1 factor low , other medium,
- for month (05-2013) device_id=4, following same procedure 1 entry i.e 0 low
- similarly values calculated.
to last 6 months via query i'm trying following:
select month(date_add(now(), interval aint month)) amonth ( select 0 aint union select -1 union select -2 union select -3 union select -4 union select -5 )
you need understand use of group by. thats looking for. can group results independent groups , use count , aggregation functions on groups independently. here basic tutorial.
Comments
Post a Comment