我有一个db2查询,它必须获取列的第一个值(Col6)。我已尝试使用该列的最小值,但它以升序获取第一个值。
表格如下:
Col1 Col2 Col3 Col4 Col5 Col6
10 -1 N/A 1 41 Pack Inv
10 -1 N/A 1 10 Fl Sales
10 -1 N/A 1 10 St Ss Bu
10 -1 N/A 1 41 Pack Inv
Col7的预期输出如下:
Col1 Col2 Col3 Col4 Col5 Col6 Col7
10 -1 N/A 1 41 Pack Inv Pack Inv
10 -1 N/A 1 10 Fl Sales Pack Inv
10 -1 N/A 1 10 St Ss Bu Pack Inv
10 -1 N/A 1 41 Pack Inv Pack Inv
通过使用MIN(Col6)OVER(PARTITION BY Col1)AS“Col7”,生成的输出为:
Col1 Col2 Col3 Col4 Col5 Col6 Col7
10 -1 N/A 1 41 Pack Inv Fl Sales
10 -1 N/A 1 10 Fl Sales Fl Sales
10 -1 N/A 1 10 St Ss Bu Fl Sales
10 -1 N/A 1 41 Pack Inv Fl Sales
我是否知道如何在db2中实现预期的输出。
谢谢
答案 0 :(得分:0)
虽然不清楚数据中是如何定义“第一”列的,但是通过使用import itertools
with open(infilepath) as infile: # open the file
# use itertools.repeat to duplicate the file handle
# use zip to get the pairs of consecutive lines from the file
# use enumerate to get the index of each pair
# offset the indexing of enumerate by 1 (as python starts indexing at 0)
for i, (x,y) in enumerate(zip(*itertools.repeat(infile, 2)), 1):
print("The {}th point is at x={}, y={}".format(i, x, y))
对列进行排名然后选择第一列来解决此一般问题:
row_number()
如果要将所选值添加到每一行,则可以将此结果重新连接到原始表...或者使用with ranked as (
select col1,
col6,
row_number() over (partition by col1 order by [cols that define "first"]) rank
)
select col1, col6 from ranked where rank=1
将其作为新列插入,如果这是您所追求的。