Microsoft SQL Server只需要查询中最高的结果

时间:2019-08-19 16:28:36

标签: sql-server max

我在下面创建了查询,但是无法从列b.new_versionnumber中仅返回最大值。无论该列的最大值是多少,我都想返回包含该最大值的所有行。

SELECT 
    a.new_AdjustmentAmount as Adjustment_Amount,
    a.new_adjustmenttype as Adjustment_Type,
    a.new_baseprice as Base_Price,
    a.new_price as Net_Price,
    a.new_optionidid as Product,
    a.new_price as Price,
    a.new_locationid as Location_ID,
    f.smx_name as Lab_Name,
    f.smx_addressstreet1 as Address,
    f.smx_city As City,
    f.smx_stateprovince as State,
    f.smx_country as Country,
    a.new_productconfigurationid as Product_Configuration,
    a.new_quoteid as Quote_ID,
    a.new_quantity as Quantity,
    b.new_cpqstatus as Product_Configuration_Status,
    b.new_name as Product_Configuration_Name,
    b.new_quoteid as Quote_ID,
    c.new_approvalstage as Approval_Stage,
    c.new_acquisitiontype as Acquisition_Type,
    c.new_dealcolor as Deal_Color,
    c.new_financing as Finance,
    c.new_grossprofit as Gross_Profit_Percentage,
    c.new_hybridcpr as Hybrid_CPR_Type,
    c.new_leaseintrestrate as Lease_Interest_Rate,
    c.new_terms as Lease_Months,
    c.new_leaseresidual as Lease_Residual,
    c.new_leasetype as Lease_Type,
    c.new_printtype as Print_Type,
    c.new_totalinstrumentprice as Total_Instrument_Price,
    c.new_totalinterfaceallowance as Total_Interface_Allowance,
    c.new_totalreagentprice as Total_Reagent_Price,
    c.new_totalserviceprice as Total_Service_Price,
    c.new_quoteid as Record_ID,
    c.new_name as Name,
    c.new_totalamount as Net_Amount,
    c.new_acquisitiontype as Acquisition_Type,
    c.new_approvalstage as Approval_Stage,
    c.new_name as Quote_Name,
    c.new_opportunityid as Opportunity,
    c.new_printtype as Print_Type,
    c.new_totalamount as Total_Amount,
    c.new_isprimary as Is_Primary,
    c.new_opportunityid as  Opportunity_ID_Reference,
    d.closeprobability as Probability,
    d.transactioncurrencyid as Currency,
    d.customerid as Account,
    d.estimatedclosedate as Estimated_Close_Date,
    d.estimatedvalue as Estimated_Revenue,
    d.name as Name,
    d.ownerid as Account_Manager,
    d.smx_contractsoldtoaddress as Sold_To,
    d.smx_multisite as Multi_Site,
    d.statuscode as Status,
    d.totalamount as Total_Amount,
    e.smx_description as Product_Description,
    e.smx_family as Product_Family,
    e.smx_name as Name,
    e.smx_producttype as Product_Type,
    b.new_versionnumber
FROM 
    new_cpq_lineitem_tmp a
JOIN  
    new_cpq_productconfiguration b ON a.new_productconfigurationid = b.new_cpq_productconfigurationid
JOIN 
    new_cpq_quote c ON b.new_quoteid = c.new_cpq_quoteid
JOIN 
    opportunity d ON c.new_opportunityid = d.opportunityid
JOIN 
    smx_product e ON a.new_optionid = e.smx_productid
JOIN 
    smx_address f ON a.new_locationid = f.smx_addressid
WHERE 
    e.smx_producttype = '180700001'
GROUP BY 
    b.new_versionnumber,
a.new_AdjustmentAmount,
a.new_adjustmenttype,
a.new_baseprice,
a.new_price,
a.new_optionidid,
a.new_price,
a.new_locationid,
f.smx_name,
f.smx_addressstreet1,
f.smx_city,
f.smx_stateprovince,
f.smx_country,
a.new_productconfigurationid,
a.new_quoteid,
a.new_quantity,
b.new_cpqstatus,
b.new_name,
b.new_quoteid,
c.new_approvalstage,
c.new_acquisitiontype,
c.new_dealcolor,
c.new_financing,
c.new_grossprofit,
c.new_hybridcpr,
c.new_leaseintrestrate,
c.new_terms,
c.new_leaseresidual,
c.new_leasetype,
c.new_printtype,
c.new_totalinstrumentprice,
c.new_totalinterfaceallowance,
c.new_totalreagentprice,
c.new_totalserviceprice,
c.new_quoteid,
c.new_name,
c.new_totalamount,
c.new_acquisitiontype,
c.new_approvalstage,
c.new_name,
c.new_opportunityid,
c.new_printtype,
c.new_totalamount,
c.new_isprimary,
c.new_opportunityid,
d.closeprobability,
d.transactioncurrencyid,
d.customerid,
d.estimatedclosedate,
d.estimatedvalue,
d.name,
d.ownerid,
d.smx_contractsoldtoaddress,
d.smx_multisite,
d.statuscode,
d.totalamount,
e.smx_description,
e.smx_family,
e.smx_name,
e.smx_producttype

已经尝试过MaxHaving等。。。没有任何作用。

4 个答案:

答案 0 :(得分:1)

您可以尝试这一方法,它显然不是最佳优化方法,但是它应该可以工作,并且是理解的最简单方法:

var idnum = e.target.id.toString().split("_")[1]
let newList = Array.from(this.state.mylist) //create new array so we don't modify state directly
if (type === 1) {
    let objToUpdate = newList.find((el) =>  el.id === idnum) // grab first element with matching id
    objToUpdate.trueorfalse = !objToUpdate.trueorfalse
    this.setState( { mylist: newList } )
}

您只需按b.new_versionnumber降序排列,然后仅取第一行。效率不高,因为您必须先获得全部结果,然后再保留一行,但它应该可以工作...

我希望它会有所帮助。 :-)

答案 1 :(得分:1)

您需要在子查询中找到最大版本号,并在联接中包含该版本号。这样...

INNER JOIN (
               SELECT MAX(new_versionnumber) AS max_new_versionnumber
               FROM   new_cpq_productconfiguration
           ) AS x ON b.new_versionnumber = x.max_new_versionnumber

答案 2 :(得分:1)

new_cpq_productconfiguration表中查找必须为group bypartition by的列,其结果可以满足您的需求。即

JOIN
(select new_productconfigurationid, AdditionalColumn, max (new_versionnumber) as new_versionnumber
 from new_cpq_productconfiguration
 group by new_productconfigurationid, AdditionalColumn,
) as b ON a.new_productconfigurationid = b.new_cpq_productconfigurationid

JOIN
(  select new_productconfigurationid, AdditionalColumn, 
          ROW_NUMBER () OVER (PARTITION BY DesiredColumns order by new_versionnumber desc) as RN
  from    new_cpq_productconfiguration
) as b ON a.new_productconfigurationid = b.new_cpq_productconfigurationid and b.RN = 1

答案 3 :(得分:1)

首先,由于您没有在SELECT部分执行任何汇总,因此您可以仅删除GROUP BY部分。如果您使用它来避免重复,则只需使用distinct。 二,添加  and b.new_versionnumber = (select top 1 new_versionnumber from new_cpq_productconfiguration order by new_versionnumber desc)

到您的WHERE过滤器

最终查询在下面

    SELECT distinct
    a.new_AdjustmentAmount as Adjustment_Amount,
    a.new_adjustmenttype as Adjustment_Type,
    a.new_baseprice as Base_Price,
    a.new_price as Net_Price,
    a.new_optionidid as Product,
    a.new_price as Price,
    a.new_locationid as Location_ID,
    f.smx_name as Lab_Name,
    f.smx_addressstreet1 as Address,
    f.smx_city As City,
    f.smx_stateprovince as State,
    f.smx_country as Country,
    a.new_productconfigurationid as Product_Configuration,
    a.new_quoteid as Quote_ID,
    a.new_quantity as Quantity,
    b.new_cpqstatus as Product_Configuration_Status,
    b.new_name as Product_Configuration_Name,
    b.new_quoteid as Quote_ID,
    c.new_approvalstage as Approval_Stage,
    c.new_acquisitiontype as Acquisition_Type,
    c.new_dealcolor as Deal_Color,
    c.new_financing as Finance,
    c.new_grossprofit as Gross_Profit_Percentage,
    c.new_hybridcpr as Hybrid_CPR_Type,
    c.new_leaseintrestrate as Lease_Interest_Rate,
    c.new_terms as Lease_Months,
    c.new_leaseresidual as Lease_Residual,
    c.new_leasetype as Lease_Type,
    c.new_printtype as Print_Type,
    c.new_totalinstrumentprice as Total_Instrument_Price,
    c.new_totalinterfaceallowance as Total_Interface_Allowance,
    c.new_totalreagentprice as Total_Reagent_Price,
    c.new_totalserviceprice as Total_Service_Price,
    c.new_quoteid as Record_ID,
    c.new_name as Name,
    c.new_totalamount as Net_Amount,
    c.new_acquisitiontype as Acquisition_Type,
    c.new_approvalstage as Approval_Stage,
    c.new_name as Quote_Name,
    c.new_opportunityid as Opportunity,
    c.new_printtype as Print_Type,
    c.new_totalamount as Total_Amount,
    c.new_isprimary as Is_Primary,
    c.new_opportunityid as  Opportunity_ID_Reference,
    d.closeprobability as Probability,
    d.transactioncurrencyid as Currency,
    d.customerid as Account,
    d.estimatedclosedate as Estimated_Close_Date,
    d.estimatedvalue as Estimated_Revenue,
    d.name as Name,
    d.ownerid as Account_Manager,
    d.smx_contractsoldtoaddress as Sold_To,
    d.smx_multisite as Multi_Site,
    d.statuscode as Status,
    d.totalamount as Total_Amount,
    e.smx_description as Product_Description,
    e.smx_family as Product_Family,
    e.smx_name as Name,
    e.smx_producttype as Product_Type,
    b.new_versionnumber
FROM 
    new_cpq_lineitem_tmp a
JOIN  
    new_cpq_productconfiguration b ON a.new_productconfigurationid = b.new_cpq_productconfigurationid
JOIN 
    new_cpq_quote c ON b.new_quoteid = c.new_cpq_quoteid
JOIN 
    opportunity d ON c.new_opportunityid = d.opportunityid
JOIN 
    smx_product e ON a.new_optionid = e.smx_productid
JOIN 
    smx_address f ON a.new_locationid = f.smx_addressid
WHERE 
    e.smx_producttype = '180700001'
    and b.new_versionnumber = (select top 1 new_versionnumber
                                from new_cpq_productconfiguration
                                order by new_versionnumber desc)