在Teradata Assistant与Pyodbc中运行的查询返回不同的结果

时间:2014-04-16 18:30:41

标签: python python-2.7 teradata pyodbc

我在Teradata SQL Assistant和Python 2.7中通过pyodbc运行这个确切的查询(硬编码值和所有)。我从这两种方法中得到了不同的结果,看来pyodbc是不正确的。

SELECT serial_num, event_desc,event_level, count(*) as "Count", MAX(event_ts) as "Latest Event Timestamp"
    FROM mytable
    WHERE
    serial_num in('serial1','serial2')
    AND event_ts >= '2014-01-01 00:00:00.000'
    AND event_ts <= '2014-03-31 23:59:59.999'
    and event_desc = 'My Test Event'
    group by serial_num, event_desc,event_level
    order by serial_num, "Latest Event Timestamp"

Teradata Assistant结果如下所示

serial_num  event_desc      event_level     Count   Latest Event Timestamp
serial1     My Test Event   1               5       3/4/2014 10:03:28.000000
serial2     My Test Event   1               12      3/27/2014 13:01:25.000000
serial2     My Test Event   2               4       3/27/2014 13:32:59.000000

然而,当使用pyodbc在Python中运行时,我会得到不同的结果:

serial_num  event_desc      event_level     Count   Latest Event Timestamp
serial1     'My Test Event' '1'             2       datetime.datetime(2014, 1, 3, 10, 5, 15
serial2     'My Test Event' '2'             1       datetime.datetime(2014, 3, 14, 2, 22, 47
serial2     'My Test Event' '1'             4       datetime.datetime(2014, 3, 22, 6, 36, 40

差异在count和timestamp列中。我不确定查询在python中究竟是做什么的。它返回有效的时间戳,但它们不是group by的最大时间相同且计数不正确。为什么呢?

这是未分组的数据:

serial_num  event_desc      event_level     Latest Event Timestamp
serial1     My Test Event   1               1/2/2014 07:11:22.000000
serial1     My Test Event   1               1/3/2014 10:05:15.000000
serial1     My Test Event   1               1/19/2014 13:32:17.000000
serial1     My Test Event   1               3/4/2014 09:16:15.000000
serial1     My Test Event   1               3/4/2014 10:03:28.000000
serial2     My Test Event   1               1/19/2014 14:04:47.000000
serial2     My Test Event   1               1/28/2014 13:27:00.000000
serial2     My Test Event   2               1/28/2014 13:57:12.000000
serial2     My Test Event   1               2/5/2014 01:36:47.000000
serial2     My Test Event   2               2/5/2014 02:56:53.000000
serial2     My Test Event   1               2/23/2014 01:57:19.000000
serial2     My Test Event   1               2/27/2014 13:50:08.000000
serial2     My Test Event   1               2/28/2014 13:55:51.000000
serial2     My Test Event   1               3/9/2014 15:31:00.000000
serial2     My Test Event   1               3/14/2014 01:31:36.000000
serial2     My Test Event   2               3/14/2014 02:22:47.000000
serial2     My Test Event   1               3/16/2014 03:29:04.000000
serial2     My Test Event   1               3/22/2014 02:07:04.000000
serial2     My Test Event   1               3/22/2014 06:36:40.000000
serial2     My Test Event   1               3/27/2014 13:01:25.000000
serial2     My Test Event   2               3/27/2014 13:32:59.000000

正在运行的python代码是

qry = <<Above SQL>>
conn=pyodbc.connect('DRIVER={Teradata};DBCNAME=server;UID=user;PWD=password;QUIETMODE=YES;', ANSI=True, autocommit=True)
teracurs=self.conn.cursor()
res = teracurs.execute(qry).fetchall()        

如果我在Teradata SQL Assistant中运行,为什么MAX()count语句按预期工作,但如果我通过pyodbc运行查询则不行?

0 个答案:

没有答案