Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

COUNT DISTINCT WITHIN WINDOW

$
0
0

Hi All,

I am trying to get a distinct count within a window. For example:

use tempdb;

IF OBJECT_ID('T1', 'U') IS NOT NULL
DROP TABLE T1;

CREATE TABLE T1 (
	ShipperID int primary key,
	OrderID int,
	PickID int
	);

INSERT INTO T1 
VALUES (47243, 703, 10653), (67268, 703, 40673), (6729,  703, 40697)

/*
ShipperID	OrderID	PickID
47243		703		10653
67268		703		40673
67269		703		40697
*/


INSERT INTO T1 
VALUES (67274, 30743, 40709), (67275, 30743, 40709), (67276, 30743, 40709)

/*
ShipperID	OrderID	PickID
67274		30743	40709
67275		30743	40709
67276		30743	40709
*/

SELECT ShipperID,
OrderID,
PickID,
ROW_NUMBER() OVER (PARTITION BY OrderID ORDER BY PickID) AS BackOrderCount, 
ROW_NUMBER() OVER (PARTITION BY  OrderID, PickID ORDER BY ShipperID) AS SplitShipCount
FROM 
T1 

I want my BackOrderCount for OrderID 30743 rows to be 1 i.e. I only want my BackOrderCount to increment for each unique PickID within an OrderID.

Any assistance much appreciated.

Thanks.

Clay



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>