SQL UPDATE with WHERE and MIN criteria












0















I've really tried with this problem and it's taking too much of my time now. I just need it to work to demo other features of the program to my students. I and they will not need anything this complex



I have a query where I need to update a table (bids) based on finding a field (design_code) and the minimum of the bid_amount



I have gotten as far as this



UPDATE Bids a 
INNER JOIN (SELECT DesignCode, MIN(Bid_Amount) AS Bid_Amount
FROM Bids
WHERE DesignCode = "FT1") AS b
ON (a.DesignCode=b.DesignCode AND a.Bid_Amount =b.Bid_Amount)
SET Bid_Currently_Successful = No
WHERE a.DesignCode = "FT1" AND a.Bid_Amount =b.Bid_Amount; ');


This is based on SQL Update Table Where date = MIN(date)



But I get the error:




Circular reference caused by Bid_Amount in select




I think I know why, duplicate names, but don't know which to change



I also get the error:




Your query does not include the specified expression 'DesignCode' as aprt of an aggregate function.




If I do change the AS Bid_Amount's name










share|improve this question

























  • This question could be improved by providing sample data and expected output as text in the question.

    – P.Salmon
    Nov 13 '18 at 9:43
















0















I've really tried with this problem and it's taking too much of my time now. I just need it to work to demo other features of the program to my students. I and they will not need anything this complex



I have a query where I need to update a table (bids) based on finding a field (design_code) and the minimum of the bid_amount



I have gotten as far as this



UPDATE Bids a 
INNER JOIN (SELECT DesignCode, MIN(Bid_Amount) AS Bid_Amount
FROM Bids
WHERE DesignCode = "FT1") AS b
ON (a.DesignCode=b.DesignCode AND a.Bid_Amount =b.Bid_Amount)
SET Bid_Currently_Successful = No
WHERE a.DesignCode = "FT1" AND a.Bid_Amount =b.Bid_Amount; ');


This is based on SQL Update Table Where date = MIN(date)



But I get the error:




Circular reference caused by Bid_Amount in select




I think I know why, duplicate names, but don't know which to change



I also get the error:




Your query does not include the specified expression 'DesignCode' as aprt of an aggregate function.




If I do change the AS Bid_Amount's name










share|improve this question

























  • This question could be improved by providing sample data and expected output as text in the question.

    – P.Salmon
    Nov 13 '18 at 9:43














0












0








0








I've really tried with this problem and it's taking too much of my time now. I just need it to work to demo other features of the program to my students. I and they will not need anything this complex



I have a query where I need to update a table (bids) based on finding a field (design_code) and the minimum of the bid_amount



I have gotten as far as this



UPDATE Bids a 
INNER JOIN (SELECT DesignCode, MIN(Bid_Amount) AS Bid_Amount
FROM Bids
WHERE DesignCode = "FT1") AS b
ON (a.DesignCode=b.DesignCode AND a.Bid_Amount =b.Bid_Amount)
SET Bid_Currently_Successful = No
WHERE a.DesignCode = "FT1" AND a.Bid_Amount =b.Bid_Amount; ');


This is based on SQL Update Table Where date = MIN(date)



But I get the error:




Circular reference caused by Bid_Amount in select




I think I know why, duplicate names, but don't know which to change



I also get the error:




Your query does not include the specified expression 'DesignCode' as aprt of an aggregate function.




If I do change the AS Bid_Amount's name










share|improve this question
















I've really tried with this problem and it's taking too much of my time now. I just need it to work to demo other features of the program to my students. I and they will not need anything this complex



I have a query where I need to update a table (bids) based on finding a field (design_code) and the minimum of the bid_amount



I have gotten as far as this



UPDATE Bids a 
INNER JOIN (SELECT DesignCode, MIN(Bid_Amount) AS Bid_Amount
FROM Bids
WHERE DesignCode = "FT1") AS b
ON (a.DesignCode=b.DesignCode AND a.Bid_Amount =b.Bid_Amount)
SET Bid_Currently_Successful = No
WHERE a.DesignCode = "FT1" AND a.Bid_Amount =b.Bid_Amount; ');


This is based on SQL Update Table Where date = MIN(date)



But I get the error:




Circular reference caused by Bid_Amount in select




I think I know why, duplicate names, but don't know which to change



I also get the error:




Your query does not include the specified expression 'DesignCode' as aprt of an aggregate function.




If I do change the AS Bid_Amount's name







mysql sql sql-update






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 6:52









a_horse_with_no_name

295k46451546




295k46451546










asked Nov 13 '18 at 6:50









MrLeeMrLee

1




1













  • This question could be improved by providing sample data and expected output as text in the question.

    – P.Salmon
    Nov 13 '18 at 9:43



















  • This question could be improved by providing sample data and expected output as text in the question.

    – P.Salmon
    Nov 13 '18 at 9:43

















This question could be improved by providing sample data and expected output as text in the question.

– P.Salmon
Nov 13 '18 at 9:43





This question could be improved by providing sample data and expected output as text in the question.

– P.Salmon
Nov 13 '18 at 9:43












3 Answers
3






active

oldest

votes


















0














First of all, hello Mr. Lee and welcome to stackoverflow.


Secondly, in the inner query
SELECT DesignCode, MIN(Bid_Amount) AS Bid_Amount FROM Bids WHERE DesignCode = "FT1"


you are trying to use an aggregate function with another column. Usually, to solve this issue, you have to use the group by clause with the column that is not in the aggregate function.

BUT, this changes what you need, as this will be returning the rows as the minimum amount for each DesignCode. That's not what you want.


What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it.


So your inner query would be something like this:



SELECT TOP 1 DesignCode, Bid_Amount FROM Bids ORDER BY Bid_Amount


This will return the top row, when ordered in ascending order of Bid_Amount.


So, your overall query would be something like:



UPDATE Bids a
SET Bid_Currently_Successful = 'No'
WHERE
a.Bidders = (
SELECT TOP 1 Bidders
FROM Bids
WHERE DesignCode = 'FT1'
ORDER BY Bid_Amount
)
AND a.date_time_of_bid = (
SELECT TOP 1 date_time_of_bid
FROM Bids
WHERE DesignCode = 'FT1'
ORDER BY Bid_Amount
)


I think, this should work fine.

But I dont think that is a clean way, so, here is another way I would approach this...



UPDATE a
SET a.Bid_Currently_Successful = 'No'
FROM
Bids a
INNER JOIN (
SELECT TOP 1
Bidders, date_time_of_bid
FROM
Bids
WHERE
DesignCode LIKE 'FT1'
ORDER BY
Bid_Amount
) b ON b.Bidders = a.Bidders AND b.date_time_of_bid = a.date_time_of_bid


I think this is a much cleaner and faster way. In this approach I made it sure that the only row, that gets joined with the Bids table is the one that fits your condition.


Though, I should advise, I haven't checked this approach, but it seemed a fun and faster way to do it.





Happy teaching.. :)





EDIT1: In case, you are using MySQL, the query would be:



UPDATE Bids a
SET Bid_Currently_Successful = 'No'
WHERE
a.Bidders = (
SELECT Bidders
FROM Bids
WHERE DesignCode = 'FT1'
ORDER BY Bid_Amount
LIMIT 1
)
AND a.date_time_of_bid = (
SELECT date_time_of_bid
FROM Bids
WHERE DesignCode = 'FT1'
ORDER BY Bid_Amount
LIMIT 1
)


and the cleaner approach would be:



UPDATE a
SET a.Bid_Currently_Successful = 'No'
FROM
Bids a
INNER JOIN (
SELECT
Bidders, date_time_of_bid
FROM
Bids
WHERE
DesignCode LIKE 'FT1'
ORDER BY
Bid_Amount
LIMIT 1
) b ON b.Bidders = a.Bidders AND b.date_time_of_bid = a.date_time_of_bid



Thanks P.Salmon for the edit.





EDIT2: Thanks for the table details Mr. Lee. Helped me a lot. And also added the where condition, that I seemed to miss.






share|improve this answer


























  • The mysql equivalent of top is limit.

    – P.Salmon
    Nov 13 '18 at 9:40











  • Hi guys. Thanks for the help. However that code does what I had a few days ago where ALL successful bids with FT1 change to unsuccessful :'-( I'm connecting to access by the way (not my call, school's choice before I got here)

    – MrLee
    Nov 13 '18 at 10:32













  • That would be the case, as the differentiating factor between all the rows might not be the column, 'DesignCode'. If you could specify the structure of tables and some sample data, it would be a lot more helpful... :)

    – VisheshNayak
    Nov 13 '18 at 10:36








  • 1





    "What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it." If the primary key is important then that is a composite of two other fields, bidders and date_time_of_bid

    – MrLee
    Nov 13 '18 at 10:40













  • Table bidders > Bidders(PK), address details etc. Table Bids > DateTime, Bidders (composite key), BidAmount, DesignCode, CurrentlySuccessful. Table Designs > DesignCode(PK), Description, ReservePrice, MinSuccessfulBid. Table Plots > PlotID, DesignCode

    – MrLee
    Nov 13 '18 at 10:45



















0














This should work in MySQL:



UPDATE Bids b JOIN
(SELECT b.DesignCode, MIN(Bid_Amount) AS Min_Bid_Amount
FROM Bids b
WHERE b.DesignCode = 'FT1'
GROUP BY b.DesignCode
) d
ON b.DesignCode = d.DesignCode AND
b.Bid_Amount = d.Min_Bid_Amount)
SET b.Bid_Currently_Successful = 'No'
WHERE b.DesignCode = 'FT1' ;


Most of the changes are cosmetic, but still useful:




  • Alias on Bid_Currently_Successful. This is a good practice when using joins in the update.

  • Single quotes instead of double quotes around string constants.

  • Single quotes around no (unless that is a column in your data).


  • GROUP BY in the subquery.






share|improve this answer
























  • Hi guys. Once again thanks for the help. I'm not using mySQL i'm connecting to Access so I just get syntax errors using these. Sorry for the slow reply I'm busy right now. I didn't put the mysql tag in by the way

    – MrLee
    Nov 15 '18 at 8:14











  • @MrLee . . . You should ask another question. This one has been answered multiple times, assuming the database is MS Access. If you just re-tag this question, you won't get an answer and others will see the incompatible answers and might downvote the answers.

    – Gordon Linoff
    Nov 15 '18 at 13:28



















0














I did it! Thanks for the help guys, it helped me to figure it out. Probably isn't pretty but I've other things to do so I don't care



UPDATE Bids a SET Bid_Currently_Successful = No
WHERE (((a.Bidders)=(SELECT TOP 1 Bidders FROM Bids WHERE Bid_Currently_Successful = Yes AND DesignCode = 'FT1' ORDER BY Bid_Amount))
AND ((a.date_time_of_bid)=(SELECT TOP 1 date_time_of_bid FROM Bids
WHERE Bid_Currently_Successful = Yes AND DesignCode = 'FT1' ORDER BY Bid_Amount)));



All the extra brackets courtesy of Access...






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275341%2fsql-update-with-where-and-min-criteria%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    First of all, hello Mr. Lee and welcome to stackoverflow.


    Secondly, in the inner query
    SELECT DesignCode, MIN(Bid_Amount) AS Bid_Amount FROM Bids WHERE DesignCode = "FT1"


    you are trying to use an aggregate function with another column. Usually, to solve this issue, you have to use the group by clause with the column that is not in the aggregate function.

    BUT, this changes what you need, as this will be returning the rows as the minimum amount for each DesignCode. That's not what you want.


    What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it.


    So your inner query would be something like this:



    SELECT TOP 1 DesignCode, Bid_Amount FROM Bids ORDER BY Bid_Amount


    This will return the top row, when ordered in ascending order of Bid_Amount.


    So, your overall query would be something like:



    UPDATE Bids a
    SET Bid_Currently_Successful = 'No'
    WHERE
    a.Bidders = (
    SELECT TOP 1 Bidders
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    )
    AND a.date_time_of_bid = (
    SELECT TOP 1 date_time_of_bid
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    )


    I think, this should work fine.

    But I dont think that is a clean way, so, here is another way I would approach this...



    UPDATE a
    SET a.Bid_Currently_Successful = 'No'
    FROM
    Bids a
    INNER JOIN (
    SELECT TOP 1
    Bidders, date_time_of_bid
    FROM
    Bids
    WHERE
    DesignCode LIKE 'FT1'
    ORDER BY
    Bid_Amount
    ) b ON b.Bidders = a.Bidders AND b.date_time_of_bid = a.date_time_of_bid


    I think this is a much cleaner and faster way. In this approach I made it sure that the only row, that gets joined with the Bids table is the one that fits your condition.


    Though, I should advise, I haven't checked this approach, but it seemed a fun and faster way to do it.





    Happy teaching.. :)





    EDIT1: In case, you are using MySQL, the query would be:



    UPDATE Bids a
    SET Bid_Currently_Successful = 'No'
    WHERE
    a.Bidders = (
    SELECT Bidders
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    LIMIT 1
    )
    AND a.date_time_of_bid = (
    SELECT date_time_of_bid
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    LIMIT 1
    )


    and the cleaner approach would be:



    UPDATE a
    SET a.Bid_Currently_Successful = 'No'
    FROM
    Bids a
    INNER JOIN (
    SELECT
    Bidders, date_time_of_bid
    FROM
    Bids
    WHERE
    DesignCode LIKE 'FT1'
    ORDER BY
    Bid_Amount
    LIMIT 1
    ) b ON b.Bidders = a.Bidders AND b.date_time_of_bid = a.date_time_of_bid



    Thanks P.Salmon for the edit.





    EDIT2: Thanks for the table details Mr. Lee. Helped me a lot. And also added the where condition, that I seemed to miss.






    share|improve this answer


























    • The mysql equivalent of top is limit.

      – P.Salmon
      Nov 13 '18 at 9:40











    • Hi guys. Thanks for the help. However that code does what I had a few days ago where ALL successful bids with FT1 change to unsuccessful :'-( I'm connecting to access by the way (not my call, school's choice before I got here)

      – MrLee
      Nov 13 '18 at 10:32













    • That would be the case, as the differentiating factor between all the rows might not be the column, 'DesignCode'. If you could specify the structure of tables and some sample data, it would be a lot more helpful... :)

      – VisheshNayak
      Nov 13 '18 at 10:36








    • 1





      "What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it." If the primary key is important then that is a composite of two other fields, bidders and date_time_of_bid

      – MrLee
      Nov 13 '18 at 10:40













    • Table bidders > Bidders(PK), address details etc. Table Bids > DateTime, Bidders (composite key), BidAmount, DesignCode, CurrentlySuccessful. Table Designs > DesignCode(PK), Description, ReservePrice, MinSuccessfulBid. Table Plots > PlotID, DesignCode

      – MrLee
      Nov 13 '18 at 10:45
















    0














    First of all, hello Mr. Lee and welcome to stackoverflow.


    Secondly, in the inner query
    SELECT DesignCode, MIN(Bid_Amount) AS Bid_Amount FROM Bids WHERE DesignCode = "FT1"


    you are trying to use an aggregate function with another column. Usually, to solve this issue, you have to use the group by clause with the column that is not in the aggregate function.

    BUT, this changes what you need, as this will be returning the rows as the minimum amount for each DesignCode. That's not what you want.


    What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it.


    So your inner query would be something like this:



    SELECT TOP 1 DesignCode, Bid_Amount FROM Bids ORDER BY Bid_Amount


    This will return the top row, when ordered in ascending order of Bid_Amount.


    So, your overall query would be something like:



    UPDATE Bids a
    SET Bid_Currently_Successful = 'No'
    WHERE
    a.Bidders = (
    SELECT TOP 1 Bidders
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    )
    AND a.date_time_of_bid = (
    SELECT TOP 1 date_time_of_bid
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    )


    I think, this should work fine.

    But I dont think that is a clean way, so, here is another way I would approach this...



    UPDATE a
    SET a.Bid_Currently_Successful = 'No'
    FROM
    Bids a
    INNER JOIN (
    SELECT TOP 1
    Bidders, date_time_of_bid
    FROM
    Bids
    WHERE
    DesignCode LIKE 'FT1'
    ORDER BY
    Bid_Amount
    ) b ON b.Bidders = a.Bidders AND b.date_time_of_bid = a.date_time_of_bid


    I think this is a much cleaner and faster way. In this approach I made it sure that the only row, that gets joined with the Bids table is the one that fits your condition.


    Though, I should advise, I haven't checked this approach, but it seemed a fun and faster way to do it.





    Happy teaching.. :)





    EDIT1: In case, you are using MySQL, the query would be:



    UPDATE Bids a
    SET Bid_Currently_Successful = 'No'
    WHERE
    a.Bidders = (
    SELECT Bidders
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    LIMIT 1
    )
    AND a.date_time_of_bid = (
    SELECT date_time_of_bid
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    LIMIT 1
    )


    and the cleaner approach would be:



    UPDATE a
    SET a.Bid_Currently_Successful = 'No'
    FROM
    Bids a
    INNER JOIN (
    SELECT
    Bidders, date_time_of_bid
    FROM
    Bids
    WHERE
    DesignCode LIKE 'FT1'
    ORDER BY
    Bid_Amount
    LIMIT 1
    ) b ON b.Bidders = a.Bidders AND b.date_time_of_bid = a.date_time_of_bid



    Thanks P.Salmon for the edit.





    EDIT2: Thanks for the table details Mr. Lee. Helped me a lot. And also added the where condition, that I seemed to miss.






    share|improve this answer


























    • The mysql equivalent of top is limit.

      – P.Salmon
      Nov 13 '18 at 9:40











    • Hi guys. Thanks for the help. However that code does what I had a few days ago where ALL successful bids with FT1 change to unsuccessful :'-( I'm connecting to access by the way (not my call, school's choice before I got here)

      – MrLee
      Nov 13 '18 at 10:32













    • That would be the case, as the differentiating factor between all the rows might not be the column, 'DesignCode'. If you could specify the structure of tables and some sample data, it would be a lot more helpful... :)

      – VisheshNayak
      Nov 13 '18 at 10:36








    • 1





      "What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it." If the primary key is important then that is a composite of two other fields, bidders and date_time_of_bid

      – MrLee
      Nov 13 '18 at 10:40













    • Table bidders > Bidders(PK), address details etc. Table Bids > DateTime, Bidders (composite key), BidAmount, DesignCode, CurrentlySuccessful. Table Designs > DesignCode(PK), Description, ReservePrice, MinSuccessfulBid. Table Plots > PlotID, DesignCode

      – MrLee
      Nov 13 '18 at 10:45














    0












    0








    0







    First of all, hello Mr. Lee and welcome to stackoverflow.


    Secondly, in the inner query
    SELECT DesignCode, MIN(Bid_Amount) AS Bid_Amount FROM Bids WHERE DesignCode = "FT1"


    you are trying to use an aggregate function with another column. Usually, to solve this issue, you have to use the group by clause with the column that is not in the aggregate function.

    BUT, this changes what you need, as this will be returning the rows as the minimum amount for each DesignCode. That's not what you want.


    What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it.


    So your inner query would be something like this:



    SELECT TOP 1 DesignCode, Bid_Amount FROM Bids ORDER BY Bid_Amount


    This will return the top row, when ordered in ascending order of Bid_Amount.


    So, your overall query would be something like:



    UPDATE Bids a
    SET Bid_Currently_Successful = 'No'
    WHERE
    a.Bidders = (
    SELECT TOP 1 Bidders
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    )
    AND a.date_time_of_bid = (
    SELECT TOP 1 date_time_of_bid
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    )


    I think, this should work fine.

    But I dont think that is a clean way, so, here is another way I would approach this...



    UPDATE a
    SET a.Bid_Currently_Successful = 'No'
    FROM
    Bids a
    INNER JOIN (
    SELECT TOP 1
    Bidders, date_time_of_bid
    FROM
    Bids
    WHERE
    DesignCode LIKE 'FT1'
    ORDER BY
    Bid_Amount
    ) b ON b.Bidders = a.Bidders AND b.date_time_of_bid = a.date_time_of_bid


    I think this is a much cleaner and faster way. In this approach I made it sure that the only row, that gets joined with the Bids table is the one that fits your condition.


    Though, I should advise, I haven't checked this approach, but it seemed a fun and faster way to do it.





    Happy teaching.. :)





    EDIT1: In case, you are using MySQL, the query would be:



    UPDATE Bids a
    SET Bid_Currently_Successful = 'No'
    WHERE
    a.Bidders = (
    SELECT Bidders
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    LIMIT 1
    )
    AND a.date_time_of_bid = (
    SELECT date_time_of_bid
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    LIMIT 1
    )


    and the cleaner approach would be:



    UPDATE a
    SET a.Bid_Currently_Successful = 'No'
    FROM
    Bids a
    INNER JOIN (
    SELECT
    Bidders, date_time_of_bid
    FROM
    Bids
    WHERE
    DesignCode LIKE 'FT1'
    ORDER BY
    Bid_Amount
    LIMIT 1
    ) b ON b.Bidders = a.Bidders AND b.date_time_of_bid = a.date_time_of_bid



    Thanks P.Salmon for the edit.





    EDIT2: Thanks for the table details Mr. Lee. Helped me a lot. And also added the where condition, that I seemed to miss.






    share|improve this answer















    First of all, hello Mr. Lee and welcome to stackoverflow.


    Secondly, in the inner query
    SELECT DesignCode, MIN(Bid_Amount) AS Bid_Amount FROM Bids WHERE DesignCode = "FT1"


    you are trying to use an aggregate function with another column. Usually, to solve this issue, you have to use the group by clause with the column that is not in the aggregate function.

    BUT, this changes what you need, as this will be returning the rows as the minimum amount for each DesignCode. That's not what you want.


    What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it.


    So your inner query would be something like this:



    SELECT TOP 1 DesignCode, Bid_Amount FROM Bids ORDER BY Bid_Amount


    This will return the top row, when ordered in ascending order of Bid_Amount.


    So, your overall query would be something like:



    UPDATE Bids a
    SET Bid_Currently_Successful = 'No'
    WHERE
    a.Bidders = (
    SELECT TOP 1 Bidders
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    )
    AND a.date_time_of_bid = (
    SELECT TOP 1 date_time_of_bid
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    )


    I think, this should work fine.

    But I dont think that is a clean way, so, here is another way I would approach this...



    UPDATE a
    SET a.Bid_Currently_Successful = 'No'
    FROM
    Bids a
    INNER JOIN (
    SELECT TOP 1
    Bidders, date_time_of_bid
    FROM
    Bids
    WHERE
    DesignCode LIKE 'FT1'
    ORDER BY
    Bid_Amount
    ) b ON b.Bidders = a.Bidders AND b.date_time_of_bid = a.date_time_of_bid


    I think this is a much cleaner and faster way. In this approach I made it sure that the only row, that gets joined with the Bids table is the one that fits your condition.


    Though, I should advise, I haven't checked this approach, but it seemed a fun and faster way to do it.





    Happy teaching.. :)





    EDIT1: In case, you are using MySQL, the query would be:



    UPDATE Bids a
    SET Bid_Currently_Successful = 'No'
    WHERE
    a.Bidders = (
    SELECT Bidders
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    LIMIT 1
    )
    AND a.date_time_of_bid = (
    SELECT date_time_of_bid
    FROM Bids
    WHERE DesignCode = 'FT1'
    ORDER BY Bid_Amount
    LIMIT 1
    )


    and the cleaner approach would be:



    UPDATE a
    SET a.Bid_Currently_Successful = 'No'
    FROM
    Bids a
    INNER JOIN (
    SELECT
    Bidders, date_time_of_bid
    FROM
    Bids
    WHERE
    DesignCode LIKE 'FT1'
    ORDER BY
    Bid_Amount
    LIMIT 1
    ) b ON b.Bidders = a.Bidders AND b.date_time_of_bid = a.date_time_of_bid



    Thanks P.Salmon for the edit.





    EDIT2: Thanks for the table details Mr. Lee. Helped me a lot. And also added the where condition, that I seemed to miss.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 11:35

























    answered Nov 13 '18 at 9:23









    VisheshNayakVisheshNayak

    184




    184













    • The mysql equivalent of top is limit.

      – P.Salmon
      Nov 13 '18 at 9:40











    • Hi guys. Thanks for the help. However that code does what I had a few days ago where ALL successful bids with FT1 change to unsuccessful :'-( I'm connecting to access by the way (not my call, school's choice before I got here)

      – MrLee
      Nov 13 '18 at 10:32













    • That would be the case, as the differentiating factor between all the rows might not be the column, 'DesignCode'. If you could specify the structure of tables and some sample data, it would be a lot more helpful... :)

      – VisheshNayak
      Nov 13 '18 at 10:36








    • 1





      "What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it." If the primary key is important then that is a composite of two other fields, bidders and date_time_of_bid

      – MrLee
      Nov 13 '18 at 10:40













    • Table bidders > Bidders(PK), address details etc. Table Bids > DateTime, Bidders (composite key), BidAmount, DesignCode, CurrentlySuccessful. Table Designs > DesignCode(PK), Description, ReservePrice, MinSuccessfulBid. Table Plots > PlotID, DesignCode

      – MrLee
      Nov 13 '18 at 10:45



















    • The mysql equivalent of top is limit.

      – P.Salmon
      Nov 13 '18 at 9:40











    • Hi guys. Thanks for the help. However that code does what I had a few days ago where ALL successful bids with FT1 change to unsuccessful :'-( I'm connecting to access by the way (not my call, school's choice before I got here)

      – MrLee
      Nov 13 '18 at 10:32













    • That would be the case, as the differentiating factor between all the rows might not be the column, 'DesignCode'. If you could specify the structure of tables and some sample data, it would be a lot more helpful... :)

      – VisheshNayak
      Nov 13 '18 at 10:36








    • 1





      "What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it." If the primary key is important then that is a composite of two other fields, bidders and date_time_of_bid

      – MrLee
      Nov 13 '18 at 10:40













    • Table bidders > Bidders(PK), address details etc. Table Bids > DateTime, Bidders (composite key), BidAmount, DesignCode, CurrentlySuccessful. Table Designs > DesignCode(PK), Description, ReservePrice, MinSuccessfulBid. Table Plots > PlotID, DesignCode

      – MrLee
      Nov 13 '18 at 10:45

















    The mysql equivalent of top is limit.

    – P.Salmon
    Nov 13 '18 at 9:40





    The mysql equivalent of top is limit.

    – P.Salmon
    Nov 13 '18 at 9:40













    Hi guys. Thanks for the help. However that code does what I had a few days ago where ALL successful bids with FT1 change to unsuccessful :'-( I'm connecting to access by the way (not my call, school's choice before I got here)

    – MrLee
    Nov 13 '18 at 10:32







    Hi guys. Thanks for the help. However that code does what I had a few days ago where ALL successful bids with FT1 change to unsuccessful :'-( I'm connecting to access by the way (not my call, school's choice before I got here)

    – MrLee
    Nov 13 '18 at 10:32















    That would be the case, as the differentiating factor between all the rows might not be the column, 'DesignCode'. If you could specify the structure of tables and some sample data, it would be a lot more helpful... :)

    – VisheshNayak
    Nov 13 '18 at 10:36







    That would be the case, as the differentiating factor between all the rows might not be the column, 'DesignCode'. If you could specify the structure of tables and some sample data, it would be a lot more helpful... :)

    – VisheshNayak
    Nov 13 '18 at 10:36






    1




    1





    "What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it." If the primary key is important then that is a composite of two other fields, bidders and date_time_of_bid

    – MrLee
    Nov 13 '18 at 10:40







    "What you want is the primary key for the row, which has the lowest of all bid amounts, so that, you can update the table bids with it." If the primary key is important then that is a composite of two other fields, bidders and date_time_of_bid

    – MrLee
    Nov 13 '18 at 10:40















    Table bidders > Bidders(PK), address details etc. Table Bids > DateTime, Bidders (composite key), BidAmount, DesignCode, CurrentlySuccessful. Table Designs > DesignCode(PK), Description, ReservePrice, MinSuccessfulBid. Table Plots > PlotID, DesignCode

    – MrLee
    Nov 13 '18 at 10:45





    Table bidders > Bidders(PK), address details etc. Table Bids > DateTime, Bidders (composite key), BidAmount, DesignCode, CurrentlySuccessful. Table Designs > DesignCode(PK), Description, ReservePrice, MinSuccessfulBid. Table Plots > PlotID, DesignCode

    – MrLee
    Nov 13 '18 at 10:45













    0














    This should work in MySQL:



    UPDATE Bids b JOIN
    (SELECT b.DesignCode, MIN(Bid_Amount) AS Min_Bid_Amount
    FROM Bids b
    WHERE b.DesignCode = 'FT1'
    GROUP BY b.DesignCode
    ) d
    ON b.DesignCode = d.DesignCode AND
    b.Bid_Amount = d.Min_Bid_Amount)
    SET b.Bid_Currently_Successful = 'No'
    WHERE b.DesignCode = 'FT1' ;


    Most of the changes are cosmetic, but still useful:




    • Alias on Bid_Currently_Successful. This is a good practice when using joins in the update.

    • Single quotes instead of double quotes around string constants.

    • Single quotes around no (unless that is a column in your data).


    • GROUP BY in the subquery.






    share|improve this answer
























    • Hi guys. Once again thanks for the help. I'm not using mySQL i'm connecting to Access so I just get syntax errors using these. Sorry for the slow reply I'm busy right now. I didn't put the mysql tag in by the way

      – MrLee
      Nov 15 '18 at 8:14











    • @MrLee . . . You should ask another question. This one has been answered multiple times, assuming the database is MS Access. If you just re-tag this question, you won't get an answer and others will see the incompatible answers and might downvote the answers.

      – Gordon Linoff
      Nov 15 '18 at 13:28
















    0














    This should work in MySQL:



    UPDATE Bids b JOIN
    (SELECT b.DesignCode, MIN(Bid_Amount) AS Min_Bid_Amount
    FROM Bids b
    WHERE b.DesignCode = 'FT1'
    GROUP BY b.DesignCode
    ) d
    ON b.DesignCode = d.DesignCode AND
    b.Bid_Amount = d.Min_Bid_Amount)
    SET b.Bid_Currently_Successful = 'No'
    WHERE b.DesignCode = 'FT1' ;


    Most of the changes are cosmetic, but still useful:




    • Alias on Bid_Currently_Successful. This is a good practice when using joins in the update.

    • Single quotes instead of double quotes around string constants.

    • Single quotes around no (unless that is a column in your data).


    • GROUP BY in the subquery.






    share|improve this answer
























    • Hi guys. Once again thanks for the help. I'm not using mySQL i'm connecting to Access so I just get syntax errors using these. Sorry for the slow reply I'm busy right now. I didn't put the mysql tag in by the way

      – MrLee
      Nov 15 '18 at 8:14











    • @MrLee . . . You should ask another question. This one has been answered multiple times, assuming the database is MS Access. If you just re-tag this question, you won't get an answer and others will see the incompatible answers and might downvote the answers.

      – Gordon Linoff
      Nov 15 '18 at 13:28














    0












    0








    0







    This should work in MySQL:



    UPDATE Bids b JOIN
    (SELECT b.DesignCode, MIN(Bid_Amount) AS Min_Bid_Amount
    FROM Bids b
    WHERE b.DesignCode = 'FT1'
    GROUP BY b.DesignCode
    ) d
    ON b.DesignCode = d.DesignCode AND
    b.Bid_Amount = d.Min_Bid_Amount)
    SET b.Bid_Currently_Successful = 'No'
    WHERE b.DesignCode = 'FT1' ;


    Most of the changes are cosmetic, but still useful:




    • Alias on Bid_Currently_Successful. This is a good practice when using joins in the update.

    • Single quotes instead of double quotes around string constants.

    • Single quotes around no (unless that is a column in your data).


    • GROUP BY in the subquery.






    share|improve this answer













    This should work in MySQL:



    UPDATE Bids b JOIN
    (SELECT b.DesignCode, MIN(Bid_Amount) AS Min_Bid_Amount
    FROM Bids b
    WHERE b.DesignCode = 'FT1'
    GROUP BY b.DesignCode
    ) d
    ON b.DesignCode = d.DesignCode AND
    b.Bid_Amount = d.Min_Bid_Amount)
    SET b.Bid_Currently_Successful = 'No'
    WHERE b.DesignCode = 'FT1' ;


    Most of the changes are cosmetic, but still useful:




    • Alias on Bid_Currently_Successful. This is a good practice when using joins in the update.

    • Single quotes instead of double quotes around string constants.

    • Single quotes around no (unless that is a column in your data).


    • GROUP BY in the subquery.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 12:20









    Gordon LinoffGordon Linoff

    767k35300402




    767k35300402













    • Hi guys. Once again thanks for the help. I'm not using mySQL i'm connecting to Access so I just get syntax errors using these. Sorry for the slow reply I'm busy right now. I didn't put the mysql tag in by the way

      – MrLee
      Nov 15 '18 at 8:14











    • @MrLee . . . You should ask another question. This one has been answered multiple times, assuming the database is MS Access. If you just re-tag this question, you won't get an answer and others will see the incompatible answers and might downvote the answers.

      – Gordon Linoff
      Nov 15 '18 at 13:28



















    • Hi guys. Once again thanks for the help. I'm not using mySQL i'm connecting to Access so I just get syntax errors using these. Sorry for the slow reply I'm busy right now. I didn't put the mysql tag in by the way

      – MrLee
      Nov 15 '18 at 8:14











    • @MrLee . . . You should ask another question. This one has been answered multiple times, assuming the database is MS Access. If you just re-tag this question, you won't get an answer and others will see the incompatible answers and might downvote the answers.

      – Gordon Linoff
      Nov 15 '18 at 13:28

















    Hi guys. Once again thanks for the help. I'm not using mySQL i'm connecting to Access so I just get syntax errors using these. Sorry for the slow reply I'm busy right now. I didn't put the mysql tag in by the way

    – MrLee
    Nov 15 '18 at 8:14





    Hi guys. Once again thanks for the help. I'm not using mySQL i'm connecting to Access so I just get syntax errors using these. Sorry for the slow reply I'm busy right now. I didn't put the mysql tag in by the way

    – MrLee
    Nov 15 '18 at 8:14













    @MrLee . . . You should ask another question. This one has been answered multiple times, assuming the database is MS Access. If you just re-tag this question, you won't get an answer and others will see the incompatible answers and might downvote the answers.

    – Gordon Linoff
    Nov 15 '18 at 13:28





    @MrLee . . . You should ask another question. This one has been answered multiple times, assuming the database is MS Access. If you just re-tag this question, you won't get an answer and others will see the incompatible answers and might downvote the answers.

    – Gordon Linoff
    Nov 15 '18 at 13:28











    0














    I did it! Thanks for the help guys, it helped me to figure it out. Probably isn't pretty but I've other things to do so I don't care



    UPDATE Bids a SET Bid_Currently_Successful = No
    WHERE (((a.Bidders)=(SELECT TOP 1 Bidders FROM Bids WHERE Bid_Currently_Successful = Yes AND DesignCode = 'FT1' ORDER BY Bid_Amount))
    AND ((a.date_time_of_bid)=(SELECT TOP 1 date_time_of_bid FROM Bids
    WHERE Bid_Currently_Successful = Yes AND DesignCode = 'FT1' ORDER BY Bid_Amount)));



    All the extra brackets courtesy of Access...






    share|improve this answer




























      0














      I did it! Thanks for the help guys, it helped me to figure it out. Probably isn't pretty but I've other things to do so I don't care



      UPDATE Bids a SET Bid_Currently_Successful = No
      WHERE (((a.Bidders)=(SELECT TOP 1 Bidders FROM Bids WHERE Bid_Currently_Successful = Yes AND DesignCode = 'FT1' ORDER BY Bid_Amount))
      AND ((a.date_time_of_bid)=(SELECT TOP 1 date_time_of_bid FROM Bids
      WHERE Bid_Currently_Successful = Yes AND DesignCode = 'FT1' ORDER BY Bid_Amount)));



      All the extra brackets courtesy of Access...






      share|improve this answer


























        0












        0








        0







        I did it! Thanks for the help guys, it helped me to figure it out. Probably isn't pretty but I've other things to do so I don't care



        UPDATE Bids a SET Bid_Currently_Successful = No
        WHERE (((a.Bidders)=(SELECT TOP 1 Bidders FROM Bids WHERE Bid_Currently_Successful = Yes AND DesignCode = 'FT1' ORDER BY Bid_Amount))
        AND ((a.date_time_of_bid)=(SELECT TOP 1 date_time_of_bid FROM Bids
        WHERE Bid_Currently_Successful = Yes AND DesignCode = 'FT1' ORDER BY Bid_Amount)));



        All the extra brackets courtesy of Access...






        share|improve this answer













        I did it! Thanks for the help guys, it helped me to figure it out. Probably isn't pretty but I've other things to do so I don't care



        UPDATE Bids a SET Bid_Currently_Successful = No
        WHERE (((a.Bidders)=(SELECT TOP 1 Bidders FROM Bids WHERE Bid_Currently_Successful = Yes AND DesignCode = 'FT1' ORDER BY Bid_Amount))
        AND ((a.date_time_of_bid)=(SELECT TOP 1 date_time_of_bid FROM Bids
        WHERE Bid_Currently_Successful = Yes AND DesignCode = 'FT1' ORDER BY Bid_Amount)));



        All the extra brackets courtesy of Access...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 10:38









        MrLeeMrLee

        1




        1






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275341%2fsql-update-with-where-and-min-criteria%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ