I’ve updated the post in response to RobertG, I’d inadvertently uploaded the wrong version of the database. I’ve also copied into the main question the SQL code of the query that is functional.
Hello, I’m using LibreOffice 7.5.7.1 and I’m building a database for my carpentry business. I’ve got a database structure more or less worked out, and I’ve loaded it with a little bit of test data to help me figure out my queries. I’m very much a novice, and I’ve been using ChatGPT to help me write queries.
JARVISTest.odb (44.3 KB)
My particular question is how to reorganise a query that I’ve written (ChatGPT really) such that entries with “category”=2 come first, ordered by “label” descending, then “category”=3 come second, ordered by “label” descending, then “category”=4 come third, ordered by “label” descending, and then “category”=1 entries are listed last, ordered by “label” ascending. The query is called “query_PowellsLoadFromCoefficient”, and the SQL is all on the database as well as below. As a note, programme_id=2 is the programme that has some test data associated with it.
SELECT “TaskPart”.“length”, “TaskPart”.“width”, “TaskPart”.“quantity”, “TaskPartType”.“name” AS “material”, “TaskPart”.“id” AS “label”, ( “TaskPart”.“length” * “TaskPart”.“width” * “TaskPartType”.“depth” * “Density”.“density” * “TaskPart”.“quantity” ) / 1000000 AS “weight”, ( SELECT SUM( ( “tp”.“length” * “tp”.“width” * “tpt”.“depth” * “d”.“density” * “tp”.“quantity” ) / 1000000 ) FROM “TaskPart” AS “tp” INNER JOIN “TaskPartType” AS “tpt” ON “tp”.“task_part_type_id” = “tpt”.“id” INNER JOIN “Density” AS “d” ON “tpt”.“density_id” = “d”.“id” INNER JOIN “Task” AS “t” ON “tp”.“task_id” = “t”.“id” INNER JOIN “Job” AS “j” ON “t”.“job_id” = “j”.“id” INNER JOIN “Programme” AS “p” ON “j”.“programme_id” = “p”.“id” WHERE “tpt”.“supplier_id” = 1 AND “p”.“id” = :programme_id AND “tp”.“id” <= “TaskPart”.“id” ) AS “running_total”, CEILING( ( SELECT SUM( ( “tp”.“length” * “tp”.“width” * “tpt”.“depth” * “d”.“density” * “tp”.“quantity” ) / 1000000 ) FROM “TaskPart” AS “tp” INNER JOIN “TaskPartType” AS “tpt” ON “tp”.“task_part_type_id” = “tpt”.“id” INNER JOIN “Density” AS “d” ON “tpt”.“density_id” = “d”.“id” INNER JOIN “Task” AS “t” ON “tp”.“task_id” = “t”.“id” INNER JOIN “Job” AS “j” ON “t”.“job_id” = “j”.“id” INNER JOIN “Programme” AS “p” ON “j”.“programme_id” = “p”.“id” WHERE “tpt”.“supplier_id” = 1 AND “p”.“id” = :programme_id AND “tp”.“id” <= “TaskPart”.“id” ) / ( SELECT “coefficient” FROM “Coefficient” WHERE “id” = 6 ) ) AS “category” FROM “TaskPart” INNER JOIN “TaskPartType” ON “TaskPart”.“task_part_type_id” = “TaskPartType”.“id” INNER JOIN “Density” ON “TaskPartType”.“density_id” = “Density”.“id” INNER JOIN “Task” ON “Task”.“id” = “TaskPart”.“task_id” INNER JOIN “Job” ON “Task”.“job_id” = “Job”.“id” INNER JOIN “Programme” ON “Job”.“programme_id” = “Programme”.“id” WHERE “TaskPartType”.“supplier_id” = 1 AND “Programme”.“id” = :programme_id ORDER BY “TaskPart”.“id”
I’ve asked ChatGPT to write the necessary SQL for me, but it’s SQL doesn’t work and I couldn’t work out how to fix it. I can also post that SQL on here if that’s useful.
The reason this query needs to be reordered is that it dictates how how I sort my sheet materials into sequenced loads for me to take to site (my van can only carry so much!). I think it’s feasible for me to import this data into a spreadsheet and do what I need to do there, but I’d rather keep it all in the database if at all possible so as to avoid user error.