我一直试图在查询中排序一天的最后一个条目,其中每个实体获得24行。我已经在this thred中阅读了与sollution相当类似的问题,但我无法将其纳入我现有的查询中。
我现有的查询:
SELECT distinct b.Udc_id as EAN, sax.attrib_01, c.channel_no, a.local_read_time, a.read, a.Status
from register_reads a, lookup_mv_channels1 c, lookup_mv_sdps b
LEFT outer JOIN s_asset@mudr2sebl sa
ON sa.x_udc_asset_id = b.Udc_id
LEFT outer JOIN s_asset_xm@mudr2sebl sax
ON sax.par_row_id = sa.row_id
WHERE c.Channel_id = a.channel_id
AND b.meter_id = c.meter_id
AND sax.attrib_02 = 'Y'
AND a.local_read_time >= (SYSDATE) - 1
order by b.Udc_id, a.local_read_time
这样做的结果将是例如是:
||EAN | ATTRIB_01 | channel_no | LOCAL_READ_TIME | READ | STATUS||
||1234| ready | 1 |09/11/2015 01:00:01| 0| val ||
||1234| ready | 1 |09/11/2015 02:00:01| 10| val ||
||1234| ready | 1 | ... | ...| val ||
||1234| ready | 1 |09/11/2015 23:59:01| 60| val ||
||3456| ready | 1 |09/11/2015 01:00:01| 204| val ||
||3456| ready | 1 | ... | ...| val ||
||3456| ready | 1 |09/11/2015 23:58:01| 400| val ||
我想要达到的结果是:
||EAN | ATTRIB_01 | channel_no | LOCAL_READ_TIME | READ | STATUS||
||1234| ready | 1 |09/11/2015 23:59:01| 60| val ||
||3456| ready | 1 |09/11/2015 23:58:01| 400| val ||
来自@levelonehuman的反馈后更改:
SELECT b.Udc_id as EAN,
c.physical_channel_no,
a.local_read_time,
a.cum_read,
a.validation_Status
FROM register_reads a, lookup_mv_channels1 c, lookup_mv_sdps b
INNER JOIN (SELECT b.Udc_id,
MAX(a.LOCAL_READ_TIME) AS LOCAL_READ_TIME
FROM register_reads, lookup_mv_sdps b
GROUP BY b.Udc_id) AS t2
ON b.Udc_id = t2.Udc_id AND b.LOCAL_READ_TIME = t2.LOCAL_READ_TIME
;
答案 0 :(得分:1)
注意:这适用于def test_export_timesheet(self):
change_url = '/admin/core/timesheet/'
#response contains the generated file using openpyxl
response = self.client.post(change_url, {'action': 'export_xlsx', '_selected_action': [x.id for x in timesheets]})
content = response._headers.get('content-disposition')[1]
start = content.find('=') + 1
end = content.find('.xlsx')
content_path = (content[start:end]+'.xlsx')
#Passing file like object
wb = load_workbook(BytesIO(filename="'"+content_path+"'"))
ws = wb.get_sheet_by_name(name="'" + content[start:end] + "'")
for row in ws.iter_rows():
for cell in row:
print cell.value
您需要为其他表添加其他联接,但您可以使用MSSQL
和MAX(LOCAL_READ_TIME)
获取最后一行:
GROUP BY EAN
SELECT EAN,
ATTRIB_01,
channel_no,
LOCAL_READ_TIME,
READ,
STATUS
FROM TableName t1
INNER JOIN ( SELECT EAN,
MAX(LOCAL_READ_TIME) AS LOCAL_READ_TIME
FROM TableName
GROUP BY EAN
) AS t2 ON t1.EAN = t2.EAN AND t1.LOCAL_READ_TIME = t2.LOCAL_READ_TIME
部分将选择INNER JOIN
上有TableName
和EAN
的行 - 然后,您将加入MAX(LOCAL_READ_TIME)
(同一表格)根据TableName
条件选择其余数据。
实际上,这会过滤掉不符合JOIN
条件的行。