How to display the total count of the last row by filestatus?

半世苍凉 提交于 2021-01-29 08:13:06

问题


I have to display the total count of the last row by filestatus.

tbl_bankdata

bank_id | b_orderno| b_bankname| lead_id
     1  | 01-01    | 1         |  1
     2  | 01-02    | 2         |  1
     3  | 02-01    | 3         |  2
     4  | 03-01    | 1         |  3

tbl_fileStatus

f_id | f_bankid| f_filestatus
  1  |      1  | 1
  2  |      2  | 1
  3  |      2  | 2
  4  |      1  | 2
  5  |      1  | 3
  6  |      3  | 2 
  7  |      3  | 3  

I have two tables tbl_bankdata and tbl_fileStatus. I am sending bank_id in the tbl_fileStatus as a f_bank_id.

Now I have to show the last f_bankid count.

For example, I have to fetch the count where f_filestatus=1. so my output will be 0. Why 0 because f_bankid 1 and 2 have a f_filestatus=1 but f_bankid 1 and 2 have the last row with f_filestatus the 3 and 2.

If I have to count f_filestatus=2 then I will get the output 1 and if count f_filestatus=3 then the output will be 2. why 2 because f_bank_id 1 have f_filestatus 3 and f_bank_id 3 have f_filestatus 3

This is my query

select (
    SELECT COUNT(f_id) 
    FROM tbl_fileStatus 
    WHERE f_filestatus=1 and f_id IN (
        SELECT MAX(f_id) FROM tbl_fileStatus GROUP BY f_bankid
    )
) as tcount

Would you help me out with this issue?

After suggested by @forpas

SELECT (SELECT Count(DISTINCT f_bankid)
    FROM   tbl_filestatus t
    WHERE  1 = (SELECT f_filestatus
                FROM   tbl_filestatus
                WHERE  f_bankid = t.f_bankid
                ORDER  BY f_id DESC
                LIMIT  1)) AS tcount1,
   (SELECT Count(DISTINCT f_bankid)
    FROM   tbl_filestatus t
    WHERE  2 = (SELECT f_filestatus
                FROM   tbl_filestatus
                WHERE  f_bankid = t.f_bankid
                ORDER  BY f_id DESC
                LIMIT  1)) AS tcount2,
   (SELECT Count(DISTINCT f_bankid)
    FROM   tbl_filestatus t
    WHERE  3 = (SELECT f_filestatus
                FROM   tbl_filestatus
                WHERE  f_bankid = t.f_bankid
                ORDER  BY f_id DESC
                LIMIT  1)) AS tcount3  

回答1:


Use a correlated subquery:

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)

Replace ? with the f_bankid you search for.
See the demo.

In MySql 8.0+ you can use FIRST_VALUE() window function:

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 = ?

See the demo.

If you want results for all f_filestatus in 1 row:

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

See the demo.
Results:

> tcount1 | tcount2 | tcount3
> ------: | ------: | ------:
>       0 |       1 |       2



回答2:


In MySQL 8.0, I woud recommend row_number():

select count(*) as cnt
from (
    select fs.*,
        row_number() over(partition by f_bank_id order by f_id desc) rn
    from tbl_filestatus fs
) fs
where rn = 1 and filestatus = ?

An alternative that works across all versions is a correlated subquery:

select count(*) as cnt
from tbl_filestatus fs
where fs.f_id = (
    select max(fs1.f_id)
    from tbl_filestatus fs1
    where fs1.f_bank_id = fs.bank_id and fs1.filestatus = ?
)

For performance with the second query, consider an index on (bank_id, filestatus, fs_id desc). With this index, that query may run faster than the first approach using row_number(), even in MySQL 8.0.

If you want the count for all filestatuses at once:

select filestatus, count(*) as cnt
from tbl_filestatus fs
where fs.f_id = (
    select max(fs1.f_id)
    from tbl_filestatus fs1
    where fs1.f_bank_id = fs.bank_id
)
group by filestatus



回答3:


You can use next query with one exception - query does not return 0 result but it can be solved on visualisation level:

SELECT COUNT(*) FROM tbl_fileStatus 
JOIN (
    SELECT f_bankid, MAX(f_filestatus) AS max_f_filestatus
    FROM tbl_fileStatus GROUP BY f_bankid
) AS m ON m.max_f_filestatus = tbl_fileStatus.f_filestatus and
  m.f_bankid = tbl_fileStatus.f_bankid
  WHERE tbl_fileStatus.f_filestatus = 3 -- or 2 or any other value :)

Test SQL here



来源:https://stackoverflow.com/questions/65466022/how-to-display-the-total-count-of-the-last-row-by-filestatus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!