Home > database > SQL have such operations, such as a field in a table, found to field a=1 data apart is the largest l
SQL have such operations, such as a field in a table, found to field a=1 data apart is the largest l
Time:10-07
My watch is such a ` ` ` ` a num field is continued, continuous line 1 of renewal, a code number is belong to, and there's a CREATE_DATE creation time, I want to check is ` ` ` in this week, for example, two of the same code to record the largest number apart (num field subtraction)
CodePudding user response:
Between two successive code, such as the num=10 code=3, num=20 code=3, num=50 code=3, found out is value is 30 (50-20=30)
CodePudding user response:
Select code, Max (num) from ( Select code, num - (select num from TB b where a.c ode=biggest ode and b.n um & lt; A.n um order by biggest reate_date desc, b.n um desc limit 1) as num The from TB a ) data group by code