DimSql as String="SELECT xp.*,"SQL= SQL +"Xf_owner.ownername,"SQL= SQL +"Xf_receiver.receivename,"SQL= SQL +"Lastprice ="SQL= SQL +" ( "SQL= SQL +"SELECT TOP 1 "SQL= SQL +"Lp.tonprice"SQL= SQL +" from"SQL= SQL +" ( "SQL= SQL +"SELECT *"SQL= SQL +"From Xf_price"SQL= SQL +"WHERE Pk_price in ("SQL= SQL +"SELECT TOP 2 "SQL= SQL +"Pk_price"SQL= SQL +"From Xf_price"SQL= SQL +"WHERE pk_pricegroup = xp. Pk_pricegroup"SQL= SQL +"and Pk_owner = xp. Pk_owner"SQL= SQL +"and pk_receiver = xp. Pk_receiver"SQL= SQL +"ORDER by Enabletime DESC"SQL= SQL +" ) "SQL= SQL +") LP"SQL= SQL +"ORDER by Lp.enabletime ASC"SQL= SQL +" ), "SQL= SQL +"Transname = case"SQL= SQL +"When transtype = 0 Then"SQL= SQL +"' External transport '"SQL= SQL +"When transtype = 1 Then"SQL= SQL +"' in-plant transshipment '"SQL= SQL +"ELSE"SQL= SQL +"' off-plant transshipment '"SQL= SQL +"END"SQL= SQL +"from xf_price xp "SQL= SQL +"Left JOIN Xf_owner"SQL= SQL +"On Xf_owner.pk_owner = Xp.pk_owner"SQL= SQL +"Left JOIN xf_receiver"SQL= SQL +"On xf_receiver.pk_receiver = Xp.pk_receiver"SQL= SQL +"WHERE xp.dr = 0"SQL= SQL +"and Xp.pk_pricegroup = @pk_pricegroup"SQL= SQL +"and Pk_price in ("SQL= SQL +"SELECT TOP 1 "SQL= SQL +"Pk_price"SQL= SQL +"From Xf_price"SQL= SQL +"WHERE pk_pricegroup = xp. Pk_pricegroup"SQL= SQL +"and Pk_owner = xp. Pk_owner"SQL= SQL +"and pk_receiver = xp. Pk_receiver"SQL= SQL +"ORDER by Enabletime DESC"SQL= SQL +" ) "SQL= SQL +"ORDER by Xp.enabletime DESC;" DimCom as Newsqlclient.sqlcommand (SQL, Strcon) com. Parameters.Add (NewSqlclient.sqlparameter ("@pk_pricegroup", Pk_pricegroup)) DimDs as NewDataSetDimAdapter as Newsqlclient.sqldataadapter (COM) strcon.open () adapter. Fill (DS,"Xf_price") Strcon.close ()
Main idea: Take the whole result set first, then the condition takes the primary key of the first record in the reverse order, then there is a field that needs to take the result value of the last modification (alias Lastprice, that is, the value tonprice), the first top 2 Take two data, and then in reverse chronological order, and then top 1 to take the first one is the result of the last modified value.
Note the magic of Xf_price XP, you can solve the problem of displaying only one return record when the primary key condition takes the first one.
Sql_ from the result set of the query to take the last valid data into a new result set (to be optimized)