如何通过文件状态显示最后一行的总数?

How 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  

我有两个表 tbl_bankdatatbl_fileStatus。我将 bank_id 作为 f_bank_id.

tbl_fileStatus 中发送

现在我必须显示最后 f_bankid 计数。

例如,我必须获取 f_filestatus=1 的计数。所以我的输出将是 0。为什么 0 因为 f_bankid 1 and 2 有一个 f_filestatus=1f_bankid 1 and 2 最后一行有 f_filestatus 3 and 2.

如果我必须计数 f_filestatus=2,那么我将得到输出 1,如果计数 f_filestatus=3,那么输出将是 2。为什么 2 因为 f_bank_id 1f_filestatus 3f_bank_id 3f_filestatus 3

这是我的查询

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

你能帮我解决这个问题吗?

根据@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  

您可以使用下一个查询,但有一个例外 - 查询没有 return 0 个结果,但可以在可视化级别解决:

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

使用相关子查询:

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
参见 demo.

在MySql 8.0+中你可以使用FIRST_VALUE() window函数:

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

参见demo

如果您想要 1 行中所有 f_filestatus 的结果:

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

参见demo
结果:

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

在 MySQL 8.0 中,我会推荐 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 = ?

适用于所有版本的替代方法是相关子查询:

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

为了提高第二个查询的性能,请考虑 (bank_id, filestatus, fs_id desc) 上的索引。使用此索引,该查询可能 运行 比使用 row_number() 的第一种方法快,即使在 MySQL 8.0.

中也是如此

如果您想要一次计算所有 filestatus 个数:

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