请帮我查一下查询中的错误

时间:2016-10-01 17:11:12

标签: sql sqlite

Hye,

我有表AddClient,它有这样的数据

      idClient  NameClient              Balance  Date

        CL-MK   Muhammad Kashif  CL-MK  9000    2016-10-01
        CL-MA   Asim  CL-MA             8000    2016-10-01
        CL-MQ   Qasim  CL-MQ            7000    2016-10-01
        CL-MS   Saim  CL-MS             1000    2016-10-01
        CL-MF   Fahad  CL-MF            3000    2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MA   Asim  CL-MA                     2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MK   Muhammad Kashif  CL-MK          2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01
        CL-MS   Saim  CL-MS                     2016-10-01
        CL-MK   Muhammad Kashif  CL-MK  409005  2016-10-01
        CL-MK   Muhammad Kashif  CL-MK  400000  2016-10-01
        CL-MA   Asim  CL-MA             566340  2016-10-01
        CL-MA   Asim  CL-MA             560000  2016-10-01
        CL-MS   Saim  CL-MS             401005  2016-10-01
        CL-MS   Saim  CL-MS             400000  2016-10-01
        CL-MF   Fahad  CL-MF                    2016-10-01
        CL-MQ   Qasim  CL-MQ                    2016-10-01

在Balance列中,空格等于null

我正在尝试此查询以获得每个客户端的最低余额

SELECT idClient,NameClient,min(Balance) from AddClient group by NameClient

但结果显示像这样

CL-MA   Asim  CL-MA             560000 
CL-MF   Fahad  CL-MF            3000
CL-MK   Muhammad Kashif  CL-MK  400000
CL-MQ   Qasim  CL-MQ            7000
CL-MS   Saim  CL-MS             1000

在客户名称Asim和Kashif中没有显示正确的结果 Asim的最小余额为8000,Kashif为9000

哪里出错?

1 个答案:

答案 0 :(得分:0)

您需要按客户端的ID和名称对这两者进行分组,而不是仅按名称进行分组:

SELECT idClient, NameClient, min(Balance) 
FROM AddClient 
GROUP BY idClient, NameClient

此外,您用于平衡的数据类型似乎不是数字类型 - 这可以解释您看到的奇怪结果。如果数据类型不是数字,则应首先将其转换为数字类型。下面的代码假设Balance列中的所有值都可以转换为整数值,否则您将获得异常:

SELECT idClient, NameClient, min(CAST(Balance as INTEGER)) 
FROM AddClient 
GROUP BY idClient, NameClient