I have the following (example) table:
+----------+--------------+------------+------------+------------+
| Customer | Subscription | Status | Start Date | End Date |
+----------+--------------+------------+------------+------------+
| 1 | 1a001 | Pending | 02.02.2020 | |
| 1 | 1b002 | Pending | 02.02.2020 | 05.02.2020 |
| 1 | 1c003 | Rejected | 02.02.2020 | |
| 1 | 1d004 | Incomplete | 02.02.2020 | |
| 1 | 1e005 | Pending | 07.02.2020 | |
| 1 | 1f006 | Active | 07.02.2020 | |
| 2 | 2a001 | Pending | 02.02.2020 | |
| 2 | 2b002 | Pending | 02.02.2020 | |
| 2 | 3c003 | Rejected | 02.02.2020 | |
| 2 | 4d004 | Incomplete | 02.02.2020 | |
| 2 | 5e005 | Pending | 07.02.2020 | 07.02.2020 |
| 2 | 6f006 | Active | 07.02.2020 | |
+----------+--------------+------------+------------+------------+
The aim is to select only Active Customers. Assumptions and conditions are:
One Customer_no can have many Subscriptions.
Active Customer = Customer that has at least one Active Subscription.
Active Subscription is when: Status = Active, the start_date is >= sysdate() and there is no end_date.
I am going to have very long list of Statuses what can be mapped using CASE statement. Is there any idea how to select distinct Active customers basing on that example? I am a begginer and do not know how to start with this task.
Thanks, Pawel
Your condition for an active customer can be written like this:
status = 'Active' and start_date >= curdate() and end_date is null
So group by customer and use the condition in the having clause:
select customer
from tablename
group by customer
having sum(status = 'Active' and start_date >= curdate() and end_date is null)
If the sum() returns 0 then it is evaluated to false.
In any other case it is evaluated to true.
Related
I'm trying to get the total amount of overdraft accounts from an old Date, the goal is to get the total amount it was on the 31st of January.
I have the following tables Users and Transactions.
USERS (currently)
| user_id | name | account_balance |
|---------|---------|------------------|
| 1 | Wells | 1.00 |
| 2 | John | -10.00 |
| 3 | Sahar | -5.00 |
| 4 | Peter | 1.00 |
TRANSACTIONS (daily transition can go back in time)
| trans_id | user_id | amount_tendered | trans_datetime |
|------------|---------|-------------------|---------------------|
| 1 | 1 | 2 | 2021-02-16 |
| 2 | 2 | 3 | 2021-02-16 |
| 3 | 3 | 5 | 2021-02-16 |
| 4 | 4 | 2 | 2021-02-16 |
| 5 | 1 | 10 | 2021-02-15 |
so the current total overdraft amount is
SELECT sum(account_balance) AS O_D_Amount
FROM users
WHERE account_balance < 0;
| O_D_Amount |
|------------|
| -15 |
I need Help to reverse this amount to a date in history.
Assuming overdrafts are based on the sum of transactions up to a point, you can use a subquery:
select sum(total) as total_overdraft
from (select user_id, sum(amount_tendered) as total
from transactions t
where t.trans_datetime <= ?
group by user_id
) t
where total < 0;
The ? is a parameter placeholder for the date/time you care about.
I have a table of subscriptions, storing user id, subscription end date, program id. One user can be subscribed to many programs, but for the scope of the problem the latest date is considered as the end date of the subscription. The goal is to find the number of users whose subscription is ending for each month of each year. To illustrate it:
-------------------------------------------
| user_id | program_id | end_date |
-------------------------------------------
| 1 | 1 | 2015-12-10 |
| 1 | 2 | 2017-08-27 |
| 2 | 1 | 2017-09-20 |
| 3 | 2 | 2017-10-01 |
| 2 | 3 | 2017-09-18 |
| 5 | 12 | 2017-10-22 |
| 4 | 3 | 2017-10-10 |
| 3 | 8 | 2018-11-15 |
-------------------------------------------
Intermediate result show when will the subscription end for each user (only month is needed):
------------------------------
| user_id | enddate |
------------------------------
| 1 | 2017-08 |
| 2 | 2017-09 |
| 3 | 2018-11 |
| 4 | 2017-10 |
| 5 | 2017-10 |
------------------------------
This was achieved with the query:
Select user_id, DATE_FORMAT(max(end_date), '%Y-%m') AS as enddate
From subscription
Group by user_id
Order by end_date desc;
The final result must further filter the list, showing only how many users will be left with no subscription in each month, like this:
------------------------------
| count | month, year |
------------------------------
| 1 | 2017-08 |
| 1 | 2017-09 |
| 2 | 2017-10 |
| 1 | 2018-11 |
------------------------------
This is where I am stuck with no mysql ideas. Iterating through the results and counting is out of the question.
You could try arranging the results by the enddate, like this:
select count(user_id), DATE_FORMAT(max_end_date, '%Y-%m')as enddate
from (
select user_id, max(end_date) as max_end_date
From subscription
Group by user_id
) n
group by enddate
Order by enddate desc;
Try this -
Select COUNT(*), DATE_FORMAT(MAX(end_date), '%Y-%m') AS as enddate
From subscription
Group by user_id
Order by end_date desc;
I have a table, let's say, like this one:
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| user_id | mortgage_id | value | classification | created_at |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 6c1e1f12-2e5d-488d-b02d-29fcffe783f2 | 1e76bcbb-70ee-4966-87fd-1d6024a04513 | 0 | initial | 2014-08-23 14:25:42 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 49dc3dab-d2d0-400b-b964-71e03339d475 | 59366911-f1a8-4a8c-b7ea-c3257d04478e | 1 | created | 2015-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 76ce889b-2f2c-435f-8754-7c5ec15cbfcb | b962e26b-1ba6-4547-8eb8-167989a0705e | 5 | created | 2016-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 5d9f1892-05c0-4b0a-b5d9-a501595fa351 | fb4be36e-e156-4c1b-bd40-422d30646f8e | 8 | created | 2016-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 49dc3dab-d2d0-400b-b964-71e03339d475 | 2cee0bc7-744f-4f51-a094-f5eb66ac482e | 2 | created | 2017-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 76ce889b-2f2c-435f-8754-7c5ec15cbfcb | b0d27c9e-907c-43df-abd2-5772785cb91c | 0 | created | 2017-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
I'm trying to fetch/get all the distinct/unique user_ids that don't have any records from a given moment in time and onwards.
For instance, if I choose that "time frame" to be: After 2017-01-01 00:00:00, the return would be:
|--------------------------------------+
| user_id |
|--------------------------------------+
| 6c1e1f12-2e5d-488d-b02d-29fcffe783f2 |
|--------------------------------------+
| 5d9f1892-05c0-4b0a-b5d9-a501595fa351 |
|--------------------------------------+
I have this query, but I think there should be a better way to do this:
SET #timestamp = '2017-01-01 00:00:00';
SELECT DISTINCT user_id
FROM mortgages
WHERE user_id NOT IN (SELECT DISTINCT user_id FROM mortgages WHERE created_at > #timestamp);
I would use group by:
select user_id
from mortgages
group by user_id
having max(created_at) <= #timestamp;
+-----------+------------+------------+
| ACCOUNT | PAID_DATE | DUE_DATE |
+-----------+------------+------------+
| 103240005 | 2010-07-22 | 2009-11-30 |
| 103240005 | 2010-07-22 | 2007-09-30 |
| 103240005 | 2010-07-22 | 2008-09-30 |
| 103240006 | 2010-07-22 | 2009-09-30 |
| 103240006 | 2010-07-22 | 2007-07-22 |
| 103240007 | 2010-07-22 | 2008-07-22 |
| 103240008 | 2010-07-22 | 2009-08-31 |
| 103240009 | 2010-07-22 | 2007-12-31 |
| 103240009 | 2010-07-22 | 2008-12-31 |
| 103240005 | 2010-07-22 | 2009-12-31 |
+-----------+------------+------------+
The above sample dataset is from a banking application I am building.
I would like to get per account, the amount of records where the payments were made on time, i.e DATEDIFF(DUE_DATE, PAID_DATE) = 0. Please note that there are multiple entries per account.
Here is my problematic query:
select ACCOUNT_NUMBER, count(DATEDIFF(PAID_DATE, DUE_DATE) as diff) as diff_count
from TRANSACTIONS
where diff=0 group by ACCOUNT_NUMBER;
Assuming the paid_date and due_date are of date type, you can use:
select account_number,
sum(paid_date = due_date) as diff_count
from transactions
group by account_number;
if the two dates are equal, the result will be true which is taken as 1 by mysql and 0 for false.
EDIT:
You can further add more aggregates as needed. For e.g. - count where the overdue is 10 or more days, you can use:
select account_number,
sum(paid_date = due_date) as diff_count,
sum(datediff(paid_date, due_date) >= 10) as overdue,
from transactions
group by account_number;
I'm creating a small employment site and am wondering if this is possible in MySQL: I have 3 sample jobs and I want to show all users who applied to job_id = 1 who have an application status of 'pending' while showing the total number of other 'pending' and 'pending' + 'hired' applications each user has.
I've been trying to get my head around this but I'm having problems. Is this something MySQL can do?
users
+----+-------+
| ID | name |
+----+-------+
| 1 | hanna |
| 2 | bob |
| 3 | rick |
+----+-------+
job
+--------+------------+
| job_id | jobname |
+--------+------------+
| 1 | 'waiter'|
| 2 | 'janitor'|
| 3 | 'cook'|
+--------+------------+
applications
+----------+---------+-----------+
| user_id | job_id | status |
+----------+---------+-----------+
| 1 | 1 | 'pending' |
| 1 | 2 | 'pending' |
| 1 | 3 | ' hired' |
| 2 | 1 | 'pending' |
| 3 | 1 | 'removed' |
+----------+---------+-----------+
My result set
+--------+---------+-----------+---------------+--------------------+
| job_id | user_id | status | count_pending | count_pendinghired |
+--------+---------+-----------+---------------+--------------------+
| 1 | 1 | 'pending' | 2 | 3 |
| 1 | 2 | 'pending' | 1 | 1 |
+--------+---------+-----------+---------------+--------------------+
The following query comes close to your suggested output. Note that it doesn't make sense to associate a single job_id with a given user, because a user may have multiple jobs. Likewise, it also doesn't make sense to associate a single status with a given user, since each record represents an aggregation of more than one status.
SELECT user_id,
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) AS count_pending,
SUM(CASE WHEN status = 'pending' OR status = 'hired'
THEN 1 ELSE 0 END) AS count_pendinghired
FROM applications
GROUP BY user_id