如何统计数据库中的记录并放入List<Long>,每一行用字母表示?

How to count records from database and put them into List<Long>,each row represent by the letter?

我需要从数据库中获取值并将它们放入 List<Long>。我进行了本机查询,但无法正常工作。这是我数据库中列的图片:

我需要统计相同的状态(“W”和“I”代表相同的状态但用另一个字母表示)。 我准备了如下所示的查询:

@Query(value = "SELECT COUNT(CASE WHEN table.status = 'W' OR  table.status = 'I' THEN 1 ELSE 0 END) AS WI,"
            " COUNT(CASE WHEN table.status = 'S' THEN 1 ELSE 0 END) AS S," +
            " COUNT(CASE WHEN table.status = 'N' THEN 1 ELSE 0 END) AS N," +
            " COUNT(CASE WHEN table.status = 'D' THEN 1 ELSE 0 END) AS D," +
            " COUNT(CASE WHEN table.status = 'E' THEN 1 ELSE 0 END) AS E," +
            " COUNT(CASE WHEN table.status = 'T' THEN 1 ELSE 0 END) AS T" +
            " FROM table.table table" +
            " WHERE table.id = :id"+
            " GROUP BY table.status" +
            " ORDER BY table.status ASC",
            nativeQuery = true)
    List<Long> query1 (Long id);

但它无法正常工作。当我 运行 查询时,我得到以下结果:

但应该是 {2(D),2(E),1(N),1(S),1(T),3(W 和 I)。 您知道如何解决它吗?

本次查询returns你想要什么:

select count(*) from (
    SELECT CASE WHEN status = 'W' OR  status = 'I' THEN 'WI' else status end as status
    FROM @table 
) as a
group by status
order by status

也许这就是您需要的代码(您的 table 名称非常复杂):

@Query(value = "select count(*) from (" + 
            " SELECT CASE WHEN table.status = 'W' OR table.status = 'I' THEN 'WI' else table.status end as status" +
            " FROM table.table table " +
            " WHERE table.id = :id"+
            " ) as a " +
            " group by status" +
            " order by status",
            nativeQuery = true)
    List<Long> query1 (Long id);