简体   繁体   中英

How to select minimum non duplicated value in a column?

Can you help me with SQL statements to find minimum non duplicated value? This is my sql statement

DECLARE @currentDate DATETIME = CONVERT(VARCHAR(10), Getdate(), 120)

UPDATE Dinfo
SET    WinnerID = result.CustomerID
FROM   Daily_Info Dinfo
       JOIN (SELECT CO.DailyInfoID,
                    CO.CustomerID
             FROM   Customer_Offer CO
             WHERE  CO.OfferDate = @currentDate
             GROUP  BY CO.DailyInfoID,
                       CO.CustomerID
             HAVING ( Count(CO.OfferPrice) = 1 )) result
         ON Dinfo.DailyID = result.DailyInfoID 

and i want to update my winner who offered minimum unique offer. How can i select it?

If you want to find data, then I would expect a select . I think the following query might do what you want:

select min(offerprice)
from (select co.*, count(*) over (partition by co.offerprice) as cnt
      from Customer_Offer co
      where CO.OfferDate = @currentDate
     ) co
where cnt = 1;

If you want to update information based on this, then use join :

update dinfo 
    set winnerId = c.CustomerId
    from dinfo cross join
         (select top 1 co.*
          from (select co.*, count(*) over (partition by co.offerprice) as cnt
                from Customer_Offer co
                where CO.OfferDate = @currentDate
               ) co
          where cnt = 1
          order by offerprice
         ) c

This follows the structure of your query, but it is going to update all rows in dinfo . You might want some other conditions to so only one row is updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM