我如何在PostgreSQL中有条件地找到模式

时间:2017-06-11 10:15:55

标签: sql postgresql conditional mode

我在PostgreSQL 9.5数据库中有一些示例数据,如下所示:

def gen_records(record_name, img_path_file, label_map):
    writer = tf.python_io.TFRecordWriter(record_name)
    classes = []

    with open(label_map, 'r') as f:
        for l in f.readlines():
            classes.append(l.split(',')[0])

    with open(img_path_file, 'r') as f:
        lines = f.readlines()
        num_images = len(lines)
    print 'total number to be written' + str(num_images)
    print 'start writing...'

    patches = []
    with open(img_path_file, 'r') as f:
        for patch in f.readlines():
            patches.append(patch[:-1])

    cnt = 0
    for patch in patches:
        cnt += 1
        # print '[' + str(cnt) + ' / ' + str(num_images) + ']' + 'writing  ' + str()
        img = tf.image.resize_images(np.array(Image.open(patch)), (224, 224), method=tf.image.ResizeMethod.BILINEAR)
        img_raw = np.array(img).tostring()
        label = classes.index(patch.split('/')[1])
        example = tf.train.Example(features=tf.train.Features(feature={
            'label': _int64_feature(int(label)),
            'image': _bytes_feature(img_raw)
        }))

        writer.write(example.SerializeToString())

    writer.close()

对于每个组,我想使用PostgreSQL Group length A 19.3 A 19.3 A 20.3 A 20.3 A 19.3 A 19.3 B 22.1 B 19.3 B B 15.5 B 12.8 B 14.7 函数有条件地找到模式(最重复/常用值),这样:

  • 如果长度为null,请将其替换为0.0
  • 查找每个/唯一群组的模式
  • 如果有两种模式,则返回最大值
  • 如果模式不存在,则为该组返回0.0

所需的输出可能如下:

mode()

我如何有条件地找到模式,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

我认为你的条件都归结为:

select group,
       mode() within group (order by coalesce(length, 0.0) desc)
from t
group by group

如果您有group行,则mode()无法返回NULL,因此不需要进一步外COALESCE()