I have three tables here
1).organization_entries
+----+---------+-----------+-----------------+---------------+------+------------------+--------------+------------+---------+----------+--------------+---------------------+---------------------+---------------------+
| id | user_id | arena_ids | organization_id | serial_number | type | registration_fee | tax_group_id | from_date | to_date | status | comments | created_at | updated_at | deleted_at |
+----+---------+-----------+-----------------+---------------+------+------------------+--------------+------------+---------+----------+--------------+---------------------+---------------------+---------------------+
| 1 | 3 | [1] | 1 | 1 | 1 | 600 | NULL | 2018-02-04 | NULL | inactive | present date | 2018-02-04 22:21:04 | 2018-05-13 14:09:46 | NULL |
| 2 | 4 | [1] | 1 | 2 | 1 | 600 | NULL | 2018-02-05 | NULL | active | NULL | 2018-02-04 22:22:29 | 2018-05-13 14:09:46 | NULL |
| 3 | 5 | [1] | 1 | 3 | 1 | 600 | NULL | 2018-02-06 | NULL | active | NULL | 2018-02-04 22:23:23 | 2018-05-13 14:09:46 | NULL |
| 4 | 6 | [1] | 1 | 4 | 1 | 600 | NULL | 2018-02-03 | NULL | inactive | NULL | 2018-02-04 22:24:07 | 2018-05-13 14:09:46 | NULL |
| 5 | 7 | [1] | 1 | 5 | 1 | 600 | NULL | 2017-12-05 | NULL | active | NULL | 2018-02-04 22:25:33 | 2018-05-13 14:09:46 | NULL |
| 6 | 8 | [1] | 1 | 6 | 1 | 600 | NULL | 2018-02-04 | NULL | inactive | NULL | 2018-02-04 22:32:22 | 2018-05-13 14:09:46 | NULL |
| 7 | 9 | [2] | 1 | 7 | 1 | 600 | NULL | 2018-02-05 | NULL | active | NULL | 2018-02-05 08:06:31 | 2018-05-13 14:09:46 | NULL |
| 8 | 10 | null | 1 | 8 | 1 | NULL | NULL | 2018-02-05 | NULL | new | NULL | 2018-02-05 13:17:11 | 2018-05-13 14:09:46 | NULL |
| 9 | 11 | [1] | 1 | 9 | 1 | 0 | NULL | 2018-02-05 | NULL | active | need to pay | 2018-02-05 15:26:04 | 2018-05-13 14:09:46 | 2018-03-01 00:23:00 |
| 10 | 14 | [1] | 1 | 10 | 1 | 600 | NULL | 2018-02-10 | NULL | active | NULL | 2018-02-10 12:40:30 | 2018-05-13 14:09:46 | NULL |
+----+---------+-----------+-----------------+---------------+------+------------------+--------------+------------+---------+----------+--------------+---------------------+---------------------+---------------------+
2).coaching_class_entries
+----+---------+-------------------+---------------+------+------------+--------------+------------+------------+--------------+----------------+----------------+---------------------------------------------------+---------------------+---------------------+---------------------+
| id | user_id | coaching_class_id | serial_number | fees | bill_cycle | bill_plan_id | from_date | to_date | tax_group_id | last_bill_date | next_bill_date | comments | created_at | updated_at | deleted_at |
+----+---------+-------------------+---------------+------+------------+--------------+------------+------------+--------------+----------------+----------------+---------------------------------------------------+---------------------+---------------------+---------------------+
| 1 | 3 | 3 | 1 | 5000 | 0 | 3 | 2018-02-04 | 2018-05-31 | NULL | 2018-02-04 | NULL | NULL | 2018-02-04 22:25:55 | 2018-05-09 23:55:17 | 2018-02-18 16:10:01 |
| 2 | 3 | 4 | 2 | 1000 | 2 | 4 | 2018-02-04 | 2018-05-09 | NULL | 2018-02-04 | NULL | NULL | 2018-02-04 22:26:06 | 2018-05-09 23:55:17 | NULL |
| 3 | 4 | 3 | 3 | 5000 | 0 | 3 | 2018-02-05 | 2018-02-18 | NULL | 2018-02-05 | NULL | NULL | 2018-02-04 22:26:22 | 2018-05-09 23:55:17 | NULL |
| 4 | 4 | 4 | 4 | 1000 | 2 | 4 | 2018-02-05 | NULL | NULL | 2018-02-05 | 2018-04-05 | NULL | 2018-02-04 22:26:44 | 2018-05-09 23:55:17 | NULL |
| 5 | 5 | 3 | 5 | 5000 | 0 | 3 | 2018-02-06 | 2018-05-31 | NULL | 2018-02-06 | NULL | NULL | 2018-02-04 22:27:19 | 2018-05-09 23:55:17 | NULL |
| 6 | 5 | 4 | 6 | 1000 | 2 | 4 | 2018-02-06 | 2018-04-28 | NULL | 2018-02-06 | NULL | NULL | 2018-02-04 22:27:33 | 2018-05-09 23:55:17 | NULL |
| 7 | 6 | 3 | 7 | 5000 | 0 | 3 | 2018-02-03 | 2018-04-28 | NULL | 2018-02-05 | NULL | NULL | 2018-02-04 22:27:50 | 2018-05-09 23:55:17 | NULL |
| 8 | 6 | 4 | 8 | 1000 | 2 | 4 | 2018-02-03 | 2018-04-28 | NULL | 2018-02-04 | NULL | NULL | 2018-02-04 22:28:02 | 2018-05-09 23:55:17 | NULL |
| 9 | 7 | 3 | 9 | 5000 | 0 | 3 | 2017-12-03 | 2018-05-31 | NULL | 2018-02-04 | NULL | NULL | 2018-02-04 22:28:31 | 2018-05-09 23:55:17 | NULL |
| 10 | 7 | 4 | 10 | 1000 | 2 | 4 | 2017-12-05 | 2018-08-04 | NULL | 2018-02-05 | 2018-04-05 | starts on dec 5, 2017 and ends after 4 bill cycle | 2018-02-04 22:29:07 | 2018-05-09 23:55:17 | NULL |
+----+---------+-------------------+---------------+------+------------+--------------+------------+------------+--------------+----------------+----------------+---------------------------------------------------+---------------------+---------------------+---------------------+
3). coaching_classes
+----+----------------------+-----------------+----------+------------+------+--------+--------+-------------+------------+------------+-----------+----------+----------+-------+------------+-------------------------------+-----------+---------------------+---------------------+---------------------+
| id | name | organization_id | arena_id | trainer_id | type | sports | gender | max_entries | from_date | to_date | from_time | to_time | day_slot | fees | bill_cycle | bill_plans | due_after | created_at | updated_at | deleted_at |
+----+----------------------+-----------------+----------+------------+------+--------+--------+-------------+------------+------------+-----------+----------+----------+-------+------------+-------------------------------+-----------+---------------------+---------------------+---------------------+
| 1 | delete dummy plan | 1 | 1 | NULL | M | 1 | NULL | 100 | 2018-02-04 | NULL | 11:11:00 | 10:10:00 | 2 | 10000 | 1 | [{"fees": 10000, "cycle": 1}] | 20 | 2018-02-04 15:40:21 | 2018-03-15 12:01:06 | 2018-02-04 22:17:18 |
| 2 | delete dummy plan 2 | 1 | 1 | NULL | M | 1 | NULL | 100 | 2018-02-04 | NULL | 10:10:00 | 11:11:00 | 2 | 2000 | 1 | [{"fees": 2000, "cycle": 1}] | 10 | 2018-02-04 22:16:55 | 2018-03-15 12:01:06 | 2018-02-04 22:17:24 |
| 3 | Summer Camp | 1 | 1 | NULL | M | 1 | NULL | 100 | 2018-01-09 | 2018-05-31 | 10:10:00 | 11:11:00 | 2 | 5000 | 0 | [{"fees": 5000, "cycle": 0}] | 20 | 2018-02-04 22:19:04 | 2018-03-25 14:52:01 | NULL |
| 4 | Bi-Monthly Bad Plan | 1 | 1 | NULL | M | 1 | NULL | NULL | 2018-01-01 | NULL | 10:10:00 | 11:11:00 | 14 | 1000 | 2 | [{"fees": 1000, "cycle": 2}] | 10 | 2018-02-04 22:19:44 | 2018-04-27 09:25:01 | NULL |
| 5 | BadmintonEvent | 1 | 1 | 10 | C | 1 | NULL | 200 | 2018-02-01 | 2018-02-08 | 07:50:00 | 08:50:00 | 14 | 500 | 0 | [{"fees": 500, "cycle": 0}] | 2 | 2018-02-05 17:40:33 | 2018-05-08 12:42:25 | NULL |
| 6 | Advanced Coaching | 1 | 2 | 10 | C | 1 | M | 100 | 2018-02-06 | NULL | 09:00:00 | 10:00:00 | 14 | 1200 | 1 | [{"fees": 1200, "cycle": 1}] | 10 | 2018-02-06 12:17:40 | 2018-05-07 23:13:49 | NULL |
| 7 | Advanced Coaching 2 | 1 | 2 | 10 | C | 1 | M | 100 | 2018-02-06 | NULL | 09:00:00 | 10:00:00 | 14 | 1200 | 1 | [{"fees": 1200, "cycle": 1}] | 10 | 2018-02-12 22:55:52 | 2018-04-28 16:41:43 | NULL |
| 8 | Bi-Monthly good Plan | 1 | 1 | NULL | M | 1 | NULL | NULL | 2018-01-01 | NULL | 10:10:00 | 11:11:00 | 14 | 1000 | 2 | [{"fees": 1000, "cycle": 2}] | 10 | 2018-02-14 16:53:37 | 2018-03-15 12:01:06 | NULL |
| 9 | One time | 2 | 3 | NULL | M | 1 | NULL | 100 | 2018-01-01 | 2018-04-30 | 06:00:00 | 07:00:00 | 31 | 2000 | 0 | [{"fees": 2000, "cycle": 0}] | 10 | 2018-02-14 19:25:26 | 2018-03-15 12:01:06 | NULL |
| 10 | Monthly | 2 | 3 | NULL | M | 1 | NULL | 100 | 2018-01-01 | 2018-04-30 | 06:00:00 | 07:00:00 | 31 | 2000 | 1 | [{"fees": 2000, "cycle": 1}] | 10 | 2018-02-14 19:38:50 | 2018-05-02 18:52:49 | NULL |
+----+----------------------+-----------------+----------+------------+------+--------+--------+-------------+------------+------------+-----------+----------+----------+-------+------------+-------------------------------+-----------+---------------------+---------------------+---------------------+
Now the result which I want is something like this
Basically my problem is I want the result organization wise but orgnization_id is in coaching_classes table
+-----+--------+--------+----------------+-------------------------+-----------------------------+-------------------------------+-----------------------------+
| id | userId | status | organizationId | coachingClassEntries.id | coachingClassEntries.userId | coachingClassEntries.fromDate | coachingClassEntries.toDate |
+-----+--------+--------+----------------+-------------------------+-----------------------------+-------------------------------+-----------------------------+
| 197 | 200 | active | 2 | 252 | 200 | 2018-05-17 | NULL |
| 198 | 200 | active | 3 | 252 | 200 | 2018-05-17 | NULL |
| 199 | 187 | new | 3 | NULL | NULL | NULL | NULL |
| 200 | 199 | active | 2 | 253 | 199 | 2018-05-17 | NULL |
| 201 | 199 | active | 4 | 253 | 199 | 2018-05-17 | NULL |
| 202 | 194 | new | 2 | NULL | NULL | NULL | NULL |
| 203 | 196 | active | 2 | 246 | 196 | 2018-05-13 | NULL |
| 203 | 196 | active | 2 | 254 | 196 | 2018-05-17 | NULL |
| 204 | 196 | active | 3 | 246 | 196 | 2018-05-13 | NULL |
| 204 | 196 | active | 3 | 254 | 196 | 2018-05-17 | NULL |
| 205 | 176 | new | 2 | NULL | NULL | NULL | NULL |
| 206 | 176 | new | 4 | NULL | NULL | NULL | NULL |
+-----+--------+--------+----------------+-------------------------+-----------------------------+-------------------------------+-----------------------------+
I have written the query something like this
SELECT `organizationEntries`.*, `coachingClassEntries`.`id` AS `coachingClassEntries.id`, `coachingClassEntries`.`user_id` AS `coachingClassEntries.userId`, `coachingClassEntries`.`from_date` AS `coachingClassEntries.fromDate`, `coachingClassEntries`.`to_date` AS `coachingClassEntries.toDate` FROM (SELECT `organizationEntries`.`id`, `organizationEntries`.`user_id` AS `userId`, `organizationEntries`.`status`, `organizationEntries`.`organization_id` AS `organizationId` FROM `organization_entries` AS `organizationEntries` WHERE (`organizationEntries`.`deleted_at` > '2018-05-17 19:34:16' OR `organizationEntries`.`deleted_at` IS NULL) LIMIT 150, 200) AS `organizationEntries` LEFT OUTER JOIN `coaching_class_entries` AS `coachingClassEntries` ON `organizationEntries`.`userId` = `coachingClassEntries`.`user_id` AND (`coachingClassEntries`.`deleted_at` > '2018-05-17 19:34:16' OR `coachingClassEntries`.`deleted_at` IS NULL);
This is somewhat complex database but I have tried a lot but I am not able to get the correct query . Please help me or give me some hint
Related
I am trying to find account info for which the sum of available_balance is between two values. I tried to the following but it is not working as expected:
SELECT sum(a.avail_balance) `sum`
FROM account a
WHERE `sum` BETWEEN 5000 AND 10000
Result:ERROR 1054 (42S22): Unknown column 'sum' in 'where clause'
How can I accomplish my intended result?
My table:
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| account_id | product_cd | cust_id | open_date | close_date | last_activity_date | status | open_branch_id | open_emp_id | avail_balance | pending_balance |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| 1 | CHK | 1 | 2000-01-15 | NULL | 2005-01-04 | ACTIVE | 2 | 10 | 1057.75 | 1057.75 |
| 2 | SAV | 1 | 2000-01-15 | NULL | 2004-12-19 | ACTIVE | 2 | 10 | 500.00 | 500.00 |
| 3 | CD | 1 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 2 | 10 | 3000.00 | 3000.00 |
| 4 | CHK | 2 | 2001-03-12 | NULL | 2004-12-27 | ACTIVE | 2 | 10 | 2258.02 | 2258.02 |
| 5 | SAV | 2 | 2001-03-12 | NULL | 2004-12-11 | ACTIVE | 2 | 10 | 200.00 | 200.00 |
| 7 | CHK | 3 | 2002-11-23 | NULL | 2004-11-30 | ACTIVE | 3 | 13 | 1057.75 | 1057.75 |
| 8 | MM | 3 | 2002-12-15 | NULL | 2004-12-05 | ACTIVE | 3 | 13 | 2212.50 | 2212.50 |
| 10 | CHK | 4 | 2003-09-12 | NULL | 2005-01-03 | ACTIVE | 1 | 1 | 534.12 | 534.12 |
| 11 | SAV | 4 | 2000-01-15 | NULL | 2004-10-24 | ACTIVE | 1 | 1 | 767.77 | 767.77 |
| 12 | MM | 4 | 2004-09-30 | NULL | 2004-11-11 | ACTIVE | 1 | 1 | 5487.09 | 5487.09 |
| 13 | CHK | 5 | 2004-01-27 | NULL | 2005-01-05 | ACTIVE | 4 | 16 | 2237.97 | 2897.97 |
| 14 | CHK | 6 | 2002-08-24 | NULL | 2004-11-29 | ACTIVE | 1 | 1 | 122.37 | 122.37 |
| 15 | CD | 6 | 2004-12-28 | NULL | 2004-12-28 | ACTIVE | 1 | 1 | 10000.00 | 10000.00 |
| 17 | CD | 7 | 2004-01-12 | NULL | 2004-01-12 | ACTIVE | 2 | 10 | 5000.00 | 5000.00 |
| 18 | CHK | 8 | 2001-05-23 | NULL | 2005-01-03 | ACTIVE | 4 | 16 | 3487.19 | 3487.19 |
| 19 | SAV | 8 | 2001-05-23 | NULL | 2004-10-12 | ACTIVE | 4 | 16 | 387.99 | 387.99 |
| 21 | CHK | 9 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 1 | 1 | 125.67 | 125.67 |
| 22 | MM | 9 | 2004-10-28 | NULL | 2004-10-28 | ACTIVE | 1 | 1 | 9345.55 | 9845.55 |
| 23 | CD | 9 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 1 | 1 | 1500.00 | 1500.00 |
| 24 | CHK | 10 | 2002-09-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 23575.12 | 23575.12 |
| 25 | BUS | 10 | 2002-10-01 | NULL | 2004-08-28 | ACTIVE | 4 | 16 | 0.00 | 0.00 |
| 27 | BUS | 11 | 2004-03-22 | NULL | 2004-11-14 | ACTIVE | 2 | 10 | 9345.55 | 9345.55 |
| 28 | CHK | 12 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 38552.05 | 38552.05 |
| 29 | SBL | 13 | 2004-02-22 | NULL | 2004-12-17 | ACTIVE | 3 | 13 | 50000.00 | 50000.00 |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
You can only use aggregates for comparison in the HAVING clause:
GROUP BY ...
HAVING SUM(avail_balance) BETWEEN 5000 AND 10000
The HAVING clause requires you to define a GROUP BY clause.
Assuming, you want to SUM the balance for each cust_id, you need something like
SELECT sum(a.avail_balance) `sum`
FROM account a
GROUP BY `cust_id`
HAVING SUM(a.avail_balance) BETWEEN 5000 AND 10000
SELECT sum(a.avail_balance)'sum'
FROM account a
WHERE a.avail_balance between 5000 and 10000
Aggregate functions(AVG,COUNT,MIN,MAX,SUM) for comparison can be achieved by using the "HAVING" clause which requires "GROUP BY" clause(relevant category column depending on the requirement).
As rightly pointed by Pankaj Gadge, cust_id is the most suitable for the requirement of the problem statement
SELECT SUM(a.avail_balance) 'Sum'
FROM account a
GROUP BY cust_id
HAVING SUM(a.avail_balance) BETWEEN 5000 AND 10000;
aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause
ex: group by column_name
having sum(avail_balance) between 5000 and 10000
I am learning about SQL subqueries. Here is the subquery I am using from the book:
SELECT account_id, product_cd, cust_id
FROM account
WHERE open_branch_id = (
SELECT branch_id
FROM branch
WHERE name = 'Woburn Branch'
) AND open_emp_id IN (
SELECT emp_id
FROM employee
WHERE title = 'Teller' OR title = 'Head Teller'
);
Result:
+------------+------------+---------+
| account_id | product_cd | cust_id |
+------------+------------+---------+
| 1 | CHK | 1 |
| 2 | SAV | 1 |
| 3 | CD | 1 |
| 4 | CHK | 2 |
| 5 | SAV | 2 |
| 17 | CD | 7 |
| 27 | BUS | 11 |
+------------+------------+---------+
I have looked over this query trying to interpret it and understand it as well as the reasoning behind it's clauses but, I fail to understand the reason for the last AND clause AND open_emp_id IN...
I noticed that with just
SELECT account_id, product_cd, cust_id
FROM account
WHERE open_branch_id = (
SELECT branch_id
FROM branch
WHERE name = 'Woburn Branch'
)
You get the same result as above. Can anyone explain to me the reasoning behind the last AND open_emp_id IN clause and how omitting it would affect the final result?
Tables used in subquery:
Account table
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| account_id | product_cd | cust_id | open_date | close_date | last_activity_date | status | open_branch_id | open_emp_id | avail_balance | pending_balance |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| 1 | CHK | 1 | 2000-01-15 | NULL | 2005-01-04 | ACTIVE | 2 | 10 | 1057.75 | 1057.75 |
| 2 | SAV | 1 | 2000-01-15 | NULL | 2004-12-19 | ACTIVE | 2 | 10 | 500.00 | 500.00 |
| 3 | CD | 1 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 2 | 10 | 3000.00 | 3000.00 |
| 4 | CHK | 2 | 2001-03-12 | NULL | 2004-12-27 | ACTIVE | 2 | 10 | 2258.02 | 2258.02 |
| 5 | SAV | 2 | 2001-03-12 | NULL | 2004-12-11 | ACTIVE | 2 | 10 | 200.00 | 200.00 |
| 7 | CHK | 3 | 2002-11-23 | NULL | 2004-11-30 | ACTIVE | 3 | 13 | 1057.75 | 1057.75 |
| 8 | MM | 3 | 2002-12-15 | NULL | 2004-12-05 | ACTIVE | 3 | 13 | 2212.50 | 2212.50 |
| 10 | CHK | 4 | 2003-09-12 | NULL | 2005-01-03 | ACTIVE | 1 | 1 | 534.12 | 534.12 |
| 11 | SAV | 4 | 2000-01-15 | NULL | 2004-10-24 | ACTIVE | 1 | 1 | 767.77 | 767.77 |
| 12 | MM | 4 | 2004-09-30 | NULL | 2004-11-11 | ACTIVE | 1 | 1 | 5487.09 | 5487.09 |
| 13 | CHK | 5 | 2004-01-27 | NULL | 2005-01-05 | ACTIVE | 4 | 16 | 2237.97 | 2897.97 |
| 14 | CHK | 6 | 2002-08-24 | NULL | 2004-11-29 | ACTIVE | 1 | 1 | 122.37 | 122.37 |
| 15 | CD | 6 | 2004-12-28 | NULL | 2004-12-28 | ACTIVE | 1 | 1 | 10000.00 | 10000.00 |
| 17 | CD | 7 | 2004-01-12 | NULL | 2004-01-12 | ACTIVE | 2 | 10 | 5000.00 | 5000.00 |
| 18 | CHK | 8 | 2001-05-23 | NULL | 2005-01-03 | ACTIVE | 4 | 16 | 3487.19 | 3487.19 |
| 19 | SAV | 8 | 2001-05-23 | NULL | 2004-10-12 | ACTIVE | 4 | 16 | 387.99 | 387.99 |
| 21 | CHK | 9 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 1 | 1 | 125.67 | 125.67 |
| 22 | MM | 9 | 2004-10-28 | NULL | 2004-10-28 | ACTIVE | 1 | 1 | 9345.55 | 9845.55 |
| 23 | CD | 9 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 1 | 1 | 1500.00 | 1500.00 |
| 24 | CHK | 10 | 2002-09-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 23575.12 | 23575.12 |
| 25 | BUS | 10 | 2002-10-01 | NULL | 2004-08-28 | ACTIVE | 4 | 16 | 0.00 | 0.00 |
| 27 | BUS | 11 | 2004-03-22 | NULL | 2004-11-14 | ACTIVE | 2 | 10 | 9345.55 | 9345.55 |
| 28 | CHK | 12 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 38552.05 | 38552.05 |
| 29 | SBL | 13 | 2004-02-22 | NULL | 2004-12-17 | ACTIVE | 3 | 13 | 50000.00 | 50000.00 |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
Branch table:
+-----------+---------------+----------------------+---------+-------+-------+
| branch_id | name | address | city | state | zip |
+-----------+---------------+----------------------+---------+-------+-------+
| 1 | Headquarters | 3882 Main St. | Waltham | MA | 02451 |
| 2 | Woburn Branch | 422 Maple St. | Woburn | MA | 01801 |
| 3 | Quincy Branch | 125 Presidential Way | Quincy | MA | 02169 |
| 4 | So. NH Branch | 378 Maynard Ln. | Salem | NH | 03079 |
+-----------+---------------+----------------------+---------+-------+-------+
Employee table:
+--------+----------+-----------+------------+----------+-----------------+---------+--------------------+--------------------+
| emp_id | fname | lname | start_date | end_date | superior_emp_id | dept_id | title | assigned_branch_id |
+--------+----------+-----------+------------+----------+-----------------+---------+--------------------+--------------------+
| 1 | Michael | Smith | 2005-06-22 | NULL | NULL | 3 | President | 1 |
| 2 | Susan | Barker | 2006-09-12 | NULL | 1 | 3 | Vice President | 1 |
| 3 | Robert | Tyler | 2005-02-09 | NULL | 1 | 3 | Treasurer | 1 |
| 4 | Susan | Hawthorne | 2006-04-24 | NULL | 3 | 1 | Operations Manager | 1 |
| 5 | John | Gooding | 2007-11-14 | NULL | 4 | 2 | Loan Manager | 1 |
| 6 | Helen | Fleming | 2008-03-17 | NULL | 4 | 1 | Head Teller | 1 |
| 7 | Chris | Tucker | 2008-09-15 | NULL | 6 | 1 | Teller | 1 |
| 8 | Sarah | Parker | 2006-12-02 | NULL | 6 | 1 | Teller | 1 |
| 9 | Jane | Grossman | 2006-05-03 | NULL | 6 | 1 | Teller | 1 |
| 10 | Paula | Roberts | 2006-07-27 | NULL | 4 | 1 | Head Teller | 2 |
| 11 | Thomas | Ziegler | 2004-10-23 | NULL | 10 | 1 | Teller | 2 |
| 12 | Samantha | Jameson | 2007-01-08 | NULL | 10 | 1 | Teller | 2 |
| 13 | John | Blake | 2004-05-11 | NULL | 4 | 1 | Head Teller | 3 |
| 14 | Cindy | Mason | 2006-08-09 | NULL | 13 | 1 | Teller | 3 |
| 15 | Frank | Portman | 2007-04-01 | NULL | 13 | 1 | Teller | 3 |
| 16 | Theresa | Markham | 2005-03-15 | NULL | 4 | 1 | Head Teller | 4 |
| 17 | Beth | Fowler | 2006-06-29 | NULL | 16 | 1 | Teller | 4 |
| 18 | Rick | Tulman | 2006-12-12 | NULL | 16 | 1 | Teller | 4 |
+--------+----------+-----------+------------+----------+-----------------+---------+--------------------+--------------------+
The AND open_emp_id IN clause is selecting only the employee ids whose title is 'Teller' or 'Head Teller'. In your case the result is not different because you are looking for branch with the name 'Woburn Branch', and it is just a coincidence that all the employees there are of title 'Teller' or 'Head Teller' only.
| 10 | Paula | Roberts | 2006-07-27 | NULL | 4 | 1 | Head Teller | 2 |
| 11 | Thomas | Ziegler | 2004-10-23 | NULL | 10 | 1 | Teller | 2 |
| 12 | Samantha | Jameson | 2007-01-08 | NULL | 10 | 1 | Teller | 2 |
Change the branch name to 'Headquarters' in the first query, and you will see the difference that the subquery makes. Any employees at the 'Headquarters' who do not have title 'Teller' or 'Head Teller' will get excluded.
The last AND narrows the selection criteria to include only accounts that were open by a Teller or Head Teller. If the Loan Manager or Operations Manager had opened the account it would be excluded.
mysql> select calendar.calId,calendar.cdate,lsUsers.userId,
apptActual.OPDID,count(*)
from calendar
LEFT JOIN apptActual on calendar.calId=apptActual.calendarId
LEFT JOIN lsUsers on lsUsers.userId=apptActual.OPDID
group by 1,2,3,4 order by 3
desc limit 10;
Result is:
+-------+------------+--------+-------+----------+
| calId | cdate | userId | OPDID | count(*) |
+-------+------------+--------+-------+----------+
| 76 | 2016-03-16 | 11 | 11 | 1 |
| 75 | 2016-03-15 | 11 | 11 | 1 |
| 77 | 2016-03-17 | 9 | 9 | 1 |
| 75 | 2016-03-15 | 8 | 8 | 1 |
| 75 | 2016-03-15 | 1 | 1 | 1 |
| 1279 | 2019-07-02 | NULL | NULL | 1 |
| 1287 | 2019-07-10 | NULL | NULL | 1 |
| 1295 | 2019-07-18 | NULL | NULL | 1 |
| 1303 | 2019-07-26 | NULL | NULL | 1 |
| 1311 | 2019-08-03 | NULL | NULL | 1 |
+-------+------------+--------+-------+----------+
10 rows in set (0.34 sec)
By using above query I am getting result as above, but I need result result like below (all userId's has to come.)
+-------+------------+--------+-------+----------+
| calId | cdate | userId | OPDID | count(*) |
+-------+------------+--------+-------+----------+
| 76 | 2016-03-16 | 11 | 11 | 1 |
| 75 | 2016-03-15 | 11 | 11 | 1 |
| 77 | 2016-03-17 | 9 | 9 | 1 |
| 75 | 2016-03-15 | 8 | 8 | 1 |
| 75 | 2016-03-15 | 1 | 1 | 1 |
| 1279 | 2019-07-02 | 4 | NULL | 1 |
| 1287 | 2019-07-10 | 21| NULL | 1 |
| 1295 | 2019-07-18 | 3 | NULL | 1 |
| 1303 | 2019-07-26 | 7 | NULL | 1 |
| 1311 | 2019-08-03 | 5 | NULL | 1 |
+-------+------------+--------+-------+----------+
Please suggest me how to get result as above!
If you join lsUsers on userId = appActual.OPDID you will get what you ask for: usedId (NULL) = OPDID (NULL). Is there any other way to join lsUsers?
The query:
SELECT product_cd, SUM(avail_balance) prod_balance
FROM account
WHERE status = 'ACTIVE'
GROUP BY product_cd
HAVING MIN(avail_balance) >= 1000
AND MAX(avail_balance) <= 10000;
The table:
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| account_id | product_cd | cust_id | open_date | close_date | last_activity_date | status | open_branch_id | open_emp_id | avail_balance | pending_balance |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| 1 | CHK | 1 | 2000-01-15 | NULL | 2005-01-04 | ACTIVE | 2 | 10 | 1057.75 | 1057.75 |
| 2 | SAV | 1 | 2000-01-15 | NULL | 2004-12-19 | ACTIVE | 2 | 10 | 500.00 | 500.00 |
| 3 | CD | 1 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 2 | 10 | 3000.00 | 3000.00 |
| 4 | CHK | 2 | 2001-03-12 | NULL | 2004-12-27 | ACTIVE | 2 | 10 | 2258.02 | 2258.02 |
| 5 | SAV | 2 | 2001-03-12 | NULL | 2004-12-11 | ACTIVE | 2 | 10 | 200.00 | 200.00 |
| 7 | CHK | 3 | 2002-11-23 | NULL | 2004-11-30 | ACTIVE | 3 | 13 | 1057.75 | 1057.75 |
| 8 | MM | 3 | 2002-12-15 | NULL | 2004-12-05 | ACTIVE | 3 | 13 | 2212.50 | 2212.50 |
| 10 | CHK | 4 | 2003-09-12 | NULL | 2005-01-03 | ACTIVE | 1 | 1 | 534.12 | 534.12 |
| 11 | SAV | 4 | 2000-01-15 | NULL | 2004-10-24 | ACTIVE | 1 | 1 | 767.77 | 767.77 |
| 12 | MM | 4 | 2004-09-30 | NULL | 2004-11-11 | ACTIVE | 1 | 1 | 5487.09 | 5487.09 |
| 13 | CHK | 5 | 2004-01-27 | NULL | 2005-01-05 | ACTIVE | 4 | 16 | 2237.97 | 2897.97 |
| 14 | CHK | 6 | 2002-08-24 | NULL | 2004-11-29 | ACTIVE | 1 | 1 | 122.37 | 122.37 |
| 15 | CD | 6 | 2004-12-28 | NULL | 2004-12-28 | ACTIVE | 1 | 1 | 10000.00 | 10000.00 |
| 17 | CD | 7 | 2004-01-12 | NULL | 2004-01-12 | ACTIVE | 2 | 10 | 5000.00 | 5000.00 |
| 18 | CHK | 8 | 2001-05-23 | NULL | 2005-01-03 | ACTIVE | 4 | 16 | 3487.19 | 3487.19 |
| 19 | SAV | 8 | 2001-05-23 | NULL | 2004-10-12 | ACTIVE | 4 | 16 | 387.99 | 387.99 |
| 21 | CHK | 9 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 1 | 1 | 125.67 | 125.67 |
| 22 | MM | 9 | 2004-10-28 | NULL | 2004-10-28 | ACTIVE | 1 | 1 | 9345.55 | 9845.55 |
| 23 | CD | 9 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 1 | 1 | 1500.00 | 1500.00 |
| 24 | CHK | 10 | 2002-09-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 23575.12 | 23575.12 |
| 25 | BUS | 10 | 2002-10-01 | NULL | 2004-08-28 | ACTIVE | 4 | 16 | 0.00 | 0.00 |
| 27 | BUS | 11 | 2004-03-22 | NULL | 2004-11-14 | ACTIVE | 2 | 10 | 9345.55 | 9345.55 |
| 28 | CHK | 12 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 38552.05 | 38552.05 |
| 29 | SBL | 13 | 2004-02-22 | NULL | 2004-12-17 | ACTIVE | 3 | 13 | 50000.00 | 50000.00 |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
Result:
+------------+--------------+
| product_cd | prod_balance |
+------------+--------------+
| CD | 19500.00 |
| MM | 17045.14 |
+------------+--------------+
My understanding goes as far as this, from the query above:
SELECT product_cd, SUM(avail_balance) prod_balance
FROM account
WHERE status = 'ACTIVE'
GROUP BY product_cd
Which results in:
+------------+--------------+
| product_cd | prod_balance |
+------------+--------------+
| BUS | 9345.55 |
| CD | 19500.00 |
| CHK | 73008.01 |
| MM | 17045.14 |
| SAV | 1855.76 |
| SBL | 50000.00 |
+------------+--------------+
My confusion is with:
HAVING MIN(avail_balance) >= 1000
AND MAX(avail_balance) <= 10000;
Which I interpret as: "Include only the ones having a balance that is greater than or equal to 1,000 and lesser than or equal to 10,000" which would result in
BUS: 9345.55
SAV: 1855.76
Which is completely wrong when I ran the query as above. Any explanations as to what is going on would help.
instead of MIN(avail_balance) and MIN(avail_balance), try
HAVING MIN(SUM(avail_balance)) >=1000
and MAX(SUM(avail_balance)) <=10000
You are getting CD and MM in the result beacause all of their values are in between 1000 to 10000
Do you want the total or the minimum or maximum avail_balance match a certain range? They are different things.
The product CD for instance has a total of 19500, a minimum of 1500 and a maximum of 10000. You are showing the total (SUM), but want to see results where the product's minimum (MIN) is >= 1000 and the product's maximum (MAX) is <= 10000. This is true for CD.
So your query shows the correct results.
Maybe you want these, or maybe you want
SELECT
product_cd,
SUM(avail_balance) AS prod_balance
FROM account
WHERE status = 'ACTIVE'
GROUP BY product_cd
HAVING SUM(avail_balance) >= 1000
AND SUM(avail_balance) <= 10000;
instead.
I have this simple query:
select quarter(invitation_date) as `iQuarter`, invitation_date as `iDate`,count(*) as count from data group by iQuarter,iDate with rollup
I would expect to get a table like this:
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 133 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 20 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| NULL | NULL | 864 |
+----------+------------+-------+
But what I get has the total in the wrong position and before every quarters total we have a row with the next quarter with a count of one.
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 1 |
| NULL | NULL | 1 |
| 1 | 2010-02-23 | 132 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| 2 | 2010-04-06 | 1 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 19 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| 3 | 2010-07-06 | 1 |
| NULL | NULL | 864 |
+----------+------------+-------+
Invitation date is defined as:
+-------------------+---------------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------------+------+-----+---------------------+-------+
| invitation_date | date | NO | | | |
+-------------------+---------------------------+------+-----+---------------------+-------+
Anyone have a clue on why this is happening? Mysql version: 5.0.27