row-number

SQL Server : row_number over primary key

主宰稳场 提交于 2020-06-01 08:58:19
问题 Is there any way to create column that will be increased and reset over primary key? Example: Table A ([Code], [Type], [Line No_]) Primary key is ([Code], [Type]) And when I add a new row, I want to auto generate [Line No_] like this: Code Type Line No_ ----------------------- 'U1' 0 1000 'U1' 0 2000 'U1' 1 1000 Something like ROW_NUMBER but auto generated on insert row 回答1: No, you can't use a window function in a computed column. Computed columns must be scalar values. However, this is a

Need to group records based on matching reversal in sql

拈花ヽ惹草 提交于 2020-03-28 07:04:04
问题 I have a tricky scenario to aggregate the data. Data in my source table is as follows. CustomerId Transaction Type Transaction Amount 1 Payment 100 1 ReversePayment -100 1 payment 100 1 ReversePayment -100 1 Payment 100 1 Payment 100 Requirement is as follows: If the payment as a assoociated Reversepayment with matched amount, sum these two records. If the payment does not have an associated Reverse payment, consider it as orphan(dont sum it). I want output to be like this. CustomerId

Need to group records based on matching reversal in sql

谁说胖子不能爱 提交于 2020-03-28 07:02:11
问题 I have a tricky scenario to aggregate the data. Data in my source table is as follows. CustomerId Transaction Type Transaction Amount 1 Payment 100 1 ReversePayment -100 1 payment 100 1 ReversePayment -100 1 Payment 100 1 Payment 100 Requirement is as follows: If the payment as a assoociated Reversepayment with matched amount, sum these two records. If the payment does not have an associated Reverse payment, consider it as orphan(dont sum it). I want output to be like this. CustomerId

need to group records based on matching reversals

南楼画角 提交于 2020-03-04 18:44:00
问题 This question is continuation to my earlier post. Need to group records based on matching reversal in sql I will be more clear with one complex example from my transaction table. Row_Number LOAN_ID TXN_ENTRY_API_NAME TXN_AMT 1 100 ReverseSpreadPayment 2250 2 100 SpreadPayment -2250 3 100 ReverseSpreadPayment 2250 4 100 SpreadPayment -2250 5 100 ReverseSpreadPayment 2250 6 100 SpreadPayment -2250 7 100 ReverseSpreadPayment 2250 8 100 ReverseSpreadPayment 2250 9 100 SpreadPayment 1000 In the

SQL Server 2005 - Row_Number()

五迷三道 提交于 2020-02-25 13:26:48
问题 I'm trying to understand the unusual behaviour seen when ordering results in a descending order using the row_number() function when using a DISITINCT on the outermost select in my query as below: SELECT DISTINCT (ID), State_Id, Name_Of_Trip, Date_Of_Travel, Creation_Date, Locking_Id, Applicant_Name, Reference_Number, State_Name FROM ( SELECT app.ID, app.State_Id, app.Name_Of_Trip, app.Date_Of_Travel, app.Creation_Date, app.Locking_Id, app.Applicant_Name, app.Reference_Number, State.Name AS

SQL Server 2005 - Row_Number()

时间秒杀一切 提交于 2020-02-25 13:26:35
问题 I'm trying to understand the unusual behaviour seen when ordering results in a descending order using the row_number() function when using a DISITINCT on the outermost select in my query as below: SELECT DISTINCT (ID), State_Id, Name_Of_Trip, Date_Of_Travel, Creation_Date, Locking_Id, Applicant_Name, Reference_Number, State_Name FROM ( SELECT app.ID, app.State_Id, app.Name_Of_Trip, app.Date_Of_Travel, app.Creation_Date, app.Locking_Id, app.Applicant_Name, app.Reference_Number, State.Name AS

Need to delete duplicate records from the table using row_number()

僤鯓⒐⒋嵵緔 提交于 2020-01-24 17:27:45
问题 I am having a table test having data as follows and I want to delete the trsid 124 and I have millions entry in my DB it is just a scenarion. Concept is to delete the duplicate entry from the table -------------------------------------------- TrsId | ID | Name | -------------------------------------------- 123 | 1 | ABC | 124 | 1 | ABC | I am trying something like delete from test select T.* from ( select ROW_NUMBER() over (partition by ID order by name) as r, Trsid, ID, name from test ) t

Duplicates removal using Group By, Rank, Row_Number

丶灬走出姿态 提交于 2020-01-16 09:49:28
问题 I have two tables. One is CustomerOrders and the other is OrderCustomerRef - lookup table. Both tables have one-to-many relationship - one customer may be associated with multiple orders. CustomerOrders table has duplicate Customers (same LName, FName, Email). But they have different Cust_IDs. I need to merge all duplicate contacts in the base Customer table (one-to-one). (this table is not shown here). Step 1: Need to find out which Cust_ID should be merged into which corresponding duplicate

Row_Number() CTE Performance when using ORDER BY CASE

六月ゝ 毕业季﹏ 提交于 2020-01-15 04:53:25
问题 I have a table I'd like to do paging and ordering on and was able to get a query similar to the following to do the work (the real query is much more involved with joins and such). WITH NumberedPosts (PostID, RowNum) AS ( SELECT PostID, ROW_NUMBER() OVER (ORDER BY CASE WHEN @sortCol = 'User' THEN User END DESC, CASE WHEN @sortCol = 'Date' THEN Date END DESC, CASE WHEN @sortCol = 'Email' THEN Email END DESC) as RowNum FROM Post ) INSERT INTO #temp(PostID, User, Date, Email) SELECT PostID, User

Row_Number() CTE Performance when using ORDER BY CASE

空扰寡人 提交于 2020-01-15 04:53:13
问题 I have a table I'd like to do paging and ordering on and was able to get a query similar to the following to do the work (the real query is much more involved with joins and such). WITH NumberedPosts (PostID, RowNum) AS ( SELECT PostID, ROW_NUMBER() OVER (ORDER BY CASE WHEN @sortCol = 'User' THEN User END DESC, CASE WHEN @sortCol = 'Date' THEN Date END DESC, CASE WHEN @sortCol = 'Email' THEN Email END DESC) as RowNum FROM Post ) INSERT INTO #temp(PostID, User, Date, Email) SELECT PostID, User