You can use conditional sum for this calculation something as
select
date_format(created,'%d-%m') as created_date ,
sum( case when status='open' then 1 else 0 end )+
sum( case when status='new' then 1 else 0 end )+
sum( case when status='resolved' and date(resolved) > date(created) then 1 else 0 end ) as open
from table_name
where
YEAR(Created) = '2015'
group by created_date ;
Here is a test case
mysql> select * from test ;
+------+---------------------+---------------------+----------+
| id | created | resolved | status |
+------+---------------------+---------------------+----------+
| 1 | 2015-05-10 00:00:00 | 1970-01-01 00:00:00 | open |
| 2 | 2015-05-10 00:00:00 | 1970-01-01 00:00:00 | new |
| 3 | 2015-05-10 00:00:00 | 2015-05-12 00:00:00 | resolved |
| 4 | 2015-05-11 00:00:00 | 1970-01-01 00:00:00 | open |
| 5 | 2015-05-11 00:00:00 | 1970-01-01 00:00:00 | new |
| 6 | 2015-05-11 00:00:00 | 2015-05-11 00:00:00 | resolved |
+------+---------------------+---------------------+----------+
6 rows in set (0.00 sec)
mysql> select
-> date_format(created,'%d-%m') as created_date ,
-> sum( case when status='open' then 1 else 0 end )+
-> sum( case when status='new' then 1 else 0 end )+
-> sum( case when status='resolved' and date(resolved) > date(created) then 1 else 0 end ) as open
-> from test
-> where
-> YEAR(Created) = '2015'
-> group by created_date ;
+--------------+------+
| created_date | open |
+--------------+------+
| 10-05 | 3 |
| 11-05 | 2 |
+--------------+------+
2 rows in set (0.00 sec)
UPDATE
The count of open is a incremental as per the comment. So the following should do the trick
select
created_date,
@open:= @open+open_ticket-resolved as open
from(
select
date_format(created,'%d-%m') as created_date ,
sum( case when status='open' then 1 else 0 end )+
sum( case when status='new' then 1 else 0 end )+
sum( case when status='resolved' and ( date(resolved) > date(created) or date(resolved) = date(created)) then 1 else 0 end ) as open_ticket,
sum(case when status = 'resolved' and date(resolved) = date(created) then 1 else 0 end) as resolved
from test
where
YEAR(Created) = '2015'
group by created_date
)x,(select @open:=0)r order by created_date ;
From the above sample data it will have
+--------------+------+
| created_date | open |
+--------------+------+
| 10-05 | 3 |
| 11-05 | 5 |
+--------------+------+
UPDATE:
Count the previous tickets which got resolved on a given date and deduct that from the current day open ticket, for that you need a left join
something as
select
created_date,
@open:= @open+open_ticket-resolved_ticket as open
from(
select
date_format(t.created,'%d-%m') as created_date ,
sum( case when t.status='open' then 1 else 0 end )+
sum( case when t.status='new' then 1 else 0 end )+
sum( case when t.status='resolved' and ( date(t.resolved) > date(created) or date(t.resolved) = date(created)) then 1 else 0 end ) as open_ticket,
coalesce(tot,0) as resolved_ticket
from test t left join(
select count(*) as tot, date(resolved) as resolved from test where status='resolved' group by date(resolved)
)x on date(x.resolved) = date(t.created)
where
YEAR(t.created) = '2015'
group by created_date
)x,(select @open:=0)r order by created_date ;
OUTPUT :
+--------------+------+
| created_date | open |
+--------------+------+
| 10-05 | 3 |
| 11-05 | 5 |
| 12-05 | 7 |
+--------------+------+
Given this sample data:
CREATE TABLE #d ( column1 char(2), column2 varchar(32) );
INSERT #d (column1, column2)
VALUES ('x1', 'temp/page1_l'),
('x1', 'temp/page2_f'),
('x2', 'temp/page2_d'),
('x2', 'temp/page1_k'),
('x2', 'temp/page2_e');
One way to solve it is by taking the count separately:
;WITH agg AS
(
SELECT column1, col1count = COUNT(*)
FROM #d
GROUP BY column1
)
SELECT [count(column1)] = agg.col1count, filt.column2
FROM agg INNER JOIN #d AS filt
ON agg.column1 = filt.column1
WHERE filt.column2 LIKE '%page1[_]%';
Or slightly differently:
;WITH d AS
(
SELECT column1, column2,
column1count = COUNT(*) OVER (PARTITION BY column1)
FROM #d
)
SELECT [count(column1)] = column1count, column2
FROM d
WHERE column2 LIKE '%page1[_]%';
Another is along the lines of Rob's suggestion:
SELECT [count(column1)] = COUNT(column1),
column2 = MIN(CASE WHEN column2 LIKE '%page1[_]%' THEN column2 END)
FROM #d
GROUP BY column1;
Best Answer
Use
fiddle
for to obtain needed statistic in vertical form. If you need pivotted output strictly then use CROSSTAB() or similar, or emulate it using 4 CASEs.