How to get open and close time slots in MYSQL - mysql

I have sample data like this :
ID Val Name Dt Status
1, 145, 'Test1', '2020-01-28 02:18:00', 'open'
2, 145, 'Test1', '2020-01-28 04:10:00', 'open'
3, 145, 'Test1', '2020-01-28 05:50:00', 'open'
4, 145, 'Test1', '2020-01-28 05:56:00', 'close'
5, 145, 'Test2', '2020-01-28 07:36:00', 'open'
6, 145, 'Test2', '2020-01-28 07:42:00', 'open'
7, 145, 'Test2', '2020-01-28 07:44:00', 'open'
8, 145, 'Test2', '2020-01-28 07:47:00', 'close'
How can i get the output like this :
ID Val Name o_Dt o_gate c_Dt c_gate
1, 145, 'Test1', '2020-01-28 02:18:00', 'open' NULL NULL
2, 145, 'Test1', '2020-01-28 04:10:00', 'open' NULL NULL
3, 145, 'Test1', '2020-01-28 05:50:00', 'open' '2020-01-28 05:56:00', 'close'
4, 145, 'Test2', '2020-01-28 07:36:00', 'open' NULL NULL
5, 145, 'Test2', '2020-01-28 07:42:00', 'open' NULL NULL
6, 145, 'Test2', '2020-01-28 07:44:00', 'open' '2020-01-28 07:47:00', 'close'
I Have tried with different scenarios but not moving forward Using
COALESCE(LAG(Status) OVER (ORDER BY dt)
ROW_NUMBER()OVER(PARTITION BY vehicle_id,status )
Not getting exact result . Can anyone suggest on this .
Previously I have asked question for same data but haven't got exact answer .
How to transpose rows to columns based on time intervals in MYSQL

SELECT t1.ID, t1.Val, t1.Name, t1.Dt o_Dt, t1.Status o_gate, t2.Dt c_Dt, t2.Status c_gate
FROM test t1
LEFT JOIN test t2 ON t1.Dt < t2.Dt
AND t1.Name = t2.Name
AND t2.Status = 'close'
AND NOT EXISTS ( SELECT NULL
FROM test t3
WHERE t1.Dt < t3.Dt
AND t3.Dt < t2.Dt
AND t1.Name = t3.Name)
WHERE t1.Status = 'open'
fiddle

Try this Below Code
;WITH CTE(ID ,Val,Name,Dt,[Status])
AS
(
SELECT 1, 145, 'Test1', '2020-01-28 02:18:00', 'open'
UNION ALL SELECT 2, 145, 'Test1', '2020-01-28 04:10:00', 'open'
UNION ALL SELECT 3, 145, 'Test1', '2020-01-28 05:50:00', 'open'
UNION ALL SELECT 4, 145, 'Test1', '2020-01-28 05:56:00', 'close'
UNION ALL SELECT 5, 145, 'Test2', '2020-01-28 07:36:00', 'open'
UNION ALL SELECT 6, 145, 'Test2', '2020-01-28 07:42:00', 'open'
UNION ALL SELECT 7, 145, 'Test2', '2020-01-28 07:44:00', 'open'
UNION ALL SELECT 8, 145, 'Test2', '2020-01-28 07:47:00', 'close'
UNION ALL SELECT 9, 145, 'Test3', '2020-01-28 11:42:00', 'open'
UNION ALL SELECT 10, 145, 'Test3', '2020-01-28 14:44:00', 'open'
UNION ALL SELECT 11, 145, 'Test3', '2020-01-28 15:56:00', 'close'
)
SELECT ROW_NUMBER()OVER(ORDER BY (SELECT NULL)) AS ID,*
FROM
(
SELECT Val,Name,[Ogate],[O_Dt],LEAD([C_Dt])OVER(PARTITION BY Name ORDER BY ID)AS [C_Dt] ,LEAD([C_gate])OVER(PARTITION BY Name ORDER BY ID) AS [C_gate]
FROM CTE c
CROSS APPLY (VALUES ( CASE WHEN [Status] = 'open' THEN [Status] END,
CASE WHEN [Status] = 'open' THEN [Dt] END,
CASE WHEN [Status] = 'close' THEN [Status] END,
CASE WHEN [Status] = 'close' THEN [Dt] END
)) AS Dt ([Ogate],[O_Dt],[C_gate],[C_Dt])
)Dt
WHERE COALESCE (Ogate,O_Dt,C_Dt,C_gate,'1') <> '1'
Result
ID Val Name Ogate O_Dt C_Dt C_gate
---------------------------------------------------------------------------
1 145 Test1 open 2020-01-28 02:18:00 NULL NULL
2 145 Test1 open 2020-01-28 04:10:00 NULL NULL
3 145 Test1 open 2020-01-28 05:50:00 2020-01-28 05:56:00 close
4 145 Test2 open 2020-01-28 07:36:00 NULL NULL
5 145 Test2 open 2020-01-28 07:42:00 NULL NULL
6 145 Test2 open 2020-01-28 07:44:00 2020-01-28 07:47:00 close
7 145 Test3 open 2020-01-28 11:42:00 NULL NULL
8 145 Test3 open 2020-01-28 14:44:00 2020-01-28 15:56:00 close

Related

how can I avoid column which contain null or zero value [duplicate]

This question already has an answer here:
Can you help to modify the query or other query for getting the expected result
(1 answer)
Closed 5 years ago.
I want to avoid those column which contain null or zero value
here the table structure
-- Table structure for table orders
CREATE TABLE `orders` (
`id` int(11) NOT NULL,
`customer_id` int(11) NOT NULL,
`restaurant_id` int(11) NOT NULL,
`source_id` int(1) NOT NULL,
`purchase_method` varchar(255) NOT NULL,
`total_price` int(11) NOT NULL,
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `orders`
--
INSERT INTO `orders` (`id`, `customer_id`, `restaurant_id`, `source_id`, `purchase_method`, `total_price`, `date_created`) VALUES
(1, 1, 1, 3, 'Cash', 500, '2016-05-31 11:44:16'),
(2, 1, 1, 3, 'Cash', 1500, '2016-06-01 11:44:22'),
(3, 1, 1, 3, '', 650, '2016-06-02 11:44:26'),
(4, 1, 1, 2, 'cash', 1500, '2016-06-03 11:44:31'),
(5, 1, 1, 1, 'cash', 12000, '2016-06-04 21:08:00'),
(6, 1, 1, 1, 'cash', 14500, '2016-06-05 00:00:00'),
(7, 1, 1, 2, 'cash', 15000, '2016-06-10 09:47:15'),
(8, 1, 1, 2, 'cash', 14500, '2016-05-10 10:03:55'),
(9, 1, 1, 1, 'cash', 11800, '2016-06-08 00:00:00'),
(10, 1, 1, 2, 'ss', 300, '2016-06-08 01:06:56'),
(11, 1, 1, 1, 'online', 400, '2016-05-10 10:03:20'),
(12, 1, 1, 3, 'cash', 5000, '2016-06-09 06:23:16'),
(13, 1, 1, 2, 'cash', 2000, '2016-05-10 10:03:35'),
(14, 1, 1, 1, 'cash', 499, '2016-04-11 18:30:00'),
(15, 1, 1, 1, 'cash', 2010, '2016-03-11 18:58:00'),
(16, 1, 1, 1, 'cash', 599, '2016-03-11 18:30:00'),
(17, 1, 1, 1, 'online', 699, '2016-05-02 18:30:00');
-- --------------------------------------------------------
the query I tried below it
SELECT
SUM(CASE WHEN MONTH(date_created)=1 THEN (total_price) END) Jan,
SUM(CASE WHEN MONTH(date_created)=2 THEN (total_price) END) Feb,
SUM(CASE WHEN MONTH(date_created)=3 THEN (total_price) END) Mar,
SUM(CASE WHEN MONTH(date_created)=4 THEN (total_price) END) Apr,
SUM(CASE WHEN MONTH(date_created)=5 THEN (total_price) END) May,
SUM(CASE WHEN MONTH(date_created)=6 THEN (total_price) END) Jun,
SUM(CASE WHEN MONTH(date_created)=7 THEN (total_price) END) July,
SUM(CASE WHEN MONTH(date_created)=8 THEN (total_price) END) Aug,
SUM(CASE WHEN MONTH(date_created)=9 THEN (total_price) END) Sep,
SUM(CASE WHEN MONTH(date_created)=10 THEN (total_price) END) 'Oct',
SUM(CASE WHEN MONTH(date_created)=11 THEN (total_price) END) Nov,
SUM(CASE WHEN MONTH(date_created)=12 THEN (total_price) END) 'Dec'
FROM orders
WHERE source_id =1 AND date_created BETWEEN(CURDATE() - INTERVAL 1 MONTH)
AND CURDATE()
Result I get from query
Jan Feb Mar Apr May Jun July Aug Sep Oct Nov Dec
null null null null null 38300 null null null null null null null
desire result
may june
0 38300
A SQL query returns a fixed set of columns, determined by the from clause. If you want a variable set of columns, then you can use dynamic SQL.
In your case, perhaps getting the results as columns would suffice:
select month(date_created), sum(total_price)
from orders
where source_id = 1 and
date_created BETWEEN(CURDATE() - INTERVAL 1 MONTH) and CURDATE()
group by month(date_created)
having sum(total_price) <> 0;

GROUP_CONCAT not working in Sub-Query

I have tried this query to get the following data
SELECT
GROUP_CONCAT( sb.p_id ) ,
TRUNCATE( SUM( sb.total_amount ) , 2 ) grand_total,
TRUNCATE( SUM( sb.amount_wot ) , 2 ) sale_amount,
(SELECT TRUNCATE( SUM( (item_qty * item_price) * tax_price /100 ) , 2 )
FROM ci_bill_items
WHERE bill_id IN ( GROUP_CONCAT( sb.p_id ) ) ) tax_amount
FROM ci_suppliers_bills sb
WHERE sb.p_id >0
I got the expected result but not the tax_amount its return null but if i run the seperate query like :
SELECT TRUNCATE( SUM( (item_qty * item_price) * tax_price /100 ) , 2 )
FROM ci_bill_items
WHERE bill_id IN ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 71 )
Then i get the correct result. But all i want in one query like the first one, is group_concat not working like i tried bill_id IN ( GROUP_CONCAT( sb.p_id ) )? Help is much appriciated.
You can use FIND_IN_SET like this:
SELECT
GROUP_CONCAT( sb.p_id ) ,
TRUNCATE( SUM( sb.total_amount ) , 2 ) grand_total,
TRUNCATE( SUM( sb.amount_wot ) , 2 ) sale_amount,
(SELECT TRUNCATE( SUM( (item_qty * item_price) * tax_price /100 ) , 2 )
FROM ci_bill_items
WHERE FIND_IN_SET(bill_id, GROUP_CONCAT( sb.p_id ) ) > 0 ) tax_amount
FROM ci_suppliers_bills sb
WHERE sb.p_id >0
GROUP_CONCAT doesn't return a list, it returns a string. To get a list that you can use with IN, you need to run a subquery:
WHERE bill_id IN (SELECT p_id FROM ci_suppliers_bills
WHERE p_id > 0)

Rename duplicate rows in MySQL, WHERE IN () - dosen't works fine

I started from here:
Rename duplicate rows in MySQL
SELECT mid(s,instr(s,',')+1) as dubid
FROM (
SELECT group_concat(id ORDER BY id ASC) s, count(*) c
FROM `...`
GROUP BY field
HAVING c > 1) as z
Seems to work fine and returns dubid rows like
1
3,5,6
8
10,15
So I made this:
set #suf:=0;
UPDATE `table_cf_vendlist`
SET `cf_vendlist` = concat(`cf_vendlist`, ' \(', #suf:=#suf+1,'\)')
WHERE `cf_vendlistid` IN ({SELECT mid...})
But it renames only first ID from each row. So how I can make it work? Also how to nullyfy #suf counter for each row?
And no PHP. MySQL only!
set #suf:=0;
set #last := 0;
UPDATE `vtiger_cf_vendlist` t1
INNER JOIN (
SELECT MIN(cf_vendlistid) as id, cf_vendlist
FROM vtiger_cf_vendlist
GROUP BY cf_vendlist) AS unique1
ON (t1.cf_vendlist = unique1.cf_vendlist)
SET t1.`cf_vendlist` = concat(t1.`cf_vendlist`, ' \(',
#suf := if (#last = t1.cf_vendlistid, #suf+1, 1 and #last := t1.cf_vendlist), '\)')
WHERE t1.cf_vendlistid <> unique1.id
But I am not shure where is t1.cf_vendlist, and where is unique1.cf_vendlist. Also result is 0 and 1 in names, no increments and zeroing for each id set. But still, loks like it renamed everyting is needed.
Here is some data to run with sqlfiddle.com:
CREATE TABLE IF NOT EXISTS `table_cf_vendlist` (
`cf_vendlistid` int(11) NOT NULL AUTO_INCREMENT,
`cf_vendlist` varchar(200) NOT NULL,
PRIMARY KEY (`cf_vendlistid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=78 ;
INSERT INTO `table_cf_vendlist` (`cf_vendlistid`, `cf_vendlist`) VALUES
(2, 'Поставщик 2'),
(3, 'Поставщик 3'),
(4, 'Поставщик 4'),
(5, 'del 1'),
(6, 'Поставщик 1 (1)'),
(7, 'Поставщик 1 (2)'),
(8, 'пост 66'),
(9, 'пост 77'),
(10, 'пост 67'),
(11, 'пост 5'),
(12, '123'),
(13, '102'),
(14, 'пост 5 (1)'),
(15, 'пост 70'),
(47, 'Поставщик 1'),
(48, 'Поставщик 2'),
(49, 'Поставщик 3'),
(50, 'Поставщик 4'),
(51, 'del 1'),
(52, 'Поставщик 1'),
(53, 'Поставщик 1'),
(54, 'пост 5'),
(55, '124523452346256456'),
(56, 'пост 66'),
(57, 'пост 77'),
(58, 'пост 5'),
(59, 'пост 5'),
(60, 'пост 5'),
(61, 'пост 67'),
(62, '123'),
(63, '123'),
(64, '123'),
(65, 'пост 5'),
(66, 'пост 68'),
(67, '123'),
(68, '123'),
(69, '123'),
(70, '123'),
(71, '102'),
(72, 'пост 5'),
(73, 'пост 70');
UPD
We got
52 Поставщик 1 (11)
53 Поставщик 1 (12)
Instead of
52 Поставщик 1 (3)
53 Поставщик 1 (4)
and
48 Поставщик 2 (13)
49 Поставщик 3 (14)
50 Поставщик 4 (15)
Instead of
48 Поставщик 2 (2)
49 Поставщик 3 (2)
50 Поставщик 4 (2)
UPD 2:
set #suf:=0;
set #last := 0;
UPDATE table_cf_vendlist t
INNER JOIN (SELECT cf_vendlistid, concat(cf_vendlist, ' (',
#suf := if (STRCMP(#last1, cf_vendlist) = 0,
#suf+1,
1 or #last := cf_vendlist), ')') as new_label
FROM (
SELECT t1.cf_vendlistid, t1.cf_vendlist, unique1.id as reference_id
FROM table_cf_vendlist t1
INNER JOIN (SELECT MIN(cf_vendlistid) as id, cf_vendlist
FROM table_cf_vendlist
GROUP BY cf_vendlist) AS unique1
ON t1.cf_vendlist = unique1.cf_vendlist
ORDER BY cf_vendlist, cf_vendlistid
) AS t1_sorted
WHERE t1_sorted.cf_vendlistid <> t1_sorted.reference_id)
AS t1_sorted_labeled
ON t.cf_vendlistid = t1_sorted_labeled.cf_vendlistid
SET t.cf_vendlist = t1_sorted_labeled.new_label;
works. But 1 or #last := cf_vendlist is a hack that allows to return 1 or 0(and) instead of cf_vendlist string (see my comment below)
I have a solution in sqlfiddle. To follow how it works,
1) look at the subquery t1_sorted
SELECT t1.cf_vendlistid, t1.cf_vendlist, unique1.id as reference_id
FROM table_cf_vendlist t1
INNER JOIN (SELECT MIN(cf_vendlistid) as id, cf_vendlist
FROM table_cf_vendlist
GROUP BY cf_vendlist) AS unique1
ON t1.cf_vendlist = unique1.cf_vendlist
WHERE t1.cf_vendlistid <> unique1.id
ORDER BY cf_vendlist, cf_vendlistid
which returns results like in fig. 1.
2a) In subquery t1_sorted_labeled, the results are projected to remove the first occurrence of the cf_vendlist in each group and
2b) an extra field new_label is created with the cf_vendlist and (copy number)
3) Finally, the original query is joined with this subquery in the UPDATE statement, which merely copies new_label to cf_vendlist
Thus, complete answer:
set #suf:=0;
set #last := 0;
UPDATE table_cf_vendlist t
INNER JOIN (SELECT cf_vendlistid, concat(cf_vendlist, ' (',
#suf := if (STRCMP(#last, cf_vendlist) = 0,
#suf+1,
1 and length(#last := cf_vendlist)), ')') as new_label
FROM (
SELECT t1.cf_vendlistid, t1.cf_vendlist, unique1.id as reference_id
FROM table_cf_vendlist t1
INNER JOIN (SELECT MIN(cf_vendlistid) as id, cf_vendlist
FROM table_cf_vendlist
GROUP BY cf_vendlist) AS unique1
ON t1.cf_vendlist = unique1.cf_vendlist
ORDER BY cf_vendlist, cf_vendlistid
) AS t1_sorted
WHERE t1_sorted.cf_vendlistid <> t1_sorted.reference_id)
AS t1_sorted_labeled
ON t.cf_vendlistid = t1_sorted_labeled.cf_vendlistid
SET t.cf_vendlist = t1_sorted_labeled.new_label;
BTW, you should really have an index on cf_vendlist.

MySQL include zero rows when using COUNT with LEFT OUTER JOIN and GROUP BY

How can I avoid eliminating the users with zero meetings? I'm aware there are similar questions, but this code is quite a bit more complex.
SELECT user.userID, user.contactName, user.email, COUNT( * ) AS meetingsCount
FROM user
LEFT OUTER JOIN meeting ON user.userID = meeting.userID
WHERE user.userID NOT
IN ( 1, 2, 3, 4, 5, 59, 62, 63, 64, 66, 69, 71, 72, 73, 78, 107 )
AND SUBSTRING( meeting.meetingCode, 5, 2 )
BETWEEN 12
AND 22
AND SUBSTRING( meeting.meetingCode, 7, 2 )
BETWEEN 01
AND 12
AND SUBSTRING( meeting.meetingCode, 9, 2 )
BETWEEN 01
AND 31
GROUP BY user.userID, contactName, email
ORDER BY meetingsCount DESC
You need to put the logic for the meeting code table in your join. Otherwise users matching the records you are filtering out from the meeting table will be filtered out of your results. Making your JOIN essentially an INNER join. I think you also should put single quotes around the values in your BETWEEN clauses.
SELECT user.userID, user.contactName, user.email, COUNT( meeting.userID ) AS meetingsCount
FROM user
LEFT OUTER JOIN meeting ON user.userID = meeting.userID
AND SUBSTRING( meeting.meetingCode, 5, 2 ) BETWEEN '12' AND '22'
AND SUBSTRING( meeting.meetingCode, 7, 2 ) BETWEEN '01' AND '12'
AND SUBSTRING( meeting.meetingCode, 9, 2 ) BETWEEN '01' AND '31'
WHERE user.userID NOT IN ( 1, 2, 3, 4, 5, 59, 62, 63, 64, 66, 69, 71, 72, 73, 78, 107 )
GROUP BY user.userID, contactName, email
ORDER BY meetingsCount DESC

MySql query and GROUP BY, what do I need to do differently?

Right now I have an sql query that selects a range of of my sales reps, then joins a customer table, and counts if certain values are true. My query works, however, It leaves out sales reps who have not sold any customers. I would like this query to return all sales reps, even if they have not created any client records matching the criteria. What do I need to change?
Here is a link to my SQL Query: http://pastie.org/4557540 (same as below)
SELECT u.id AS
`employee_id`,
u.`first_name` AS
`employee_first_name`,
u.`last_name` AS
`employee_last_name`,
(SELECT Count(*)
FROM saleset s
WHERE s.pitchedby_id = `employee_id`
AND s.pitchstartedat BETWEEN '2012-08-20 00:00:00' AND
'2012-08-20 23:59:59') AS
`transfers_taken`,
Count(IF(c.`saletype_id` IS NOT NULL, 1, NULL)) AS
`total_closes`,
Count(IF(c.`saletype_id` = 1, 1, NULL)) AS
`regular_sale`,
Count(IF(c.`saletype_id` = 2, 1, NULL)) AS
`postdated_sale`,
Count(IF(c.`saletype_id` = 4, 1, NULL)) AS
`attempted_sale`,
Count(IF(c.`customerstatus_id` IN ( 8, 18, 23 ), 1, NULL)) AS
`cancel_status`,
Count(IF(c.`customerstatus_id` IN ( 1, 12, 13, 24 ), 1, NULL)) AS
`pending_completion_status`,
Count(IF(c.`customerstatus_id` IN ( 5, 6, 16 ), 1, NULL)) AS
`complete_status`,
Count(IF(c.`customerstatus_id` = 20, 1, NULL)) AS
`postdate_pending`,
Count(IF(c.`customerstatus_id` = 25, 1, NULL)) AS
`postdate_declined`
FROM `user` u
LEFT JOIN customer c
ON c.`salesrep_id` = u.id
WHERE u.id IN ( 39, 65, 76, 96,
195, 266, 349, 401,
402, 404, 405, 407,
411, 412 )
AND c.`activationdate` BETWEEN
'2012-08-20 00:00:00' AND '2012-08-20 23:59:59'
GROUP BY u.`id`
The problem is that your are filtering the customers in the WHERE clause, after the LEFT JOIN is specified, so salesman with no sales get left out, because c.activationdate is null for these records.
The solution is having the client filtering inside the join conditions:
(...)
FROM `user` u
LEFT JOIN customer c
ON (c.`salesrep_id` = u.id and
u.id IN ( 39, 65, 76, 96, 195, 266, 349, 401,
402, 404, 405, 407,
411, 412 )
AND c.`activationdate` BETWEEN
'2012-08-20 00:00:00' AND '2012-08-20 23:59:59')
GROUP BY u.`id`
Perhaps you could take what you have an union it with your sales reps that have no sales, and just hard code them to 0. Something like:
SELECT u.id AS
`employee_id`,
u.`first_name` AS
`employee_first_name`,
u.`last_name` AS
`employee_last_name`,
(SELECT Count(*)
FROM saleset s
WHERE s.pitchedby_id = `employee_id`
AND s.pitchstartedat BETWEEN '2012-08-20 00:00:00' AND
'2012-08-20 23:59:59') AS
`transfers_taken`,
Count(IF(c.`saletype_id` IS NOT NULL, 1, NULL)) AS
`total_closes`,
Count(IF(c.`saletype_id` = 1, 1, NULL)) AS
`regular_sale`,
Count(IF(c.`saletype_id` = 2, 1, NULL)) AS
`postdated_sale`,
Count(IF(c.`saletype_id` = 4, 1, NULL)) AS
`attempted_sale`,
Count(IF(c.`customerstatus_id` IN ( 8, 18, 23 ), 1, NULL)) AS
`cancel_status`,
Count(IF(c.`customerstatus_id` IN ( 1, 12, 13, 24 ), 1, NULL)) AS
`pending_completion_status`,
Count(IF(c.`customerstatus_id` IN ( 5, 6, 16 ), 1, NULL)) AS
`complete_status`,
Count(IF(c.`customerstatus_id` = 20, 1, NULL)) AS
`postdate_pending`,
Count(IF(c.`customerstatus_id` = 25, 1, NULL)) AS
`postdate_declined`
FROM `user` u
INNER JOIN customer c
ON c.`salesrep_id` = u.id
WHERE u.id IN ( 39, 65, 76, 96,
195, 266, 349, 401,
402, 404, 405, 407,
411, 412 )
AND c.`activationdate` BETWEEN
'2012-08-20 00:00:00' AND '2012-08-20 23:59:59'
GROUP BY u.`id`
UNION
SELECT u.id AS
`employee_id`,
u.`first_name` AS
`employee_first_name`,
u.`last_name` AS
`employee_last_name`,
0 AS
`transfers_taken`,
Count(IF(c.`saletype_id` IS NOT NULL, 1, NULL)) AS
`total_closes`,
Count(IF(c.`saletype_id` = 1, 1, NULL)) AS
`regular_sale`,
Count(IF(c.`saletype_id` = 2, 1, NULL)) AS
`postdated_sale`,
Count(IF(c.`saletype_id` = 4, 1, NULL)) AS
`attempted_sale`,
Count(IF(c.`customerstatus_id` IN ( 8, 18, 23 ), 1, NULL)) AS
`cancel_status`,
Count(IF(c.`customerstatus_id` IN ( 1, 12, 13, 24 ), 1, NULL)) AS
`pending_completion_status`,
Count(IF(c.`customerstatus_id` IN ( 5, 6, 16 ), 1, NULL)) AS
`complete_status`,
Count(IF(c.`customerstatus_id` = 20, 1, NULL)) AS
`postdate_pending`,
Count(IF(c.`customerstatus_id` = 25, 1, NULL)) AS
`postdate_declined`
FROM `user` u
LEFT JOIN customer c
ON c.`salesrep_id` = u.id
WHERE u.id IN ( 39, 65, 76, 96,
195, 266, 349, 401,
402, 404, 405, 407,
411, 412 )
AND c.`activationdate` BETWEEN
'2012-08-20 00:00:00' AND '2012-08-20 23:59:59'
AND c.`salesrep_id` IS NULL
GROUP BY u.`id`