i am facing the below issue
Table structure :
some_table CREATE TABLE `some_table ` (
`wallet_id` int(11) NOT NULL AUTO_INCREMENT,
`wallet_amount` double(10,2) NOT NULL,
`actual_wallet_amount` double(10,2) NOT NULL,
`customer_id` int(11) NOT NULL,
`last_update_on` datetime NOT NULL,
`redeemption_date` date NOT NULL,
`expiry_date` date NOT NULL,
`wallet_status` varchar(200) NOT NULL,
`order_id` varchar(250) NOT NULL,
PRIMARY KEY (`wallet_id`)
) ENGINE=InnoDB
query :
SELECT *
FROM some_table
WHERE redeemption_date <= CURDATE( ) - INTERVAL 6 MONTH
AND wallet_status != "EXPIRED"
Desired Result:
- Need the get all the unique customer_id with latest last_updated_on
- Need the get all the records in the table which have only one enter last 6 months and above
- when adding group by clause on customer_id i am not getting latest
updated customer_id - i need all the latest unique customer_id
please help thanks in advance.
Best Answer
This type of queries are sometimes called "greatest-n-pre-group" (there's even a tag for these questions at SO!) and there are various ways to write them.
Assuming that the
(customer_id, last_updated_on)
is unique - i.e. there are no two rows with samelast_update_on
timestamp for the same customer - then you could firstGROUP BY customer_id
and get the maximum timestamp in a derived table and then join to the original table. Like this:-- query 1
Another variation is to first find the customer ids (similarly to above) but then join to the table using a correlated subquery. This is often more efficient if one can find easily the distinct customer ids (so, perhaps not in your case). Here is how it can be done, so you can test both:
-- query 2