एक सहसंबद्ध उपश्रेणी का प्रयोग करें:
SELECT COUNT(DISTINCT f_bankid) AS tcount
FROM tbl_fileStatus t
WHERE ? = (SELECT f_filestatus FROM tbl_fileStatus WHERE f_bankid = t.f_bankid ORDER BY f_id DESC LIMIT 1)
?
f_bankid
. के साथ आप खोजते हैं।
डेमो
देखें। .
MySql 8.0+ में आप FIRST_VALUE()
. का उपयोग कर सकते हैं विंडो फ़ंक्शन:
SELECT COUNT(*) AS tcount
FROM (
SELECT DISTINCT f_bankid,
FIRST_VALUE(f_filestatus) OVER (PARTITION BY f_bankid ORDER BY f_id DESC) f_filestatus
FROM tbl_fileStatus
) t
WHERE f_filestatus = ?
डेमो
देखें .
यदि आप सभी f_filestatus
. के लिए परिणाम चाहते हैं 1 पंक्ति में:
SELECT
SUM(f_filestatus = 1) AS tcount1,
SUM(f_filestatus = 2) AS tcount2,
SUM(f_filestatus = 3) AS tcount3
FROM (
SELECT t.f_bankid, t.f_filestatus
FROM tbl_fileStatus t
WHERE t.f_id = (SELECT f_id FROM tbl_fileStatus WHERE f_bankid = t.f_bankid ORDER BY f_id DESC LIMIT 1)
) t
डेमो
देखें .
परिणाम:
> tcount1 | tcount2 | tcount3
> ------: | ------: | ------:
> 0 | 1 | 2