SELECT a.ID, a.SerialNumber, b.Remain_Toner_Black BeforeCount, a.Remain_Toner_Black AfterCount FROM ( SELECT A.ID, A.SerialNumber, A.Remain_Toner_Black, ( SELECT COUNT(*) FROM tableName c WHERE c.SerialNumber = a.SerialNumber AND c.ID <= a.ID) AS RowNumber FROM TableName a ) a INNER JOIN ( SELECT A.ID, A.SerialNumber, A.Remain_Toner_Black, ( SELECT COUNT(*) FROM tableName c WHERE c.SerialNumber = a.SerialNumber AND c.ID <= a.ID) AS RowNumber FROM TableName a ) b ON a.SerialNumber = b.SerialNumber AND a.RowNumber = b.RowNumber + 1 WHERE b.Remain_Toner_Black < a.Remain_Toner_Black
OUTPUT
ββββββββ¦ββββββββββββββββββ¦ββββββββββββββ¦βββββββββββββ β ID β SERIALNUMBER β BEFORECOUNT β AFTERCOUNT β β βββββββ¬ββββββββββββββββββ¬ββββββββββββββ¬βββββββββββββ£ β 7331 β Z5UEBJAC900002Y β 36 β 100 β β 7088 β 3960125290 β 0 β 93 β β 7100 β 3960125290 β 93 β 100 β ββββββββ©ββββββββββββββββββ©ββββββββββββββ©βββββββββββββ
SHORT DESCRIPTION
What the request does above, it generates a serial number that mimics ROW_NUMBER()
in other RDBSs for each SerialNumber
, sorted by ID
in ascending order.
Then the two subqueries are connected via SerialNumber
and the generated serial number. According to the generated number, the value in the first subquery must be equal to plus one of the values ββin the second subquery to get the amount of toner at the next level.
source share