The function I want to implement is to count the first three records of each order in the Order Log table, with the following table structure:
An order in the Fixed bar log table has more than one record, according to the time to query out the first three records of each order, SQL as follows:
SelectB.ordernumber,b.creationtime,b.remark from ( SELECTA.ordernumber,a.creationtime,a.remark from [Fortunelabford].[dbo].[So_log]awhereA.sysidinch ( Select TOP 3Sysid from [Fortunelabford].[dbo].[So_log] whereA.ordernumber=So_log.ordernumberORDER byA.creationtimeDESC)) bGroup byB.ordernumber,b.creationtime,b.remarkORDER byB.ordernumber
The idea is: first in the inner layer according to CreationTime do sort, and then self-related to the table, according to the primary Key association, and then the result set grouping and sorting, so that the same order of three records are sorted together
This method of querying applies to fields with unique identities in the table, which cannot be checked if there are no uniquely identified fields.
SQL Implementation Group queries take the first few records