Given the following code:
DECLARE @data TABLE (
[pk] [int],[identifier] [varchar](32),[date] [date],[supplier] [varchar](32),[product] [varchar](32),[office] [varchar](32),
[quantity] [numeric](18,4),[returned_quantity] [numeric](18,4),[value] [numeric](18, 4),[reference] [varchar](32),
[purchase_order] [varchar](32),[project] [varchar](32),[pct] [int]
)
INSERT INTO
@data([pk],[identifier],[date],[supplier],[product],[office],[quantity],[returned_quantity],[value],[reference],[purchase_order],[pct])
VALUES
(9363205,'TESTING','2021-01-22','123','ABCDEF123K','207',2000,0,130.2,'4564567/1','335599',86),
(9363306,'TESTING','2021-01-22','123','QXY1015-POIUY','207',200,0,12.74,'4564567/1','335599',86),
(9363316,'TESTING','2021-01-22','123','QXY1015-LKJHG','207',300,0,19.11,'4564567/1','335599',86),
(9363374,'TESTING','2021-01-22','123','NGCDSR67890-WHT','207',1000,0,50.4,'4564567/1','335599',86),
(9363372,'TESTING','2021-01-22','123','NGCDSR67890-BLK','207',1000,0,50.4,'4564567/1','335599',86),
(9363373,'TESTING','2021-01-22','123','NGCDSR67890-GRY','207',1000,0,50.4,'4564567/1','335599',86),
(9363382,'TESTING','2021-01-22','123','NGCDSR67891-BRN','207',200,0,16.8,'4564567/1','335599',86),
(9363381,'TESTING','2021-01-22','123','NGCDSR67891-BLK','207',200,0,16.8,'4564567/1','335599',86),
(9363386,'TESTING','2021-01-22','123','ZXDFGH4523-BLK','207',600,0,44.1,'4564567/1','335599',86),
(9363385,'TESTING','2021-01-22','123','ZXDFGH4523-BLU','207',600,0,44.1,'4564567/1','335599',86),
(9363384,'TESTING','2021-01-22','123','ZXDFGH4523-BRN','207',600,0,26.46,'4564567/1','335599',86),
(9363390,'TESTING','2021-01-22','123','ZXDFGH4523-GRY','207',1000,0,44.1,'4564567/1','335599',86)
SELECT
*,
ROW_NUMBER() OVER(PARTITION BY [identifier],[supplier],LEFT([product],12),[office],[reference],[purchase_order],[pct] ORDER BY [value] DESC) AS [order],
COUNT(*) OVER(PARTITION BY [identifier],[supplier],LEFT([product],12),[office],[reference],[purchase_order],[pct] ORDER BY [value] DESC) AS [count]
FROM
@data
GO
I am getting the following results:
pk | identifier | date | supplier | product | office | quantity | returned_quantity | value | reference | order | project | pct | order | count
9363205 | TESTING | 2021-01-22 | 123 | ABCDEF123K | 207 | 2000.0000 | 0.0000 | 130.2000 | 4564567/1 | 335599 | NULL | 86 | 1 | 1
9363374 | TESTING | 2021-01-22 | 123 | NGCDSR67890-WHT | 207 | 1000.0000 | 0.0000 | 50.4000 | 4564567/1 | 335599 | NULL | 86 | 1 | 3
9363372 | TESTING | 2021-01-22 | 123 | NGCDSR67890-BLK | 207 | 1000.0000 | 0.0000 | 50.4000 | 4564567/1 | 335599 | NULL | 86 | 2 | 3
9363373 | TESTING | 2021-01-22 | 123 | NGCDSR67890-GRY | 207 | 1000.0000 | 0.0000 | 50.4000 | 4564567/1 | 335599 | NULL | 86 | 3 | 3
9363382 | TESTING | 2021-01-22 | 123 | NGCDSR67891-BRN | 207 | 200.0000 | 0.0000 | 16.8000 | 4564567/1 | 335599 | NULL | 86 | 1 | 2
9363381 | TESTING | 2021-01-22 | 123 | NGCDSR67891-BLK | 207 | 200.0000 | 0.0000 | 16.8000 | 4564567/1 | 335599 | NULL | 86 | 2 | 2
9363316 | TESTING | 2021-01-22 | 123 | QXY1015-LKJHG | 207 | 300.0000 | 0.0000 | 19.1100 | 4564567/1 | 335599 | NULL | 86 | 1 | 1
9363306 | TESTING | 2021-01-22 | 123 | QXY1015-POIUY | 207 | 200.0000 | 0.0000 | 12.7400 | 4564567/1 | 335599 | NULL | 86 | 1 | 1
9363386 | TESTING | 2021-01-22 | 123 | ZXDFGH4523-BLK | 207 | 600.0000 | 0.0000 | 44.1000 | 4564567/1 | 335599 | NULL | 86 | 1 | 2 <--
9363385 | TESTING | 2021-01-22 | 123 | ZXDFGH4523-BLU | 207 | 600.0000 | 0.0000 | 44.1000 | 4564567/1 | 335599 | NULL | 86 | 2 | 2 <--
9363384 | TESTING | 2021-01-22 | 123 | ZXDFGH4523-BRN | 207 | 600.0000 | 0.0000 | 26.4600 | 4564567/1 | 335599 | NULL | 86 | 3 | 3 <--
9363390 | TESTING | 2021-01-22 | 123 | ZXDFGH4523-GRY | 207 | 1000.0000 | 0.0000 | 44.1000 | 4564567/1 | 335599 | NULL | 86 | 1 | 1
Looking at the highlighted lines, the ROW_NUMBER() is working correctly (1, 2, 3) but the COUNT(*) has the wrong number for the first two entries (2, 2, 3). Given that the OVER() information is the same and that the 3rd entry has the correct count, why is the [count] value incorrect for the first two rows?
Thanks.
Solved it! Typical, just after posting the question :-/
Need to remove "ORDER BY" from the COUNT(*) window function. Corrected line is:
COUNT(*) OVER(PARTITION BY [identifier],[supplier],LEFT([product],12),[office],[reference],[purchase_order],[pct]) AS [count]
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply