如何从小组中获得最大价值?

时间:2019-09-18 18:09:48

标签: sql-server tsql

试图从其他列的组中获取最大值。我已经在线研究了示例,但似乎不适用于我的查询。 COL1在我的表中是唯一的。尝试使用MAX(COL1)获得重复的行。

SELECT COL1,
        COL2, COL3,
        cnt, max(COL1)
FROM   (SELECT *,
               ROW_NUMBER() OVER (PARTITION BY COL2, COL3  ORDER BY COL1 desc) AS cnt
        FROM   TABLE_1 
        where ( COL2 is not null and COL3 is not null) e

    Here is sample data
    -------------------
        COL1   COL2   COL3  
        27     2222   2224   
        28     2222   2224   
        30     2222   2224   
        48     5552   8888   
        50     5552   8888   

expected result
--------------------------

    COL1   COL2   COL3  cnt  MAX(COL1)
    30     2222   2224   1    30
    28     2222   2224   2    30
    27     2222   2224   3    30
    50     5552   8888   1    50
    48     5552   8888   2    50

2 个答案:

答案 0 :(得分:2)

希望这对您有用:

import urllib3
import uuid
from pathlib import Path
from io import BytesIO
from errors import custom_exceptions as cex

BUCKET_NAME = "xxx.yyy.zzz"
POSTERS_BASE_PATH = "assets/wallcontent"
CLOUDFRONT_BASE_URL = "https://xxx.cloudfront.net/"


class S3(object):
    def __init__(self):
        self.client = boto3.client('s3')
        self.bucket_name = BUCKET_NAME
        self.posters_base_path = POSTERS_BASE_PATH

    def __download_image(self, url):
        manager = urllib3.PoolManager()
        try:
            res = manager.request('GET', url)
        except Exception:
            print("Could not download the image from URL: ", url)
            raise cex.ImageDownloadFailed
        return BytesIO(res.data)  # any file-like object that implements read()

    def upload_image(self, url):
        try:
            image_file = self.__download_image(url)
        except cex.ImageDownloadFailed:
            raise cex.ImageUploadFailed

        extension = Path(url).suffix
        id = uuid.uuid1().hex + extension
        final_path = self.posters_base_path + "/" + id
        try:
            self.client.upload_fileobj(image_file,
                                       self.bucket_name,
                                       final_path
                                       )
        except Exception:
            print("Image Upload Error for URL: ", url)
            raise cex.ImageUploadFailed

        return CLOUDFRONT_BASE_URL + id

答案 1 :(得分:0)

您可以使用DENSE_RANK来获取分区内的排名,例如

SELECT COL2, COL3, COL1, cnt
FROM
    (SELECT *,
     DENSE_RANK() OVER (PARTITION BY COL2, COL3 ORDER BY COL1 DESC) AS cnt
     FROM TABLE_1
     WHERE COL2 IS NOT NULL AND COL3 IS NOT NULL)
WHERE cnt = 1