'How to select unique records from the list of columns where we need to secure records with certain pair?

I need help with a query in MySQL for the following problem.

Here is my table. We have Name of people with mType resembling with the type of member (mType). Members have pair with mType P & S. Some members can have S along side P, some doesn't. House Number is unique for all P records such that P record following by S record will have same houseNo. If we consider P as parent and S as son, both Parent & Son lives in a same house thus have same houseNo. We need to pull out all records with P & S pair or if P doesn't have S, with records added into the table in latest date order.

Here one of the HouseNo is showing twice in the table, so we need to choose only the record with lastest date for such records and rest of the unique records.

mType | mindex    | HouseNo  |     date     |   Name
---------------------------------------------------
  P    |    1     |  111     |  2022-02-10  |  Addy
  S    |    1     |  111     |  2022-02-10  |  March
  P    |    2     |  222     |  2022-02-15  |  Joy
  S    |    2     |  222     |  2022-02-15  |  Gorge
  P    |    3     |  321     |  2022-02-10  |  Akhil
  P    |    4     |  423     |  2022-02-10  |  Rose
  P    |    5     |  111     |  2022-03-10  |  Addy
  S    |    5     |  111     |  2022-03-10  |  March
  P    |    6     |  111     |  2022-03-08  |  Addy
  S    |    6     |  111     |  2022-03-08  |  March

For an example, I need these results: Here we don't have the duplicate record from date "2022-02-10" & "2022-03-08" of the same HouseNo 111. So here we picked all the HouseNo records with latest date and simultaneous pairs.

mType | mindex    | HouseNo  |     date     |   Name
---------------------------------------------------
  P    |    2     |  222     |  2022-02-15  |  Joy
  S    |    2     |  222     |  2022-02-15  |  Gorge
  P    |    3     |  321     |  2022-02-10  |  Akhil
  P    |    4     |  423     |  2022-02-10  |  Rose
  P    |    5     |  111     |  2022-03-10  |  Addy
  S    |    5     |  111     |  2022-03-10  |  March

I tried a lot and lastly I came up with this query. Here the id is the primary key of the table. I am using mysql version 5.7.

    SELECT
    *
FROM
    `member_details` t
WHERE
    t.id =(
    SELECT
        MAX(t2.id)
    FROM
        member_details t2
    WHERE
        t.name = t2.name AND t.HouseNo = t2.HouseNo
)

But here I am unable to pick records which are latest in date. This query give me the last record from the number of duplicate records, making the result. Whereas Addy and March have record with date "2022-03-10" already available in the table.

mType | mindex    | HouseNo  |     date     |   Name
---------------------------------------------------
  P    |    2     |  222     |  2022-02-15  |  Joy
  S    |    2     |  222     |  2022-02-15  |  Gorge
  P    |    3     |  321     |  2022-02-10  |  Akhil
  P    |    4     |  423     |  2022-02-10  |  Rose
  P    |    5     |  111     |  2022-03-08  |  Addy
  S    |    5     |  111     |  2022-03-08  |  March

Here is the MySQL version on the table.

CREATE TABLE `member_details` (
  `id` int(11) NOT NULL,
  `mType` enum('P','S') NOT NULL,
  `mIndex` int(11) NOT NULL,
  `date` datetime NOT NULL,
  `Name` varchar(10) NOT NULL,
  `HouseNo` varchar(5) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `member_details`
--

INSERT INTO `member_details` (`id`, `mType`, `mIndex`, `date`, `Name`, `HouseNo`) VALUES
(1, 'P', 1, '2022-02-10 00:00:00', 'Addy', '111'),
(2, 'S', 1, '2022-02-10 00:00:00', 'Mark', '111'),
(3, 'P', 2, '2022-02-10 00:00:00', 'Joy', '222'),
(4, 'S', 2, '2022-02-10 00:00:00', 'Gorge', '222'),
(5, 'P', 3, '2022-02-10 00:00:00', 'Akhil', '232'),
(6, 'P', 4, '2022-02-10 00:00:00', 'Rose', '324'),
(7, 'P', 5, '2022-03-10 00:00:00', 'Addy', '111'),
(8, 'S', 5, '2022-03-10 00:00:00', 'Mark', '111'),
(9, 'P', 6, '2022-03-08 00:00:00', 'Addy', '111'),
(10, 'S', 6, '2022-03-08 00:00:00', 'Mark', '111');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `member_details`
--
ALTER TABLE `member_details`
  ADD PRIMARY KEY (`id`);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source