0

I am very new to PL/SQL, but I would like to populate an empty field for all entries in a table, based on the values of several fields in a giving row. So the logic I would like to put in place is, but there are several thing in which I need to keep in mind when setting up a table for a Car Dealership...

First, group by the distinct used_car_ids, then update the column based on.....

  1. If the used_car_id column is equal to the new_car_id, and both the drive_mode and saftey_rating is 1, then set the Parking_Spot number to 1 as highest precedence.
  2. If the used_car_id column is equal to the new_car_id, but the drive_mode is 1 and saftey_rating is 2, then set the Parking_Spot column to 2 or the available consecutive Parking_Spot number

    • If there are more conditions similar to this, but only the saftey_rating is different then, I would like to keep increasing the Parking_Spot number to the next available consecutive Parking_spot number for that entry
  3. If the used_car_id column is equal to the new_car_id, but the drive_mode is 2 and saftey_rating is 1, then set the Parking_Spot column to 3, or the next available Parking_Spot number

  4. If the used_car_id column is equal to the new_car_id, but the drive_mode is 2 and saftey_rating is 2, then set the Parking_Spot number to 4, or the next available Parking_Spot number
    • Same rules apply, as above.

If the highest precedence case isn't met then it should treat the 2 use case as the highest and put that as Parking_Spot=1.

This is a small picture, of what I would ideally want. So as you can see in the picture the Parking Spot number is 1, and 2 depending if the used_car_id, new_car_id, drive_mode, and safety_rating are 1 and 2, but once the used_car_id and new_car_id is different the Parking Spot number increases to 3 and 4.

Used Car Dealer Table

I'm assuming I need a function to input 4 parameters and based on those fields then I return a number as the Parking Spot. But I am still a bit unsure of how to go about it. Any help would be appreciated. Thanks.

user3116769
  • 59
  • 3
  • 9
  • PL/SQL is the procedural portion language of the Oracle Database, but you have mysql tagged. Which are you actually using? – Allan Apr 27 '18 at 19:00
  • Tag properly!!!! If this is Oracle, why do I see `MySQL` tag on your question??? – Eric Apr 27 '18 at 22:43
  • I'm sorry please bear with me. I didn't know that PL/SQL is only for Oracle. This is a MySQL database. I just edited the name of the question and the tags. Thank you to all for pointing that out. – user3116769 Apr 27 '18 at 22:48

1 Answers1

0

It's not clear from your tags whether you're actually using the Oracle database or MySQL. This answer assumes the former.

Your question has a couple gaps, but it seems like you want to give each row a sequential number based on the drive_mode and safety_rating. You mention "used_car_id column is equal to the new_car_id" several times, but never mention what you want to do when they are not equal.

For the question as written, it seems that this can be solved in a fairly straightforward way using analytic functions. In this case, we'll simply tell the function to order the rows by drive_mode_id and safety_rating and give each row from the resulting order a consecutive number:

SELECT id,
       used_car_id,
       new_car_id,
       car_features_id,
       drive_mode_id,
       safety_rating,
       ROW_NUMBER () OVER (ORDER BY drive_mode, safety_rating)
           AS parking_spot
FROM   your_table
WHERE  used_car_id = new_car_id

It looks like MySQL supports analytic functions (aka window functions) as of 8.0.2, so the above solution should still work if you are using that version or later (if not, this answer might help).

To update that to also consider used_car_id = new_car_id as a sort condition, you'll need to add a conditional to the ORDER BY:

SELECT id,
       used_car_id,
       new_car_id,
       car_features_id,
       drive_mode_id,
       safety_rating,
       ROW_NUMBER ()
       OVER (
           ORDER BY
               CASE WHEN used_car_id = new_car_id THEN 1 ELSE 2 END,
               drive_mode,
               safety_rating)
           AS parking_spot
FROM   your_table

This is untested, so I can't guarantee that this is entirely syntactically correct, but it should be close.

Allan
  • 17,141
  • 4
  • 52
  • 69
  • Thanks for your response Allan, and I'm using Mysql database. I'm sorry that was another point I wanted to bring up as well. If the used_car_id and new_car_id are not equal then it should be treated as lower precedence compared to the ones which are equal. So, if there is a row with – user3116769 Apr 27 '18 at 19:31
  • Thanks for your response Allan, and I'm using Mysql database. I'm sorry I meant to mention that point. But if the used_car_id and new_car_id are not equal then it should be treated as lower precedence compared to the ones which are equal. So, if there is a row with both used_car_id and new_car_id equal despite the values of the other columns, and it should have a lower Parking_Spot number than the ones which are not equal. I was thinking I needed to orderBy the Parking_Spots and groupBy the drive_mode. But I like this approach, except it is not able to compile on Workbench, can you assist me? – user3116769 Apr 27 '18 at 19:42
  • Is there a Mysql version to this query, since there are no functions such as ROW_NUMBER or OVER in Mysql which is causing errors. – user3116769 Apr 27 '18 at 19:48
  • I've linked to another question that pertains to emulating that function in the answer. – Allan Apr 27 '18 at 19:50
  • So the version which is being used is Mysql 5.7, so that version must've released a few months ago. So if I had to adhere to MySQL 5.7 syntax. Is there a way to replicate that function, or would I have to use pl/sql instead in my situation? – user3116769 Apr 27 '18 at 20:58
  • 1
    @user3116769 Isn't it useless if you don't tag properly??? Since you put `PL/SQL` on your title, people assume you are using Oracle. Now someone give you solution for Oracle and you can't use it. You just waste everyone's time!!! – Eric Apr 27 '18 at 22:48
  • @Eric My Apologies, I'm using a MySQL 5.7 server, and I didn't know that PL/SQL is exclusive to OracleSQL only. This is a bit new to me. So please understand. – user3116769 Apr 27 '18 at 22:52
  • Also, as I mentioned above, I included a link to a question about replicating the `row_number` function in MySQL in the answer. Here it is again: https://stackoverflow.com/questions/1895110/row-number-in-mysql – Allan Apr 30 '18 at 13:47