Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nyKg5-0003eB-AW for pgsql-novice@arkaria.postgresql.org; Mon, 06 Jun 2022 21:54:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nyKg3-0003r2-HJ for pgsql-novice@arkaria.postgresql.org; Mon, 06 Jun 2022 21:54:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nyK1Z-0001D9-RE for pgsql-novice@lists.postgresql.org; Mon, 06 Jun 2022 21:12:33 +0000 Received: from mx0a-00641c01.pphosted.com ([205.220.165.146]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nyK1T-0002Qh-W0 for pgsql-novice@lists.postgresql.org; Mon, 06 Jun 2022 21:12:32 +0000 Received: from pps.filterd (m0247474.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 256L4tBf017667 for ; Mon, 6 Jun 2022 21:12:22 GMT Received: from mxout22.s.uw.edu (mxout22.s.uw.edu [128.95.242.222]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3gg3hnqh4r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 06 Jun 2022 21:12:21 +0000 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by mxout22.s.uw.edu (8.14.4+UW20.07/8.14.4+UW22.04) with ESMTP id 256LC1Wn006870 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=OK) for ; Mon, 6 Jun 2022 14:12:16 -0700 Received: by mail-pg1-f197.google.com with SMTP id 84-20020a630057000000b003f9caa5bccfso7488866pga.9 for ; Mon, 06 Jun 2022 14:12:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=HfEftLLcOBze2HQQlOTNQl7rUxTuuAV9XzJ4fzuHdu0=; b=Su5SOZRfzmH14yYYAYjNZBGbmEfb20pf+rbEknNe0QdQOkUrxJ0EDXE6T/+Zs/jyK0 NYfPWqx3ho1O58HnDfFdPJFRn8Fd3UbAQkHqUkcx+oZwF3N18ZT+yAoysBXiwMpmRlqf aCu2M9GWMzN1WYjml4thAs/HEY6TR6TyIUbSXTw/VadZEbcn5sCdbEmH9I/XLgKgBYZ8 kYCnlkfOfCChnx/T7lDCRieRHB8Z3dibKgO4XtC0UqkIeAuLABxu+Pz2zb8uTojuBQZ9 FFy29uCmTkcZa6n25f18GBQeanc7hxHFKYUxKCKBZUWhzi3okN04PF8aHKZ/fpqmR00K LzZQ== X-Gm-Message-State: AOAM531Pke9UTbCstFg25c4+W7UCaBzighizL/ejZXpLjaOzg0EMxIz2 JXiWnKFbucIJ8WjM+913kJUnIrdN4GOepGgf9LuITMpJisOLXcEfq48YgrNNX17UWWyhwrNB+4R gpvI6ayO/1qusbgCQZ5xD7OFjy/bzcaQ6htIrtlfQKeQWv9Y9nctiK7Un X-Received: by 2002:a17:902:ef45:b0:156:1858:71fc with SMTP id e5-20020a170902ef4500b00156185871fcmr25783976plx.23.1654549936081; Mon, 06 Jun 2022 14:12:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwnLd1J9v16fDoBAlZRmI0wf1aalwpRqJloEa4vVE8VCpEmJHmKkH9vpqsFy2bvX+FMaTB52w== X-Received: by 2002:a17:902:ef45:b0:156:1858:71fc with SMTP id e5-20020a170902ef4500b00156185871fcmr25783954plx.23.1654549935733; Mon, 06 Jun 2022 14:12:15 -0700 (PDT) Received: from thargelion ([2601:602:9e80:d340::9fa0]) by smtp.gmail.com with ESMTPSA id s3-20020a17090ad48300b001e87bd6f6c2sm2832367pju.50.2022.06.06.14.12.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jun 2022 14:12:15 -0700 (PDT) Date: Mon, 6 Jun 2022 14:12:13 -0700 From: Skylar Thompson To: Jean MAURICE Cc: pgsql-novice@lists.postgresql.org Subject: Re: SQL question, TOP 5 and all OTHERS Message-ID: <20220606211213.dntvfnkwauk52fzc@thargelion> X-Accept-Primary-Language: en X-Accept-Secondary-Language: es References: <6D4DD2B0-E735-459E-BE8C-1D63646C5E32@simplelegal.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Proofpoint-GUID: mBhhaXuC5Qy6uaWFJh8oaZU-smKBGMnq X-Proofpoint-ORIG-GUID: mBhhaXuC5Qy6uaWFJh8oaZU-smKBGMnq X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.874,Hydra:6.0.517,FMLib:17.11.64.514 definitions=2022-06-06_07,2022-06-03_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 mlxscore=0 malwarescore=0 bulkscore=0 clxscore=1011 adultscore=0 phishscore=0 priorityscore=1501 mlxlogscore=990 suspectscore=0 spamscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206060085 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Jun 06, 2022 at 09:46:12PM +0200, Jean MAURICE wrote: > Hi Scott, > what about using a Common Table Expression and the clause WITH ? > I am not at home now but you can write something like > > WITH top5 AS (SELECT vendor_name AS vendor_name, > > ?????? count(DISTINCT inv_id) AS "# of Invoices" > > FROM SpendTable > > GROUP BY vendor_name > > ORDER BY "# of Invoices" DESC > > LIMIT 5) > SELECT * FROM top5 > UNION > > SELECT 'all other' AS vendor_name, > > ?????? count(DISTINCT st.inv_id) AS "# of Invoices" > > FROM SpendTable AS st > > WHERE st.vendor_name NOT IN (SELECT vendor_name FROM top5) > > ORDER BY "# of Invoices" DESC There might be a challenge with ties, especially if you don't order by the vendor name since you could get different results even on the same data set, depending on how the query plan goes. It depends on what the OP is looking for, I guess. -- -- Skylar Thompson (skylar2@u.washington.edu) -- Genome Sciences Department (UW Medicine), System Administrator -- Foege Building S046, (206)-685-7354 -- Pronouns: He/Him/His