I am not an expert in MySql. I am trying to split the data in my table in to ranges based on account_no. This is my table.
mysql> select * from manager;
+----+-------+------------+
| id | name | account_no |
+----+-------+------------+
| 1 | John | 5 |
| 2 | Peter | 15 |
| 3 | Tony | 18 |
| 4 | Mac | 35 |
| 5 | Max | 55 |
| 6 | Smith | 58 |
+----+-------+------------+
As you see the account_no is a positive number. I want to split these records in to batches of 10, based on account_no and display the count in that range.
For an example
between 0 and 10 there is only 1 record
between 11 and 20 there are 2 records
between 21 and 30 there are no records*(So this should be omitted.)*
etc...
Actually I am hoping to get an output like this.
+-------------+-----------+-------+
| range_start | range_end | count |
+-------------+-----------+-------+
| 1 | 10 | 1 | -> because there is 1 record between 1 and 10
| 11 | 20 | 2 | -> because there are 2 records between 11 and 20
| 31 | 40 | 1 | -> because there is 1 record between 31 and 40
| 51 | 60 | 2 | -> because there are 2 records between 51 and 60
+-------------+-----------+-------+
I tried several combinations but all of them give me only one row in the result. Can anybody help me?
My suggestion would be to create a table that contains the ranges - startRange and endRange:
CREATE TABLE range_values (`startRange` int, `endRange` int) ;
INSERT INTO range_values(`startRange`, `endRange`)
VALUES (1, 10), (11, 20), (21, 30), (31, 40), (51, 60);
Once the table is created, then you can easily join on the table to get the count.
select r.startRange,
r.endRange,
count(m.account_no) totalCount
from manager m
inner join range_values r
on m.account_no >=startrange
and m.account_no <= endrange
group by r.startRange, r.endRange
See SQL Fiddle with Demo.
The benefit of the table is that you are not coding the range values and can easily updated the ranges in a table without having to change your code.
This query return:
| STARTRANGE | ENDRANGE | TOTALCOUNT |
--------------------------------------
| 1 | 10 | 1 |
| 11 | 20 | 2 |
| 31 | 40 | 1 |
| 51 | 60 | 2 |
If you don't want to create a new table, then you can use something similar to the following:
select startrange,
endrange,
count(m.account_no) TotalCount
from manager m
inner join
(
select 1 startRange, 10 endrange union all
select 11 startRange, 20 endrange union all
select 21 startRange, 30 endrange union all
select 31 startRange, 40 endrange union all
select 41 startRange, 50 endrange union all
select 51 startRange, 60 endrange
) r
on m.account_no >=startrange
and m.account_no <= endrange
group by r.startRange, r.endRange
See SQL Fiddle with demo
This should give you the output you would like, and includes the ranges with zero in the count column.
SET #rs = 0; SELECT IF(#rs, #rs := #rs + 10, #rs := 1) AS range_start, #rs + 9 AS range_end, (SELECT COUNT(id) FROM manager WHERE account_no >= #rs AND account_no <= #rs + 9) AS `count` FROM manager;
To omit the rows with zero in the count column;
SET #rs = 0; SELECT * FROM (SELECT IF(#rs, #rs := #rs + 10, #rs := 1) AS range_start, #rs + 9 AS range_end, (SELECT COUNT(id) FROM manager WHERE account_no >= #rs AND account_no <= #rs + 9) AS `count` FROM manager) AS data WHERE `count` > 0;
Try with something like this:
SELECT
CASE
WHEN range_start < 10 THEN 'Under 10'
WHEN range_start BETWEEN 11 and 29 THEN '11 - 29'
(...more ranges...)
END as range,
COUNT(*) AS count
GROUP BY range
ORDER BY range
You should use functions similar to rank and dense_rank in MSSQL, you can implement them in MySQL starting at the following link:
http://www.folkstalk.com/2013/03/grouped-dense-rank-function-mysql-sql-query.html
Related
I have a database with questions with columns Question, Answer, Type.
Currently, this is the sql statement I am running:
SELECT Question, Answer, Type FROM goodquestions ORDER BY RAND() LIMIT 0,20
As you can see, I select random values from the table and I would like it to be that way. However, when type is 12 I would like to access the table row prior to that entry and print them out in conjunction
Like this
RANDOM
RANDOM
RANDOM
Question before 12 type
12 type question
RANDOM
RANDOM
RANDOM
It can also be like this:
Question before 12 type
12 type question
RANDOM
RANDOM
RANDOM
RANDOM
RANDOM
RANDOM
I just need them to be together and I am unable to do this right now.
I guess I see what you want.
Please try this query, I changed limit 1,20 to limit 1, 10:
select #next_line_id:=0, #next_line_type:=0;
select g.*, tt.is_property
from
(select * from
(select *,
rand() as rand_val,
case
when #next_line_type= 12 or Type = 12 then 1
else 0
end is_property,
#next_line_id as next_line_id,
#next_line_id:=id as current_id,
#next_line_type:=Type as current_type
from goodquestions order by id desc
) t
where t.Type <> 12
order by rand_val limit 0,10) tt
join goodquestions g on g.id = tt.id or (g.id = tt.next_line_id and tt.is_property = 1 and tt.Type <> 12)
group by g.id, g.Question, g.Answer, g.Type, tt.is_property
order by is_property desc, id
limit 0, 10;
The following are the query of creating test table:
create table goodquestions (
id int unsigned auto_increment primary key,
Question varchar(255) not null,
Answer varchar(255) not null,
Type int unsigned,
index idx_type (Type)
) engine=innodb DEFAULT CHARSET=latin1;
insert into goodquestions (Question, Answer, Type)
values ('q1', 'a1', 1),
('q2', 'a2', 2),
('q3', 'a3', 3),
('q4', 'a4', 4),
('q5', 'a5', 5),
('q6', 'a6', 6),
('q7', 'a7', 7),
('q8', 'a8', 8),
('q9', 'a9', 9),
('q10', 'a10', 10),
('q11', 'a11', 11),
('q12', 'a12', 12),
('q13', 'a13', 13),
('q14', 'a14', 14),
('q15', 'a15', 15),
('q16', 'a16', 16),
('q17', 'a17', 17),
('q18', 'a18', 18);
Please note, using rand() function may have a bad performance for a large
table. If there are performance issue, I could provide another solution for better performance.
The following query which result list must have and only have one record of type 12:
select #total_type_12:=(select count(*) from goodquestions where Type=12);
select #random_type_12:=(floor(rand()*#total_type_12) + 1) * 2;
select #next_line_id:=0, #next_line_type:=0, #is_property:=0;
select g.*, tt.is_property
from
(select * from
(select *,
case
when (#next_line_type= 12 or Type = 12) and #random_type_12 > 0 and #random_type_12 <= 2 then #is_property:=1
else #is_property:=0
end is_property,
rand() as rand_val,
#random_type_12 as cur_random_type_counter,
case
when (#next_line_type= 12 or Type = 12) and #random_type_12 > 0 then #random_type_12:=#random_type_12-1
else #random_type_12
end as next_rand_type_counter,
#next_line_id as next_line_id,
#next_line_id:=id as current_id,
#next_line_type:=Type as current_type
from goodquestions order by id desc
) t
where t.Type <> 12
order by is_property desc, rand_val limit 0,10) tt
join goodquestions g on g.id = tt.id or (g.id = tt.next_line_id and tt.is_property = 1 and tt.Type <> 12)
group by g.id, g.Question, g.Answer, g.Type, tt.is_property
order by is_property desc, id
limit 0, 10;
Test data set is as following:
mysql> select * from goodquestions;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 84
Current database: test
+----+----------+--------+------+
| id | Question | Answer | Type |
+----+----------+--------+------+
| 1 | q1 | a1 | 1 |
| 2 | q2 | a2 | 2 |
| 3 | q3 | a3 | 3 |
| 4 | q4 | a4 | 4 |
| 5 | q5 | a5 | 5 |
| 6 | q6 | a6 | 6 |
| 7 | q7 | a7 | 7 |
| 8 | q8 | a8 | 8 |
| 9 | q9 | a9 | 9 |
| 10 | q10 | a10 | 10 |
| 11 | q11 | a11 | 11 |
| 12 | q12 | a12 | 12 |
| 13 | q13 | a13 | 13 |
| 14 | q14 | a14 | 14 |
| 15 | q15 | a15 | 15 |
| 16 | q16 | a16 | 16 |
| 17 | q17 | a17 | 17 |
| 18 | q18 | a18 | 18 |
| 19 | q21 | a21 | 12 |
| 20 | q22 | a22 | 22 |
| 21 | q23 | a23 | 12 |
+----+----------+--------+------+
21 rows in set (0.34 sec)
For MySQL 8+ it can be something similar to
WITH
-- SELECT 20 random rows
cte AS ( SELECT Question, Answer, Type
FROM goodquestions
ORDER BY RAND() LIMIT 0,20 )
( SELECT Question, Answer, Type
FROM cte )
-- add pre-row if Type=12 row is selected and pre-row is not selected
UNION DISTINCT
( SELECT Question, Answer, Type
FROM goodquestions
WHERE Type = 'pre-type for type 12'
AND EXISTS ( SELECT NULL
FROM cte
WHERE Type = 12 ) )
-- sort placing pre-row and type=12 row at the top
ORDER BY Type = 'pre-type for type 12' DESC,
Type = 12 DESC,
RAND()
-- remove excess row if Type=12 row was selected in CTE
-- and pre-row was not selected in CTE but added in UNION
LIMIT 0, 20
The query assumes that goodquestions.Type is unique.
This is example of my table :
+-----+-----+------------+--------+-------------+--------------+
| LID | AID | Created | TypeID | PaymentDate | PaymentValue |
+-----+-----+------------+--------+-------------+--------------+
| 1 | 529 | 2017-05-12 | 1 | 2017-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 2 | 529 | 2018-04-10 | 4 | 2018-04-10 | 200 |
+-----+-----+------------+--------+-------------+--------------+
| 3 | 441 | 2014-01-23 | 3 | 2014-01-23 | 300 |
+-----+-----+------------+--------+-------------+--------------+
| 4 | 324 | 2017-09-14 | 1 | 2017-09-14 | 400 |
+-----+-----+------------+--------+-------------+--------------+
| 5 | 111 | 2018-05-12 | 0 | 2018-05-12 | 340 |
+-----+-----+------------+--------+-------------+--------------+
| 6 | 529 | 2018-05-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 7 | 529 | 2018-06-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 8 | 529 | 2018-07-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 9 | 529 | 2018-08-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 10 | 529 | 2018-09-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 11 | 529 | 2018-01-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 12 | 529 | 2018-05-14 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 13 | 529 | 2018-05-21 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 14 | 529 | 2018-03-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
Here another table
+-----+-------+
| ID |caption|
+-----+-------+
| 0 | bad |
+-----+-------+
| 1 | good |
+-----+-------+
I need to get 10 latest records per AID. If there less than 10 records for some AID anyway i need to get ten rows and put "No payment date" into PaymentDate and Created fields, Null into TypeID and 0 into PaymentValue. I can get 10 or less latest records with
select *
from (select *,
(#rn := if(#c = AID, #rn + 1,
if(#c := AID, 1, 1)
)
) as rn
from history cross join
(select #rn := 0, #c := -1) params
order by AID, Created desc
) t
having rn <= 10;
But i dont know how force mysql to output 10 rows for each AID. Help me please.
Result should be in a form
AID,TypeId,Created,Caption
I have done it.
This query needs to create a row of 10 records to combine with distinct AID valies in the table. I was able to show the result for Amount and Create date and will leave it to you to continue since you will get the idea.
The critical part is to build a table with 10 rows times distinct AID so about 40 rows in table r. Then do a left join to table t which is similar to what you have done. Table t gets a rank of at most 10 records. Any missing rank up to 10 recs will be filled by table r. Coalesce will assign the default values such as 0 fro amount and 'no create date' for date.
http://sqlfiddle.com/#!9/855c21/2
SELECT coalesce(r.aid, t.aid) as aid,
coalesce(t.paymentvalue, 0) as paymentvalue,
coalesce(cast(t.created as char), 'no create date') as created
FROM (select * from (
select 1 as rw union
select 2 union select 3
union select 4 union select 5
union select 6 union select 7
union select 8 union select 9
union select 10) u
cross join (select distinct aid
from history) h
) as r
LEFT JOIN (
SELECT a.aid, a.paymentvalue,
a.created, count(*) rn
FROM history a
JOIN history b
ON a.aid = b.aid
AND a.created <= b.created
GROUP BY a.aid, a.created
HAVING COUNT(*) <= 10) t
on r.rw=t.rn and r.aid=t.aid
order by aid, created;
I have added RIGHT JOIN to bring in the null rows to top up to 10 (or n) rows per AID. Initially I use SELECT 1 UNION SELECT 2 ... to generate the 10 rows. In order to make it easier to increase the number of rows (say 100), I am trying this idea of generate_series equivalent for mysql. In order for this to work, the number of rows in history table must be equal to greater than the number of rows required per AID.
select t1.lid
,t2.aid
,coalesce(t1.created, "no created date") as created
,t1.typeID
,coalesce(t1.paymentdate, "no payment date") as paymentDate
,coalesce(t1.paymentvalue, 0) as paymentValue
,t2.rn
from
(
select *,
(#rn := if(#c = AID, #rn + 1,
if(#c := AID, 1, 1)
)
) as rn
from history cross join
(select #rn := 0, #c := -1) params
order by AID, Created desc
) t1
right join
( select *
from (select distinct aid from history ) h1
cross join
(select rn -- generate table with n rows numbered from 1 to n
from
(select
#num:= 0) init
cross join
(select #num := #num +1 rn
from history ) t -- assume history has at least 10 rows
limit
10 ) h2 -- n = 10; change it to the number of rows per aid required
) t2
on t1.aid = t2.aid and t1.rn = t2.rn
order by t2.aid, t2.rn
ID Timestamp Value
1 11:59.54 10
1 12.04.00 20
1 12.12.00 31
1 12.16.00 10
1 12.48.00 05
I want the result set as
ID Timestamp Value
1 11:59.54 10
1 12:00:00 10
1 12.04.00 20
1 12.12.00 31
1 12:15:00 31
1 12:16.00 10
1 12:30:00 10
1 12:45:00 10
1 12.48.00 05
More coffee will probably lead to a simpler solution, but consider the the following...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,timestamp TIMESTAMP
,value INT NOT NULL
);
INSERT INTO my_table VALUES
(1 ,'11:59:54',10),
(2 ,'12:04:00',20),
(3 ,'12:12:00',31),
(4 ,'12:16:00',10),
(5 ,'12:48:00',05);
... in addition, I have a table of integers, that looks like this:
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
So...
SELECT a.timestamp
, b.value
FROM
( SELECT x.*
, MIN(y.timestamp) min_timestamp
FROM
( SELECT timestamp
FROM my_table
UNION
SELECT SEC_TO_TIME((i2.i*10+i1.i)*900)
FROM ints i1
, ints i2
WHERE SEC_TO_TIME((i2.i*10+i1.i)*900)
BETWEEN (SELECT MIN(timestamp) FROM my_table)
AND (SELECT MAX(timestamp) FROM my_table)
ORDER
BY timestamp
) x
LEFT
JOIN my_table y
ON y.timestamp >= x.timestamp
GROUP
BY x.timestamp
) a
JOIN my_table b
ON b.timestamp = min_timestamp;
+-----------+-------+
| timestamp | value |
+-----------+-------+
| 11:59:54 | 10 |
| 12:00:00 | 20 |
| 12:04:00 | 20 |
| 12:12:00 | 31 |
| 12:15:00 | 10 |
| 12:16:00 | 10 |
| 12:30:00 | 5 |
| 12:45:00 | 5 |
| 12:48:00 | 5 |
+-----------+-------+
The idea is as follows. Use SERIES_GENERATE() to generate the missing time stamps with the 15 minute intervals and and union it with the existing data your table T. Now you would want to use LAST_VALUE with IGNORE NULLS. IGNORE NULLS is not implemented in HANA, therefore you have to do a bit of a workaround. I use COUNT() as a window function to count the non null values. I do the same on the original data and then join both on the count. This way I repeat the last non-null value.
select X.ID, X.TIME, Y.VALUE from (
select ID, TIME, value,
count(VALUE) over (order by TIME rows between unbounded preceding and current row) as CNT
from (
--add the missing 15 minute interval timestamps
select 1 as ID, GENERATED_PERIOD_START as TIME, NULL as VALUE
from SERIES_GENERATE_TIME('INTERVAL 15 MINUTE', '12:00:00', '13:00:00')
union all
select ID, TIME, VALUE from T
)
) as X join (
select ID, TIME, value,
count(value) over (order by TIME rows between unbounded preceding and current row) as CNT
from T
) as Y on X.CNT = Y.CNT
I have the next table:
id| C1 | C2 | C3 | C4 | C5|
----------------------------
01| 23 | 19 | 30 | 30 | 30|
---------------------------
02| 23 | 40 | 30 | 30 | 30|
----------------------------
03| 23 | 20 | 19 | 30 | 30|
----------------------------
04| 23 | 19 | 30 | 30 | 30|
----------------------------
05| 23 | 23 | 23 | 19 | 30|
----------------------------
in this table the count of number 19 is 4
I need a query to count how many times each number appears in the table, searching in many rows and columns, or at least number 19 with MySQL.
I'd union all the columns, and then just count the values:
SELECT c, COUNT(*)
FROM (SELECT c1 AS c FROM my_table
UNION ALL
SELECT c2 FROM my_table
UNION ALL
SELECT c3 FROM my_table
UNION ALL
SELECT c4 FROM my_table
UNION ALL
SELECT c5 FROM my_table) t
GROUP BY c
One option is to use IF with SUM:
SELECT SUM(IF(id=19,1,0) + IF(c1=19,1,0) + IF(c2=19,1,0) +
IF(c3=19,1,0) + IF(c4=19,1,0) + IF(c5=19,1,0))
FROM YourTable
Condensed SQL Fiddle Demo
As pointed out, the IF statement isn't actually needed:
SELECT SUM((c1=19)+(c2=19)+(c3=19)+(c4=19))
FROM YourTable
select count(*) FROM tablename where c1 = 19 OR c2 = 19 or c3 = 19 etc
I prefer the SUM method:
SELECT SUM(
(t1.c1=x.num)
+ (t1.c2=x.num)
+ (t1.c3=x.num)
+ (t1.c4=x.num)
+ (t1.c5=x.num)
) count
FROM table1 t1
JOIN (SELECT 19 num) x
I've got MySQL table
CREATE TABLE cms_webstat (
ID int NOT NULL auto_increment PRIMARY KEY,
TIMESTAMP_X timestamp DEFAULT CURRENT_TIMESTAMP,
# ... some other fields ...
)
which contains statistics about site visitors.
For getting visits per hour I use
SELECT
hour(TIMESTAMP_X) as HOUR
, count(*) AS HOUR_STAT
FROM cms_webstat
GROUP BY HOUR
ORDER BY HOUR DESC
which gives me
| HOUR | HOUR_STAT |
| 24 | 15 |
| 23 | 12 |
| 22 | 9 |
| 20 | 3 |
| 18 | 2 |
| 15 | 1 |
| 12 | 3 |
| 9 | 1 |
| 3 | 5 |
| 2 | 7 |
| 1 | 9 |
| 0 | 12 |
And I'd like to get following:
| HOUR | HOUR_STAT |
| 24 | 15 |
| 23 | 12 |
| 22 | 9 |
| 21 | 0 |
| 20 | 3 |
| 19 | 0 |
| 18 | 2 |
| 17 | 0 |
| 16 | 0 |
| 15 | 1 |
| 14 | 0 |
| 13 | 0 |
| 12 | 3 |
| 11 | 0 |
| 10 | 0 |
| 9 | 1 |
| 8 | 0 |
| 7 | 0 |
| 6 | 0 |
| 5 | 0 |
| 4 | 0 |
| 3 | 5 |
| 2 | 7 |
| 1 | 9 |
| 0 | 12 |
How should I modify the query to get such result (with one mysql query, without creating temporary tables)?
Is it possible to get such result with one MySQL query?
Create another table with a single column,
CREATE TABLE hours_list (
hour int NOT NULL PRIMARY KEY
)
Fill it with all 24 hours.
Then do a join on that table to fill in the zeroes.
SELECT
hs.hour as HOUR, COUNT(ws.ID) AS HOUR_STAT
FROM hours_list hs
LEFT JOIN cms_webstat ws ON hs.hour = hour(ws.TIMESTAMP_X)
GROUP BY hs.hour
ORDER BY hs.hour DESC
This is just the 'why it is not returning` part. Marcus' answer covers the 'how to' part.
The SQL
SELECT
hour(TIMESTAMP_X) as HOUR
, count(*) AS HOUR_STAT
FROM cms_webstat
GROUP BY HOUR
ORDER BY HOUR DESC
gets the count of the records per hour, for the timestamps present in the table
It does not give the details of what is not present in the table. Since there is no recors for the timestamp corresponding to the hour 8 (from your example) the SQL does not return any records.
I've finaly found the answer.
Maybe I'm insane, but this works.
SELECT HOUR, max(HOUR_STAT) as HOUR_STAT FROM (
(
SELECT HOUR(TIMESTAMP_X) as HOUR, count(*) as HOUR_STAT
FROM cms_webstat
WHERE date(TIMESTAMP_X) = date(now())
)
UNION (SELECT 0 as HOUR, 0)
UNION (SELECT 1 as HOUR, 0)
UNION (SELECT 2 as HOUR, 0)
UNION (SELECT 3 as HOUR, 0)
UNION (SELECT 4 as HOUR, 0)
UNION (SELECT 5 as HOUR, 0)
UNION (SELECT 6 as HOUR, 0)
UNION (SELECT 7 as HOUR, 0)
UNION (SELECT 8 as HOUR, 0)
UNION (SELECT 9 as HOUR, 0)
UNION (SELECT 10 as HOUR, 0)
UNION (SELECT 11 as HOUR, 0)
UNION (SELECT 12 as HOUR, 0)
UNION (SELECT 13 as HOUR, 0)
UNION (SELECT 14 as HOUR, 0)
UNION (SELECT 15 as HOUR, 0)
UNION (SELECT 16 as HOUR, 0)
UNION (SELECT 17 as HOUR, 0)
UNION (SELECT 18 as HOUR, 0)
UNION (SELECT 19 as HOUR, 0)
UNION (SELECT 20 as HOUR, 0)
UNION (SELECT 21 as HOUR, 0)
UNION (SELECT 22 as HOUR, 0)
UNION (SELECT 23 as HOUR, 0)
)
AS `combined_table`
GROUP BY HOUR
ORDER BY HOUR DESC
One MySQL query as desired.
$sql = 'SELECT g, MAX(v) AS v, MAX(c) AS c FROM (';
$sql .= '(SELECT DATE_FORMAT(viewed, \'%d.%m.%Y\') AS g, COUNT(1) AS v, 0 AS c FROM '.$this->prefix.'view WHERE campaignid IN ('.join(', ',$ids).') GROUP BY g)';
$sql .= ' UNION (SELECT DATE_FORMAT(clicked, \'%d.%m.%Y\') AS g, 0 AS v, COUNT(1) AS c FROM '.$this->prefix.'clicks WHERE campaignid IN ('.join(', ',$ids).') GROUP BY g)';
$today = strtotime("00:00:00");
for ($i=$today; $i>=time()-30*86400; $i-=86400) {
$sql .= ' UNION (SELECT \''.date('d.m.Y',$i).'\' AS g, 0 AS v, 0 AS c)';
}
$sql .= ') AS tmp GROUP BY g ORDER BY g DESC';
$chart = DB::getAll($sql);
p($chart);
Thanks! Made it! From 2 tables, clicks and views, joined.. works. ajaxel.com