Sql help request

I’ve been using chatgpt to alter this query

SELECT DISTINCT "Plots"."name" AS "Plot", "latin_name", "start_month", "start_day", "end_month", "end_day", "practice", "detail", "phenological_event", "Common_name" FROM "plot_annual_cultivation_dates_view" JOIN "Plots" ON "plot_annual_cultivation_dates_view"."Plot" = "Plots"."plot_id" WHERE CURRENT_DATE <= CAST( YEAR( CURRENT_DATE ) || '-' || RIGHT( '0' || "end_month", 2 ) || '-' || RIGHT( '0' || "end_day", 2 ) AS DATE ) AND CURRENT_DATE BETWEEN CAST( CASE WHEN "start_month" <= "end_month" THEN YEAR( CURRENT_DATE ) ELSE YEAR( CURRENT_DATE ) - 1 END || '-' || RIGHT( '0' || "start_month", 2 ) || '-' || RIGHT( '0' || "start_day", 2 ) AS DATE ) AND CAST( CASE WHEN "start_month" <= "end_month" THEN YEAR( CURRENT_DATE ) ELSE YEAR( CURRENT_DATE ) + 1 END || '-' || RIGHT( '0' || "end_month", 2 ) || '-' || RIGHT( '0' || "end_day", 2 ) AS DATE ) ORDER BY "end_month" ASC, "end_day" ASC, "practice" ASC

so that it only returns like @Villeroy demonstrates

SELECT "Plots"."name", "beds"."bed_name", "plants".* 
FROM "Plot", "Plots", "beds", "plants" 
WHERE "Plot"."Plot" = "Plots"."plot_id" 
  AND "beds"."plot_id" = "Plots"."plot_id" 
  AND "plants"."bed_id" = "beds"."bed_id" 
  AND "beds"."bed_name" = 'seeds' 
ORDER BY "plants"."latin_name" ASC

I’m wanting the qwery on the right of the screenshot to return only the plants that are in the form on the left of the screenshot that also meet the date criterias. The query returns Oemlaria cerasiformis which is not in the form on the screenshot on the left. The query should only return the plants that are in the form on the left.
There are other plots that have plants with the bed_name “seeds” and i don’t want to see those ones. Like I said I want the query only to return results where “beds.plot_id”=“Plot.Plot”

here is the sql statement that Chatgpt made:

SELECT DISTINCT “Plots”.“name” AS “Plot”, “beds”.“bed_name” AS “Bed”, “latin_name”, “start_month”, “start_day”, “end_month”, “end_day”, “practice”, “detail”, “phenological_event”, “Common_name” FROM “plot_annual_cultivation_dates_view” JOIN “Plots” ON “plot_annual_cultivation_dates_view”.“Plot” = “Plots”.“plot_id” JOIN “beds” ON “beds”.“plot_id” = “Plots”.“plot_id” WHERE “beds”.“bed_name” = ‘seeds’ AND CURRENT_DATE BETWEEN CAST( CASE WHEN “start_month” <= “end_month” THEN YEAR( CURRENT_DATE ) || ‘-’ || LPAD( “start_month”, 2, ‘0’ ) || ‘-’ || LPAD( “start_day”, 2, ‘0’ ) ELSE ( YEAR( CURRENT_DATE ) - 1 ) || ‘-’ || LPAD( “start_month”, 2, ‘0’ ) || ‘-’ || LPAD( “start_day”, 2, ‘0’ ) END AS DATE ) AND CAST( CASE WHEN “start_month” <= “end_month” THEN YEAR( CURRENT_DATE ) || ‘-’ || LPAD( “end_month”, 2, ‘0’ ) || ‘-’ || LPAD( “end_day”, 2, ‘0’ ) ELSE ( YEAR( CURRENT_DATE ) + 1 ) || ‘-’ || LPAD( “end_month”, 2, ‘0’ ) || ‘-’ || LPAD( “end_day”, 2, ‘0’ ) END AS DATE ) AND LOWER ( “practice” ) LIKE ‘%seed%’ ORDER BY “end_month”, “end_day”, “practice”

but your query still contains them:

how do I edit the query so “beds”.“bed_name” = ‘seeds’ and “beds.plot_id”=“Plot.Plot”? So I’m only getting results from the plots that are the same as “Plot.Plot”

Right click on the query and choose “edit in sql-editor” or whatever this is called in your setup.

If you post your odb file maybe somebody can have a look at it.

1 Like

I downloaded your rar archive of March 31 from https://www.dropbox.com/scl/fo/i9x73baljkyejbnn4ch7g/ABTaiRZ930FZ6-FVVGcWriU?dl=0&e=1&noscript=1&rlkey=lo3ybfyw9yl9exdadea7o4m3l&st=m3sec38r
Built the same form as in your screenshot
Plot 1->1 Plots 1->n Beds 1->n Plants
and get the same result. I select Plot=“Sook Rd. 6411” and Bed=“seed”, which results in 80 records that look very much like the ones in your screenshot.
The following query performs the same selection:

SELECT "Plots"."name", "beds"."bed_name", "plants".* 
FROM "Plot", "Plots", "beds", "plants" 
WHERE "Plot"."Plot" = "Plots"."plot_id" 
  AND "beds"."plot_id" = "Plots"."plot_id" 
  AND "plants"."bed_id" = "beds"."bed_id" 
  AND "beds"."bed_name" = 'seeds' 
ORDER BY "plants"."latin_name" ASC

At this point, an obvious challenge could be: How to create a form where you can enter the plot.id with the string ‘seed’ and get the same result.

Why is “bed_id” an attribute of a plant? Does each species belong to one bed_id? The plants table seems to record species rather than individual plants because there is also a quantity (counting plants?).
IMHO, you plant one plant species on multiple beds, so plant (species) should be an attribute of a bed. If you plant more than one species on the same bed, this would be even a many-to-many relation.

Now I see that you have a table of species, a table of beds and the plant table maps species to beds.

1 Like

@Wanderer I’m asking for someone to write the SQL query for me so I can paste it and see if it does what I need.

@sterretje
https://www.dropbox.com/scl/fi/6w4ndob8k4ldg9zk0ohdg/LANdpLAN2025-04-08.zip?rlkey=bjzp07npfgagd74xh2kp97zf8&st=90ca5s77&dl=0

@Villeroy
Planting many species on one bed is beneficial when they are good companion plants. A ‘companions’ table will be added later to the database.
A ‘plant’ is existing in the real world so it has a location ‘bed_id’. Later I will create queries to suggest plants based on the bed characteristics like shade and soil.

@skyandrews I want to see the plants in the bed named seeds from the same plot as Plot.Plot
I do not want to see the plants in the beds named seeds that are in other plots.

I think that “plot_annual_cultivation_dates_view”.“Plot” = “Plots”.“plot_id”
should be
“plot_annual_cultivation_dates_view”.“Plot” = “Plot”.“Plot”
then i get error

BlockquoteSQL Status: 42501
Error code: -5501
user lacks privilege or object not found: Plot.Plot at ./connectivity/source/drivers/jdbc/Object.cxx:173

@Villeroy Yes these are the plants that I wanted to be returned. How can I modify the query so that I get those plants along with the all the stuff about dates?

https://www.dropbox.com/scl/fi/6w4ndob8k4ldg9zk0ohdg/LANdpLAN2025-04-08.zip?rlkey=bjzp07npfgagd74xh2kp97zf8&st=90ca5s77&dl=0
I think the plants form in the linked database can do?

I ended up creating a new “bed” table and using all the same queries that were used on the “plot” table. Thanks for all your help as I chased a wild goose through the forest of my own imagination. Thanks for witness my predicament and your kind comments helped me see things from another point of view.