This topic is locked
[SOLVED]

query not working

3/29/2024 10:02:49 AM
PHPRunner General questions
D
Dennis L author

My query is not working. ('28_Day_Total')/('Tests') AS Average - does not give me any results??

SELECT
concretelog_ID,
job_ID,
drawing_numbers,
description,
work_type,
area,
reqd_strength,
pour_date,
yards_poured,
COUNT(DISTINCT concretelog_ID) AS Test_Number,
SUM(7_day_amount_c1) AS 7_day_total,
SUM(14_day_amount_c1) AS 14_day_total,
sum(28_day_amount_c1) + sum(28_day_amount_c2) + sum(28_day_amount_c3) AS 28_day_total,
COUNT(DISTINCT concretelog_ID)*3 AS Tests,
('28_Day_Total')/('Tests') AS Average
FROM concretelogs
GROUP BY drawing_numbers, pour_date

D
DRCR Dev 3/29/2024

Test the query in mysql to get feedback from mysql what is wrong.

Also get into the habit of using backticks, that usually solves the reserved word errors.

P
pmuckleDevClub member 3/30/2024

I put the query into openai, it says:

The issue in your SQL query lies in the calculation of the Average column. Here's the corrected query:

SELECT
concretelog_ID,
job_ID,
drawing_numbers,
description,
work_type,
area,
reqd_strength,
pour_date,
yards_poured,
COUNT(DISTINCT concretelog_ID) AS Test_Number,
SUM(7_day_amount_c1) AS 7_day_total,
SUM(14_day_amount_c1) AS 14_day_total,
SUM(28_day_amount_c1) + SUM(28_day_amount_c2) + SUM(28_day_amount_c3) AS 28_day_total,
COUNT(DISTINCT concretelog_ID)3 AS Tests,
(SUM(28_day_amount_c1) + SUM(28_day_amount_c2) + SUM(28_day_amount_c3)) / (COUNT(DISTINCT concretelog_ID)
3) AS Average
FROM
concretelogs
GROUP BY
drawing_numbers, pour_date;

  1. The Average column was calculated incorrectly. It should be calculated by dividing the sum of the 28-day amounts by the total number of tests (which is the count of distinct concretelog_ID multiplied by 3).
  2. Fixed formatting for better readability.

I haven't checked any of this but I have found openai to be good with SQL

D
Dennis L author 3/30/2024

This works great thank you, but what software are you using for openai??

P
pmuckleDevClub member 3/30/2024

Just ChatGPT 3.5