Error in using subquery in access join query - sql

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

Related

Joining a group by subquery with substr columns

I am trying to join a group by subquery with another table but am running in to issues since the subquery contains aliases which are only valid within the subquery. The issue is that the AREA table only has the 3 digit locker id embedded as part of a longer string in the ARE_CODE column which is why I am using substr. Here is the group by subquery:
SELECT SUBSTR(ARE_CODE,0,3) AS "Locker", COUNT(ARE_UID) AS "Qty"
FROM AREA
WHERE SUBSTR(ARE_CODE,0,1)='L'
GROUP BY SUBSTR(ARE_CODE,0,3)
It runs fine on its own - the problem is that I am trying to join this with another table in the main query. The alias is only valid within the subquery itself and can't be referenced as I did below. I have also tried joining the substr function directly but that doesn't work either.
SELECT P.Locker, P.Permit, L.Qty
FROM PERMITS P
INNER JOIN (SELECT SUBSTR(ARE_CODE,0,3) AS "Locker", COUNT(ARE_UID) AS "Qty"
FROM AREA_LKP
WHERE SUBSTR(ARE_CODE,0,1)='L'
GROUP BY SUBSTR(ARE_CODE,0,3) L ON L.Locker = P.Locker
Any suggestions on how to work around this issue?
You just need to remove the double quotes from the alias as they makes the alias case sensistive and you must have to use them with double quotes wherever you want to use them in your query with exact case.
So better not to use the double quotes for alias untill and unless you want to give the case sensitive name or want space in the alias.
SELECT P.Locker, P.Permit, L.Qty
FROM PERMITS P
INNER JOIN (SELECT SUBSTR(ARE_CODE,0,3) AS Locker, COUNT(ARE_UID) AS Qty
FROM AREA_LKP
WHERE SUBSTR(ARE_CODE,0,1)='L'
GROUP BY SUBSTR(ARE_CODE,0,3) L ON L.Locker = P.Locker
The issue are the double quotes. But your query has other eccentricities:
In strings, the first character is at 1, not 0 (although 0 is allowed).
LIKE is a good way to do string comparisons.
So:
SELECT P.Locker, P.Permit, L.Qty
FROM PERMITS P INNER JOIN
(SELECT SUBSTR(ARE_CODE, 1, 3) AS Locker, COUNT(*) AS Qty
FROM AREA_LKP
WHERE ARE_CODE LIKE 'L%'
GROUP BY SUBSTR(ARE_CODE, 1, 3)
) L
ON L.Locker = P.Locker;
I should note that LIKE permits the use of an index on the column, but SUBSTR() generally prevents that.
Why you`re set the alias of the table at the end of the subquery?
INNER JOIN (SELECT SUBSTR(ARE_CODE,0,3) AS "Locker", COUNT(ARE_UID) AS "Qty"
FROM AREA_LKP
WHERE SUBSTR(ARE_CODE,0,1)='L'
GROUP BY SUBSTR(ARE_CODE,0,3) L
Try:
INNER JOIN (SELECT SUBSTR(ARE_CODE,0,3) AS "Locker", COUNT(ARE_UID) AS "Qty"
FROM AREA_LKP L
WHERE SUBSTR(ARE_CODE,0,1)='L'
GROUP BY SUBSTR(ARE_CODE,0,3)
For sharing expressions between different statements take a look at the with-statement:
https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/with.html
Finally, are you sure that:
L.Locker = P.Locker
Can be evaluated as expected? So is the substringged value of L.Locker same as P.Locker?

MS Access SQL sub Query using outer query result

I'm attempting to run a sub query based on the result of an outer query. The issue that I am having is that instead of using the outer query, I get a prompt for a value from the subquery.
SELECT Facilities.CustomerName, Facilities.FacilityName,
Facilities.AnnualPlan, Facilities.AppCo1,
(SELECT YeildDB.CornYield
FROM YeildDB
WHERE Facilites.AppCo1 = YeildDB.FIPS) AS Expr1
FROM Facilities
The goal is that the sub query should use the value from Facilities.AppCo1 to match with the value in YeildDB.FIPS and then return the corresponding value in YeildDB.CornYeild.
Currently I get a prompt asking for the YeildDB.FIPS value instead of the sub query using the outer query value.
Your code should work. But you can also express this using a LEFT JOIN:
SELECT Facilities.CustomerName, Facilities.FacilityName,
Facilities.AnnualPlan, Facilities.AppCo1,
YeildDB.CornYield
FROM Facilities LEFT JOIN
YeildDB
ON Facilties.AppCo1 = YeildDB.FIPS;
I noticed that you misspelled Facilities -- and that is probably why your version doesn't work. This is one reason to use table aliases:
SELECT f.CustomerName, f.FacilityName,
f.AnnualPlan, f.AppCo1,
y.CornYield
FROM Facilities as f LEFT JOIN
YeildDB as y
ON f.AppCo1 = y.FIPS;
Your sub query may be returning multiple values, hence the prompt asking you to specify which one you want. You can fix this (or at least hide this issue) by specifying top 1:
SELECT Facilities.CustomerName, Facilities.FacilityName,
Facilities.AnnualPlan, Facilities.AppCo1,
(SELECT TOP 1 YeildDB.CornYield
FROM YeildDB
WHERE Facilites.AppCo1 = YeildDB.FIPS) AS Expr1
FROM Facilities

SQL Math Operation In Correlated Subquery

I am working with three tables, basically, one is a bill of materials, one contains part inventory, and the last one contains work orders or jobs. I am trying to find out if it is possible to have a correlated subquery that can perform a math operation using a value from the outer query. Here's an example of what I'm trying to do:
SELECT A.work_order,A.assembly,A.job_quantity,
(SELECT COUNT(X.part_number)
FROM bom X
WHERE X.assembly = A.assembly
AND (X.quantity_required * A.job_quantity) >= (SELECT Y.quantity_available FROM inventory Y WHERE
Y.part_number = X.part_number)) AS negatives
FROM work_orders A
ORDER BY A.assembly ASC
I am attempting to find out, for a given work order, if there are parts that we do not have enough of to build the assembly. I'm currently getting an "Error correlating fields" error. Is it possible to do this kind of operation in a single query?
Try moving the subquery to a join, something like this:
SELECT a.work_order, a.assembly, a.job_quantity, n.negatives
FROM work_orders a JOIN (SELECT x.part_number, COUNT(x.part_number) as negatives
FROM bom x JOIN work_orders b
ON x.assembly = b.assembly
WHERE (x.quantity_required * b.job_quantity) >= (SELECT y.quantity_available
FROM inventory y WHERE
y.part_number = x.part_number)
GROUP BY x.part_number) n
ON a.part_number = n.part_number
ORDER BY a.assembly ASC
Or create a temporary cursor with the subquery and then use it to join the main table.
Hope this helps.
Luis

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).

Select a field called "return" in postgreSQL

I'm having a problem with a query in postgres, the table cgporders_items has a field called return, I cannot get actual result of that field with this query, it returns me al ceros.
SELECT "Cgporder".id AS "Cgporder__id"
,"Sale".preorder_number AS "Sale__preorder_number"
,"Contact".id AS "Contact__id"
,"Contact".NAME AS "Contact__name"
,"Ptype".NAME AS "Ptype__name"
,(
SELECT code
FROM products
WHERE id = "CgporderItem".parent_id
) AS "Product__parent_code"
,"Product".id AS "Product__id"
,"Product".code AS "Product__code"
,"Product".NAME AS "Product__name"
,"CgporderItem".quantity AS "CgporderItem__quantity"
,"CgporderItem".return AS "CgporderItem__return"
,"CgporderItem".cep_id AS "CgporderItem__cep"
FROM cgporders AS "Cgporder"
INNER JOIN contacts AS "Contact" ON ("Contact".id = "Cgporder".contact_id)
INNER JOIN cgporders_items AS "CgporderItem" ON ("Cgporder".id = "CgporderItem".cgporder_id)
INNER JOIN products AS "Product" ON ("Product".id = "CgporderItem".product_id)
INNER JOIN ptypes AS "Ptype" ON ("Ptype".id = "Product".ptype_id)
LEFT JOIN cgporders_sales AS "CgporderSale" ON ("Cgporder".id = "CgporderSale".cgporder_id)
LEFT JOIN sales AS "Sale" ON ("Sale".id = "CgporderSale".sale_id)
WHERE "CgporderItem".parent_id != 0
AND "Cgporder"."issue_date" >= '2015-11-27'
AND "Cgporder"."issue_date" <= '2015-11-27'
AND "Cgporder"."status" = 'confirmed'
ORDER BY "Ptype".NAME
,"Product"."code";
There are actually a lots of rows that matches the select condition, but it return cero on "CgporderItem".return AS "CgporderItem__return"
If I make a simple query like select "return" from cgporders_items it works. But in this query it does not work.
Can you help me please?
"return" is a reserved word in SQL, but not in Postgres. See the list here. The following code works find in Postgres (SQL Fiddle is here):
create table dum (return int);
select dum.return from dum;
Your problem is something else. If I had to guess, the where clause is too restrictive (the condition on dates is a bit suspect).