I am trying to create an SQL query that displays some information from different tables. But I'm getting the error Subquery returns more than 1 row SQL. I want it to display more than one row.
SELECT c.Name,
jn.ID,
jn.ActualWeight as GrossWt,
jn.JobNo,
COUNT(distinct jn.JobNo) as Jobs,
COUNT(distinct jd.JobID) as Dbriefs,
COUNT(distinct jn.OutTurn) as Outturns,
(select Status from jobstat where CompanyID = jn.CompanyID AND Status = "DEL") as Delivery
FROM job_new jn
LEFT JOIN customer c ON jn.CompanyID = c.Company_ID
LEFT JOIN job_debriefs jd ON jn.JobNo = jd.JobID
LEFT JOIN jobstat js ON jn.CompanyID = js.CompanyID
WHERE jn.CompanyID = 36
I've tried adding GROUP BY and ORDER BY but that doesn't work either. If I remove the select State.... line it only displays one row when it should be displaying over a hundred
You need a group by
:
SELECT c.Name, jn.ID, jn.ActualWeight as GrossWt, jn.JobNo,
COUNT(distinct jn.JobNo) as Jobs,
COUNT(distinct jd.JobID) as Dbriefs,
COUNT(distinct jn.OutTurn) as Outturns,
jobstat
FROM job_new jn LEFT JOIN
customer c
ON jn.CompanyID = c.Company_ID LEFT JOIN
job_debriefs jd
ON jn.JobNo = jd.JobID LEFT JOIN
jobstat js
ON jn.CompanyID = js.CompanyID
WHERE jn.CompanyID = 36
GROUP BY c.Name, jn.ID, jn.ActualWeight as GrossWt, jn.JobNo, js.status
I'm not sure what the subquery is supposed to be doing, so I'm guessing with regards to js.status
.
The problem with your original query is the use of COUNT()
in the SELECT
. This turns the query into an aggregation query. Without a GROUP BY
, exactly one row is returned. In most other databases, you would normally get an error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.