Multiple table query with count error - sql

Whenever I add a count to the following select statement, it causes it to give me an error, and I'm not sure why:
select "WorkOrder"."WorkOrderID", "Vehicles"."Model", "Vehicles"."Color",
"Vehicles"."LicensePlate", COUNT("WorkOrderPart"."WorkPartID") AS "Parts"
FROM "WorkOrder", "Vehicles", "WorkOrderPart"
WHERE "WorkOrder"."VIN" = "Vehicles"."VIN" AND
"WorkOrder"."WorkOrderID" = "WorkOrderPart"."WorkOrderID";
Whenever I run this, I get the following error:
select "WorkOrder"."WorkOrderID", "Vehicles"."Model", "Vehicles"."Color",
*
ERROR at line 1:
ORA-00937: not a single-group group function
What can I do about this? I am running the SQL command line with an Oracle database.

When you have a count (or other aggregate function), you need to group by the non aggregated items
select
"WorkOrder"."WorkOrderID",
"Vehicles"."Model",
"Vehicles"."Color",
"Vehicles"."LicensePlate",
COUNT("WorkOrderPart"."WorkPartID") AS "Parts"
FROM "WorkOrder", "Vehicles", "WorkOrderPart"
WHERE "WorkOrder"."VIN" = "Vehicles"."VIN"
AND "WorkOrder"."WorkOrderID" = "WorkOrderPart"."WorkOrderID"
group by "WorkOrder"."WorkOrderID", "Vehicles"."Model", "Vehicles"."Color",
"Vehicles"."LicensePlate"
As an aside, it is considered good practice to use explicit joins (unless you're using a prehistoric version of oracle) instead of the where clause
FROM "WorkOrder"
inner join "Vehicles" on "WorkOrder"."VIN" = "Vehicles"."VIN"
left join "WorkOrderPart" on "WorkOrder"."WorkOrderID" = "WorkOrderPart"."WorkOrderID"

Related

Sql subquery issue suming subfields

I am trying to get a subquery to work but i cant get past subquery returned more than 1 value. this is not permitted. No matter how i try to re-write this query I cannot get this sum area to work. I am only fairly new to subqueries so any assistance you could provide would be great the query currently stands as
SELECT dbo.h.DateDelivery, dbo.h.DateInProduction, dbo.h.JobNumber, dbo.h.JobKeyID, dbo.h.QuantityFrames, dbo.h.QuantityGlass, dbo.h.QuantityPanels,
dbo.r.Description, dbo.r.DivisionID, dbo.r.rKeyID, (SELECT SUM(t.QtyPacks) AS packqty
FROM t INNER JOIN
h ON t.JobKeyID = h.JobKeyID
WHERE (t.StageID = 10) OR
(t.StageID = 20) OR
(t.StageID = 28)
GROUP BY t.JobKeyID)
FROM dbo.h INNER JOIN
dbo.r ON dbo.h.rKeyID = dbo.r.rKeyID
WHERE (NOT (dbo.r.rKeyID IN (1, 50, 81, 91)))
You haven't specified which DBMS you are using, however in general a scalar subquery expression must return exactly one column value from one row (see for instance the Oracle documentation).
In your code there is a GROUP BY t.JobKeyID which might cause the subquery to return more than one row.

Error in using subquery in access join query

I need to use the result of a query as a lookup field for another query. What I tried was using a sub query aliased as something, but finally I got some error saying "enter parameter value" for variz.variz and Y.varizname.
I've searched in forums, but I can't find any similar problem.
SELECT sahmeharkas.attrib,
[sahmeharkas]![expenseper] AS Expense,
variz.variz
FROM sahmeharkas
LEFT JOIN
(SELECT variz.varizname FROM variz GROUP BY variz.varizname) as Y
ON sahmeharkas.attrib = Y.variz.varizname
The table variz does not exist outside of the subquery; you should instead use the subquery alias as the table qualifier, e.g.:
select s.attrib, s.expenseper as expense, y.variz
from
sahmeharkas s left join
(select v.variz, v.varizname from variz v group by v.variz, v.varizname) y on
s.attrib = y.varizname

How to execute subquery in Oracle? My query gets parentheses erro

I'm trying to execute a query inside LabVIEW so I can informations stored in a Oracle Database, but when a try to execute a query with parenthesis it doesn't works and gives me this erro:
ADO Error: 0x80004005 Exception occured in Microsoft OLE DB Provider for ODBC Drivers: [Oracle][ODBC][Ora]ORA-00907: parêntese direito não encontrado
Here is the SQL query I'm trying to execute:
SELECT
F.CODIGOFAIXAMODELO,
F.CODIGOMODELO,
F.INICIOESCALA,
F.FUNDOESCALA,
F.FAIXA,
F.DESCFAIXA,
F.ORDEM,
P.CODIGOPROCEDIMENTO
FROM FAIXAS F INNER JOIN PROCEDS P ON F.CODIGOFAIXAMODELO=(
SELECT
CODIGOFAIXAMODELO
FROM PROCEDS
WHERE
PROCEDS.CODIGOFAIXAMODELO=F.CODIGOFAIXAMODELO
LIMIT 1
)
WHERE
F.CODIGOMODELO='%CODIGOMODELO%'
ORDER BY F.ORDEM ASC;
The %CODIGOMODELO% is replaced with a value by LabVIEW.
When I try the following Query it works:
SELECT
F.CODIGOFAIXAMODELO,
F.CODIGOMODELO,
F.INICIOESCALA,
F.FUNDOESCALA,
F.FAIXA,
F.DESCFAIXA,
F.ORDEM,
P.CODIGOPROCEDIMENTO
FROM FAIXAS F INNER JOIN PROCEDS P ON F.CODIGOFAIXAMODELO=P.CODIGOFAIXAMODELO
WHERE
F.CODIGOMODELO='%CODIGOMODELO%'
ORDER BY F.ORDEM ASC;
The problem with the second solution is that it returns me many P.CODIGOPROCEDIMENTO, and what I want is to get only one even when there are many.
there is no LIMIT function in Oracle
you need to use ROWNUM = 1 or OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
Also as it is stated by #APC, you shouldn't be joining your table on a subquery.
I would write it this way. It may be more efficient and more readable to avoid trying to evaluate a subquery inside an expression:
SELECT
F.CODIGOFAIXAMODELO,
F.CODIGOMODELO,
F.INICIOESCALA,
F.FUNDOESCALA,
F.FAIXA,
F.DESCFAIXA,
F.ORDEM,
P.CODIGOPROCEDIMENTO
FROM FAIXAS F
INNER JOIN (
SELECT
P1.CODIGOFAIXAMODELO,
MAX(P1.CODIGOPROCEDIMENTO) AS CODIGOPROCEDIMENTO
FROM PROCEDS P1
GROUP BY P1.CODIGOFAIXAMODELO
) P ON P.CODIGOFAIXAMODELO = F.CODIGOFAIXAMODELO
WHERE F.CODIGOMODELO = '%CODIGOMODELO%'
ORDER BY F.ORDEM ASC;
MAX() is an Aggregate Function that will return only one value for each group - specified in the GROUP BY clause. Therefore, using a subquery and joining on CODIGOFAIXAMODELO ensures that only one row is filtered against the main query.
The results really depend on the key structures, datatypes and how many rows are available in PROCEDS. There are of course other, more complex methods to achieve the same result, such as using Analytic Functions.
I think you can write it this way:
SELECT
F.CODIGOFAIXAMODELO,
F.CODIGOMODELO,
F.INICIOESCALA,
F.FUNDOESCALA,
F.FAIXA,
F.DESCFAIXA,
F.ORDEM,
P.CODIGOPROCEDIMENTO
FROM FAIXAS F INNER JOIN PROCEDS P ON F.CODIGOFAIXAMODELO=P.CODIGOFAIXAMODELO and ROWNUM = 1
WHERE
F.CODIGOMODELO='%CODIGOMODELO%'
ORDER BY F.ORDEM ASC;

SQL COUNT FORM JOIN TABLES

I have the following sql command:
SELECT "USERNAME"."TOPICS".VALUE,
"USERNAME"."TOPICS".QID,
"USERNAME"."QUESTION".QRATING
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
It works really well, but I want to count how many element returns. So I tried to use:
SELECT COUNT("USERNAME"."TOPICS".QID)
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
But I get the error :
Column reference 'USERNAME.TOPICS.VALUE' is invalid. When the SELECT
list contains at least one aggregate then all entries must be valid
aggregate expressions.
What is the problem?
Hmmm. The ORDER BY should be getting the error, not the SELECT. However, your query would be much easier to understand using table aliases:
SELECT COUNT(t.QID)
FROM "USERNAME"."TOPICS" t JOIN
"USERNAME"."QUESTION" q
ON t.QID = q.QID AND t.VALUE = 'kia';
If the first query works, I see no reason why this would not (and your original without the ORDER BY should also work).

Whats wrong with this nested query?

I am trying to write a query to return the id of the latest version of a market index stored in a database.
SELECT miv.market_index_id market_index_id from ref_market_index_version miv
INNER JOIN ref_market_index mi ON miv.market_index_id = mi.id
WHERE mi.short_name='dow30'
AND miv.version_num = (SELECT MAX(m1.version_num) FROM ref_market_index_version m1 INNER JOIN ref_market_index m2 ON m1.market_index_id = m2.id )
The above SQL statement can be (roughly) translated into the form:
SELECT some columns FROM SOME CRITERIA MATCHED TABLES
WHERE mi.short_name='some name'
AND miv.version_num = SOME NUMBER
What I don't understand is that when I supply an actual number (instead of a sub query), the SQL statement works - also, when I test the SUB query used to determine the latest version number, that also works - however, when I attempt to use the result returned by sub query in the outer (parent?) query, it returns 0 rows - what am I doing wrong here?
Incidentally, I also tried an IN CLAUSE instead of the strict equality match i.e.
... AND miv.version_num IN (SUB QUERY)
That also resulted in 0 rows, although as before, when running the parent query with a hard coded version number, I get 1 row returned (as expected).
BTW I am using postgeresql, but I prefer the solution to be db agnostic.
The problem is probably that the max(version_num) doesn't exist for 'dow30'.
Try the following correlated subquery:
SELECT miv.market_index_id market_index_id
from ref_market_index_version miv INNER JOIN
ref_market_index mi
ON miv.market_index_id = mi.id
WHERE mi.short_name='dow30' AND
miv.version_num = (SELECT MAX(m1.version_num)
FROM ref_market_index_version m1 INNER JOIN
ref_market_index m2
ON m1.market_index_id = m2.id
where m1.short_name = 'dow30'
)
I added the where clause in the subquery.