http://sqlfiddle.com/#!9/ea4d2/1
Here is the SQLfiddle to look at. storepkid 16 is repeated 2 times. But I want it only one time. I tried DISTINCT but it eliminate entire duplicate row so it's not working in this case. What changes do I need to make in query to get proper result? Please help.
NOTE: forgot to mention in initial question that I tried GROUP BY too but it's giving incorrect data in record set result. I tried to use GROUP BY in following ways.
http://sqlfiddle.com/#!9/ea4d2/20
http://sqlfiddle.com/#!9/ea4d2/17
NOTE2: @Gabriel Valdez Timbol, I want result like this. Row with duplicate storepkid should be eliminated.
| Storepkid | selldate |
+---------------+-----------------------+
| 19 | August, 25 2015 10:00:00 |
| 12 | August, 24 2015 19:00:00 |
| 16 | August, 24 2015 16:00:00 |
| 15 | August, 23 2015 13:00:00 |
| 17 | August, 21 2015 10:00:00 |
You can use max
function. Use the below query
SELECT p.storepkid, max(p.selldate) AS recentselldate
FROM
(SELECT storepkid, purchasedatetime AS selldate
FROM t_product_purchase
UNION ALL
SELECT storepkid, starttime AS selldate
FROM t_service_purchase
UNION ALL
SELECT storepkid, selldatetime AS selldatetime
FROM t_coupon_purchase ) p
GROUP BY storepkid
order by max(p.selldate)
DESC LIMIT 0,5
OUTPUT:
| Storepkid | selldate |
+---------------------------------------+
| 19 | August, 25 2015 10:00:00 |
| 12 | August, 24 2015 19:00:00 |
| 16 | August, 24 2015 16:00:00 |
| 15 | August, 23 2015 13:00:00 |
| 14 | August, 21 2015 13:15:00 |
Check the DEMO HERE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With