0

我的目标是,对于每一个PID,选择 2 条记录,它们的test_sname值分别为 ' want ' 和 ' want2 ',它们出现在同一条记录中entry_date。我为前 5 个entry_dates包含两者的 test_snames.

这是我完成此任务的查询:

queryBuilder = 
"""select PID, test_sname, test_value, units, ref_range, entry_date from labs
   where PID=%s and (test_sname='want' or test_sname='want2') and entry_date in

   (select entry_date from labs where PID=%s and test_sname in ('want', 'want2')
   group by entry_date having count(*) = 2) 

   order by entry_date limit 10;""" % (pid, pid)

当 entry_date 只有两行包含test_sname' want ' 或 ' want2 ' 时,它会按预期工作。

PID      |test_sname  |test_value  |units    |entry_date
10000000 | want       |         343 | U/L     | 2008-01-01 01:01:01
10000000 | want2      |      984.34 |         | 2008-01-01 01:01:01
10000000 | NA1        |          56 | %       | 2008-01-01 01:01:01
10000000 | NA2        |         420 | mg/dL   | 2008-01-01 01:01:01
10000000 | NA2        |         420 | mg/dL   | 2008-01-02 01:01:01

10000000 | want       |         343 | U/L     | 2008-01-02 01:01:01
10000000 | want2      |      984.34 |         | 2008-01-02 01:01:01
10000000 | NA1        |          26 | %       | 2008-01-02 01:01:01
10000000 | NA2        |         410 | mg/dL   | 2008-01-02 01:01:01
10000000 | NA2        |         455 | mg/dL   | 2008-01-02 01:01:01

查询结果(正确):

PID      |test_sname  |test_value  |units    |entry_date
10000000 | want       |         343 | U/L     | 2008-01-01 01:01:01
10000000 | want2      |      984.34 |         | 2008-01-01 01:01:01
10000000 | want       |         343 | U/L     | 2008-01-02 01:01:01
10000000 | want2      |      984.34 |         | 2008-01-02 01:01:01

例如,当在同一个 entry_date 上有来自test_sname' wanthaving count(*) = 2 ' 的多行时,就会出现问题,因为 'want'不再有效。这样的数据没有结果。

PID      |test_sname  |test_value  |units    |entry_date
11111111 | want       |         343 | U/L     | 2009-10-26 07:25:00
11111111 | want2      |      984.34 |         | 2009-10-26 07:25:00
11111111 | want       |        189 | U/L     | 2009-10-26 07:25:00
11111111 | NA1        |         50 | %       | 2009-10-26 07:25:00
11111111 | NA2        |         40 | mg/dL   | 2009-10-26 07:25:00
11111111 | NA3        |      84.55 |         | 2009-10-26 07:25:00
11111111 | NA4        |        4.5 | thou/uL | 2009-10-26 07:25:00
11111111 | NA5        |       14.6 | g/dL    | 2009-10-26 07:25:00
11111111 | NA6        |       0.96 | mg/dL   | 2009-10-26 07:25:00

11111111 | want       |         343 | U/L     | 2009-10-30 07:25:00
11111111 | want2      |      984.34 |         | 2009-10-30 07:25:00
11111111 | want       |        189 | U/L     | 2009-10-30 07:25:00
11111111 | NA1        |          6 | %       | 2009-10-30 07:25:00
11111111 | NA2        |         40 | mg/dL   | 2009-10-30 07:25:00
11111111 | NA3        |      84.55 |         | 2009-10-30 07:25:00
11111111 | NA4        |        4.5 | thou/uL | 2009-10-30 07:25:00
11111111 | NA5        |       14.6 | g/dL    | 2009-10-30 07:25:00
11111111 | NA6        |       0.96 | mg/dL   | 2009-10-30 07:25:00

作为限制,我尝试limit 2在子查询中添加一个(我知道它本身不能解决问题),但它给出了这个错误,我认为我有最新版本的 SQL,所以显然我不能在子查询中使用limit

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

我意识到有多种方法可以解决这个问题 - 我可以选择所有值,然后以编程方式使用 Python 获取我需要的内容,但我正在寻找使用 Python mySQL-connector 编写的 mySQL 查询解决方案。不过,我不会抱怨 python 解决方案。

我将 python v3.4.4 与 mySQL-connector v2.1.3 和 MySQL 服务器 v5.7.11 一起使用

谢谢你的时间!

4

1 回答 1

1

考虑通过子查询使用分组的运行计数。然后,过滤 RowNo 为 1 或 2 的任何位置。这样,您无需传递参数,因为所有 PID 都会被处理。下面假设 labs 表具有唯一标识符ID

SELECT * 
FROM
   (SELECT PID, test_sname, test_value, units, ref_range, entry_date,    
           (SELECT count(*) FROM labs sub
            WHERE sub.test_sname in ('want', 'want2')
            AND sub.PID = labs.PID
            AND sub.entry_date = labs.entry_date
            AND sub.ID <= labs.ID) As RowNo
    FROM labs
    WHERE test_sname in ('want', 'want2')
   ) As dT
WHERE dT.RowNo <= 2

#  PID     test_sname   test_value      units   ref_range              entry_date   RowNo
#  10000000      want           33        U/L        4-40     2008-01-01 01:01:01       1
#  10000000     want2        98.34                            2008-01-01 01:01:01       2
#  10000000      want           33        U/L        4-40     2008-01-02 01:01:01       1
#  10000000     want2        98.34                            2008-01-02 01:01:01       2
#  11111111      want           33        U/L      Apr-40     2009-10-26 07:25:00       1
#  11111111     want2        98.34                            2009-10-26 07:25:00       2
#  11111111      want           33        U/L      Apr-40     2009-10-30 07:25:00       1
#  11111111     want2        98.34                            2009-10-30 07:25:00       2
于 2016-04-30T04:01:17.007 回答