Select rows with second minimum rank in SQL Server -


I have a table with customer number, order number, and amp; Rank field Each customer number can have multiple orders with different rank values. East.

  Customer # Order # Rank 1 12 1 1 13 3 1 14 2 2 15 2 2 16 1 3 17 3 3 18 4 3 19 1 3 20 2  

I am using this table to populate another table that looks like this.

  cust order1 order2 order3 order4 1 12 14 13 2 16 15 3 19 20 17 18 Then, i  order2, order3, order4 < / Code> How to select the second, third minimum rank to populate the field? 

EDIT: I do not want to be classed as 1,2,3, because sometimes there can be no RAN 2, so the order 2 field will be empty, but the order 3 field will be populated. Will go. I want to be from the lowest rank. Second lowest rank in order of 1 minimum rank, order 2 etc. Use a combination of

ROW_NUMBER () and PIVOT < / Code>, like this:

  Declaration @tump table (Customer INT, OrderNumber INT, Ranking INT) @tmp Enter (Customer, Serial Number, Ranking) Selection 1, 12, 1 Union Select 1, 13, 3 Union - 1, 14, 2 Select Union 2, 15, 2 Select Union 2, 16, 1 Union Select 3, 17, 3 Union Select 3, 18, 4 Union Select 3, 19, 1 union selection 3, 20, 2 select the customer, Max (order 1) as Order 1, Max (Or Order 2) Choose Order 4 as Order 2, Ox 3, Order 3, Order 3, Max (Order 4) *, 'Order' + CST (ROW_NUMBER ()) (VARCHAR (4)) AS RNF) D. PIVOT (Max. Order number) for RAN ([Order 1], [Order 2] as]], [Order 3], [O Rder4])) Group by Customer  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -