简体   繁体   中英

T-SQL: Select partitions which have more than 1 row

I've managed to use this query

SELECT 
    PartGrp,VendorPn, customer, sum(sales) as totalSales,
    ROW_NUMBER() OVER (PARTITION BY partgrp, vendorpn ORDER BY SUM(sales) DESC) AS seqnum
FROM 
    BG_Invoice 
GROUP BY 
    PartGrp, VendorPn, customer
ORDER BY 
    PartGrp, VendorPn, totalSales DESC

To get a result set like this. A list of sales records grouped by a group, a product ID ( VendorPn ), a customer, the customer's sales, and a sequence number which is partitioned by the group and the productID.

PartGrp     VendorPn        Customer    totalSales seqnum
------------------------------------------------------------
AGS-AS      002A0002-252    10021013    19307.00    1
AGS-AS      002A0006-86     10021013    33092.00    1
AGS-AS      010-63078-8     10020987    10866.00    1
AGS-SQ      B71040-39       10020997     7174.00    1
AGS-SQ      B71040-39       10020998        2.00    2
AIRFRAME    0130-25         10017232     1971.00    1
AIRFRAME    0130-25         10000122     1243.00    2
AIRFRAME    0130-25         10008637      753.00    3
HARDWARE    MS28775-261     10005623      214.00    1
M250        23066682        10013266      175.00    1

How can I filter the result set to only return rows which have more than 1 seqnum ? I would like the result set to look like this

PartGrp     VendorPn        Customer    totalSales seqnum
------------------------------------------------------------
AGS-SQ      B71040-39       10020997    7174.00     1
AGS-SQ      B71040-39       10020998       2.00     2
AIRFRAME    0130-25         10017232    1971.00     1
AIRFRAME    0130-25         10000122    1243.00     2
AIRFRAME    0130-25         10008637     753.00     3

Out of the first result set example, only rows with VendorPn "B71040-39" and "0130-25" had multiple customers purchase the product. All products which had only 1 customer were removed. Note that my desired result set isn't simply seqnum > 1 , because i still need the first seqnum per partition.

I would change your query to be like this:

SELECT PartGrp,
       VendorPn,
       customer,
       sum(sales) as totalSales,
       ROW_NUMBER() OVER (PARTITION BY partgrp,vendorpn ORDER BY SUM(sales) DESC) as seqnum,
       COUNT(1) OVER (PARTITION BY partgrp,vendorpn) as cnt
FROM BG_Invoice 
GROUP BY PartGrp,VendorPn, customer
HAVING cnt > 1
ORDER BY PartGrp,VendorPn, totalSales desc

You can try something like:

SELECT PartGrp,VendorPn, customer, sum(sales) as totalSales,
ROW_NUMBER() OVER (PARTITION BY partgrp,vendorpn ORDER BY SUM(sales) DESC) as seqnum
FROM BG_Invoice 
GROUP BY PartGrp,VendorPn, customer
HAVING seqnum <> '1'
ORDER BY PartGrp,VendorPn, totalSales desc
WITH CTE AS (
    SELECT 
        PartGrp,VendorPn, customer, sum(sales) as totalSales,
        ROW_NUMBER() OVER (PARTITION BY partgrp, vendorpn ORDER BY SUM(sales) DESC) AS seqnum
    FROM 
        BG_Invoice 
    GROUP BY 
        PartGrp, VendorPn, customer)
SELECT DISTINCT
    a.*
FROM
    CTE a
JOIN
    CTE b
        ON a.PartGrp = b.PartGrp
        AND a.VendorPn = b.VendorPn
WHERE
    b.seqnum > 1
ORDER BY 
    a.PartGrp,
    a.VendorPn,
    a.totalSales DESC;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM