Trying to query the date that a field changed to a zero amount. Here is some of the data from the tables involved:
-- mbMaster is the master table for all 'm' type tables (mvTable, mcTable), but does not have certain data that the 'm' tables have. The 'm' tables are broken up by program. All of the 'm' tables contain past to current data.
create table mbMaster (
mbID char(5),
mbShortDate char(10),
mbAction char(2),
mbEffectiveDate varchar (12) );
insert into mbMaster (
mbID, mbShortDate, mbAction, mbEffectiveDate)
values('11984','202405','3','5/1/2021'),
('11984','202403','2','3/1/2024'),
('11984','202312','13','12/13/2023'),
('11984','202303','2','3/1/2023'),
('11984','202212','13','12/13/2022'),
('12385','202402','2','2/29/2024'),
('12385','202401','10','2/31/2024'),
('12385','202311','2','11/1/2023'),
('12385','202310','13','10/9/2023'),
('12385','202308','3','8/1/2023'),
('12385','202306','3','6/1/2023'),
('17976','202403','3','3/1/2024'),
('17976','202401','10','1/31/2024'),
('17976','202310','13','10/23/2023'),
('17976','202310','2','10/1/2023'),
('17976','202303','3','3/1/2023'),
('17976','202210','1','10/18/2022'),
('18489','202404','3','4/1/2024'),
('18489','202310','3','10/1/2023'),
('18489','202307','1','7/1/2023'),
('12845','202402','3','2/1/2024'),
('12845','202311','2','11/1/2023'),
('12845','202310','13','10/16/2023'),
('12845','202305','3','5/1/2023'),
('12845','202304','3','4/1/2023'),
('19976','202311','3','11/1/2023'),
('19976','202306','2','6/1/2023'),
('19976','202305','13','5/2/2023'),
('19976','202210','3','10/1/2022');
-- tenMain only shows current information
create table tenMain (
tenID char(5),
tenPayment varchar(10),
tenStatus char(1) );
insert into tenMain (
tenID, tenPayment, tenStatus)
values('11984','46.00','A'),
('12385','0.00','A'),
('17976','0.00','A'),
('18489','136.00','A'),
('12845','0.00','A'),
('19976','0.00','A');
-- c program data
create table mcTable (
mcID char(5),
mcShortDate char(6),
mcPayment char(4) );
insert into mcTable (
mcID, mcShortDate, mcPayment)
values('17976','202403','0000'),
('17976','202401','0000'),
('17976','202401','0000'),
('17976','202310','0000'),
('17976','202310','0000'),
('17976','202303','0249'),
('17976','202210','0249');
-- v program data
create table mvTable (
mvID char(5),
mvShortDate char(6),
mvPayment char(4) );
insert into mvTable (
mvID, mvShortDate, mvPayment)
values('11984','202405','0046'),
('11984','202403','0000'),
('11984','202312','0000'),
('11984','202303','0025'),
('11984','202212','0000'),
('12385','202402','0000'),
('12385','202401','0000'),
('12385','202311','0000'),
('12385','202310','0000'),
('12385','202308','0138'),
('12385','202306','0138'),
('17976','202210','0249',
('18489','202404','0136'),
('18489','202310','0000'),
('18489','202307','0917'),
('12845','202402','0000'),
('12845','202311','0000'),
('12845','202310','0000'),
('12845','202305','0357'),
('12845','202304','1090'),
('19976','202311','0000'),
('19976','202306','0093'),
('19976','202305','0000'),
('19976','202210','0215');
I have a query that includes 3 tables, but is missing data from a fourth similar table (mcTable
).
This is where I'm at with the query:
select m2.mbID as member_id
,t.startDate as start_date
,c.tenPayment as member_payment
from mbMaster m2
inner join tenMain c on c.tenID = m2.mbID
inner join
(
select b.mvID
,min(
case
when tenPayment = 0.00
and b.mvShortDate = m1.mbShortDate
and b.mvPayment = '0000' then m1.mbEffectiveDate end
) as startDate
from mvTable b
inner join tenMain on tenID = b.mvID
inner join mbMaster m1 on m1.mbID = b.mvID
where tenStatus = 'A'
and tenPayment = 0.00
and m1.mbAction in ('1','2','3')
and not exists
(
select 1
from mvTable a
where a.mvID = b.mvID
and a.mvPayment <> b.mvPayment
and a.mvShortDate > b.mvShortDate
)
group by b.mvID
) t on t.mvID = m2.mbID
and t.startDate = m2.mbEffectiveDate
order by m2.mbID
I tried to add mcTable
into the max(case)
expression, similar to how the mvTable
is written, but the query returned nothing. Here's what the result should be:
member_id | start_date | member_payment |
---|---|---|
12385 | 11/1/2023 | 0.00 |
12845 | 11/1/2023 | 0.00 |
19976 | 11/1/2023 | 0.00 |
The mcID
17976 should be part of the results once the table is part of the query. Do I need to do another join/self join for the mcTable
? This third party app and db uses mysql as far as I can tell. Also, I am very limited as far as what I can use for doing a query. I've noticed that I can't use most of windows functions, so I'm just looking for how I can solve with subqueries. Thanks.
Edit: Got the tables/columns renamed so things make sense (hopefully). The query returns the results needed for finding when the Payments changed to 0.00 or 0000, except for mcTable
, which is not part of the query. The mcTable
follows the same filter as the mvTable
, but I don't know how to include the mcTable
that will return mcID
17976 and corresponding mbEffectiveDate
, which would be 10/1/2023. Any attempt at adding the mcTable
has resulted in no results. The full historical payment amounts for mcID
17976 is only found in mcTable
. Hope this all makes sense. Just looking for a solution to add mcIDs
with its corresponding mbEffectiveDate
to the query.