November 22, 2020 at 5:20 am
Most folks here won't touch an Excel file. Make life easy on everyone and post the setup script for your question... something like this:
use tempdb;
go
CREATE TABLE SalesTable (
SNO int
,OrdNo int
,AccountNo int
,ItemCategory varchar(20)
,SalesPersonID int
, rnk tinyint
);
GO
INSERT INTO Salestable (sno, ordno, AccountNo, ItemCategory, SalespersonID, rnk)
VALUES (2,77,123,'item1',5,1)
,(2,77,123,'item1',5,1)
,(2,77,123,'item1',5,1)
,(2,86,22,'item1',2,2)
,(2,86,22,'item1',6,2)
,(3,99,36,'Item2',5,1)
,(3,99,36,'Item2',9,1)
,(3,120,225,'Item2',2,2)
,(3,120,225,'Item2',6,2);
-- I need to group by item category and then rank them by ordbo
SELECT sno, ordno, accountno, itemCategory
, grp = rank() over (partition by itemCategory order by ordno)
FROM SalesTable;
Not totally sure that's what you wanted, but at least now you're closer to an answer... but the short answer is to use a windowing function to do it.
November 22, 2020 at 6:08 pm
Thanks SSCU guru point taken.
I ran the query and it s returning
What I am looking for is , I want to rank orderno within each item category
Any help appreciated
November 22, 2020 at 7:02 pm
Use DENSE_RANK instead of RANK in pietlinden's code.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 22, 2020 at 7:29 pm
Awesome
November 22, 2020 at 7:33 pm
Awesome
The question now is... do you know how and why it works?
https://www.google.com/search?dense_rank+sql+server
--Jeff Moden
Change is inevitable... Change for the better is not.
November 22, 2020 at 10:12 pm
Yes Sir, Dense rank does skip the consecutive numbers.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply