Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. sql question - if you allow...
Forum Updated to NodeBB v4.3 + New Features

sql question - if you allow...

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 130 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • HoMaH Offline
    HoMaH Offline
    HoMa
    wrote on last edited by
    #1

    I know, this is not a perfect match for this forum, however, maybe someone has an idea:

    Two tables:
    contracts with fields: id, firstBooking (note: firstBooking is a newly added field and therefor empty)
    bookings with fields:ContractId, bookingDate

    with smthg like
    SELEcT contractId, MIN(bookingDate) FROM bookings
    I get a list with all first bookings.
    Now I want to store these dates in the contracts table field "firstBooking" - with the matching contract, of course.
    Is there a way to do this without programming a loop - just in one SQL statement? I tried a lot ...

    1 Reply Last reply
    0
    • HoMaH Offline
      HoMaH Offline
      HoMa
      wrote on last edited by
      #2

      OK - never mind. Finally I did it: One needs to have a UPDATE clause where there is a FROM part, which basically extracts the minimal booking date and a WHERE part, that compares the Contract Ids
      So it is

      UPDATE Vertraege
        SET firstBooking = bookings.minBDate
      
      FROM ( SELEcT Vertraege.id AS VertragsId, MIN(Datum) AS minBDate
      FRoM Buchungen Join Vertraege on Vertraege.id = Buchungen.VertragsId
      GROUP BY VertragsId) AS bookings
      
      WHERE Vertraege.id = bookings.VertragsId
      

      Just in case someone comes across smthg similar. The UPDATE SQL statement is pretty powerful, but not always easy to master ;)

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved