We are trying to rank Invoice column and Only want first shipment Invoices among parts of Invoices.
That is in that, column we have multiple Invoices. We would like to select that group of invoices among rank column which consist of Earliest Timestamp.
Nature of TimeStamp Column:
The SQL Server timestamp data type has nothing to do with times or dates it is just a binary number to indicate a sequence.The timestamp column is recorded and incremented each time a record is added.
We tried following query :
select *
from
(select
myrank = rank() over (partition by a.SalesOrder order by c.Invoice),
c.Invoice, c.TimeStamp
from
OrderDetail a
left join
OrderMaster b on a.SalesOrder = b.SalesOrder
left join
InvoiceDetail c on a.SalesOrder = c.SalesOrder
and a.SalesOrderLine = c.SalesOrderLine
where
a.SalesOrder = '218438'
and c.LineType = '1'
and b.OrderStatus = '9' ) z
Sample Data:
myrank | SalesOrder | SalesOrderLine |Invoice | TimeStamp
1 218438 8 207964 0x0000000048CD8CD4
2 218438 1 207965 0x0000000048CD8CD5
2 218438 2 207965 0x0000000048CD8CD6
2 218438 3 207965 0x0000000048CD8CD7
2 218438 4 207965 0x0000000048CD8CD8
2 218438 5 207965 0x0000000048CD8CD9
2 218438 6 207965 0x0000000048CD8CDA
2 218438 7 207965 0x0000000048CD8CDB
Expected Data:
Record Should be rank as First shipment invoice and First Added Record on the basis of timestamp What should be the output If i select 2 as my rank
myrank | SalesOrder | SalesOrderLine |Invoice | TimeStamp
1 218438 8 207964 0x0000000048CD8CD4
Thanks in advance !