Sql_ from the result set of the query to take the last valid data into a new result set (to be optimized)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.