Why am I getting this error? Error is showing up in the OR clause, but I'm selecting gl_ID inside the SELECT statement.
; DECLARE v_firstDate DATETIME
SET #p_ID = 368
SELECT SUBDATE(NOW(), 1) INTO v_firstDate;
SELECT t_ID, t_firstName, t_lastName
FROM t_table
WHERE
((#p_ID IN (
SELECT tt_ID
FROM tt_tableTwo
INNER JOIN st_stats ON (st_ID = tt_ID)
INNER JOIN da_data ON (da_ID = st_ID AND da_name IN ("allCompanies", "allglobals"))
)
))
OR
(
(gl_ID IN ( //problem is here
SELECT gl_ID
FROM gl_globals
INNER JOIN tr_transport ON (tr_id = gl_caseID AND tr_idOther = #p_ID)
INNER JOIN co_countries ON (co_ID = gl_ID AND co_ID = #p_ID)
)
)
)
Error message:
Unknown column 'gl_ID' in 'IN/ALL/ANY subquery'
Should I be using an AS or a HAVING?
gl_ID is not accessible in the OR clause because it doesn't exist in t_table. An inner join under the first FROM clause solves the problem.
; DECLARE v_firstDate DATETIME
SET #p_ID = 368
SELECT SUBDATE(NOW(), 1) INTO v_firstDate;
SELECT t_ID, t_firstName, t_lastName
FROM t_table
INNER JOIN gl_globals ON (t_ID = gl_ID) // fix
WHERE
((#p_ID IN (
SELECT tt_ID
FROM tt_tableTwo
INNER JOIN st_stats ON (st_ID = tt_ID)
INNER JOIN da_data ON (da_ID = st_ID AND da_name IN ("allCompanies", "allglobals"))
)
))
OR
(
(gl_ID IN (
SELECT gl_ID
FROM gl_globals
INNER JOIN tr_transport ON (tr_id = gl_caseID AND tr_idOther = #p_ID)
INNER JOIN co_countries ON (co_ID = gl_ID AND co_ID = #p_ID)
)
)
)
i am try to do somethink like this
UPDATE BA C
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`To` FROM `Trans`
) A ON C.`UserID` = A.`To`
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`From` FROM `Trans`
) B ON C.`UserID` = B.`From`
SET `Bal` = A.`LSC` - B.`LSC`;
and this is not workink :\
when i just do 1 inner join and run the query twice once to A and second to be it work but i want to do it in one query ...
UPDATE BA C
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`To` FROM `Trans`
) A ON C.`UserID` = A.`To`
SET `Bal` = A.`LSC`; // Work
The view below does not return correct results. I am trying to get the sum of Picked, Printed & Scanned grouped by Plan_Id & PartNum. I need to return the correct totals regardless if there are corresponding records in the child tables. I know how to do it if I use three different views and join them, but how do i do it all in a single view? Any help appreciated.
SELECT
`prod_plan`.`Prp_ProdPlanId` AS `PlanId`,
`prod_plan`.`Prp_PartNum` AS `PartNum`,
sum(`prod_plan`.`Prp_Picked`) AS `Picked`,
sum(`printed`.`PtQty`) AS `Printed`,
sum(`scanned`.`PtQty`) AS `Scanned`
FROM
(
(
`prod_plan`
LEFT JOIN `product_trans` `printed` ON (
(
(
`printed`.`PtPlanId` = `prod_plan`.`Prp_ProdPlanId`
)
AND (
`printed`.`PtPartNum` = `prod_plan`.`Prp_PartNum`
)
)
)
)
LEFT JOIN `product_trans` `scanned` ON (
(
(
`scanned`.`PtPlanId` = `prod_plan`.`Prp_ProdPlanId`
)
AND (
`scanned`.`PtPartNum` = `prod_plan`.`Prp_PartNum`
)
)
)
)
WHERE
(
(
`printed`.`PtPart` = 'Barcode Print'
)
AND (
`scanned`.`PtPart` = 'Barcode Scan'
)
)
GROUP BY
`prod_plan`.`Prp_ProdPlanId`,
`prod_plan`.`Prp_PartNum`
You need to check PtPart in the ON clauses. Otherwise, you won't get the rows with no matches in the child tables, because those columns will be NULL.
SELECT
`prod_plan`.`Prp_ProdPlanId` AS `PlanId`,
`prod_plan`.`Prp_PartNum` AS `PartNum`,
sum(`prod_plan`.`Prp_Picked`) AS `Picked`,
sum(`printed`.`PtQty`) AS `Printed`,
sum(`scanned`.`PtQty`) AS `Scanned`
FROM `prod_plan`
LEFT JOIN `product_trans` `printed`
ON `printed`.`PtPlanId` = `prod_plan`.`Prp_ProdPlanId`
AND `printed`.`PtPartNum` = `prod_plan`.`Prp_PartNum`
AND `printed`.`PtPart` = 'Barcode Print'
LEFT JOIN `product_trans` `scanned`
ON `scanned`.`PtPlanId` = `prod_plan`.`Prp_ProdPlanId`
AND `scanned`.`PtPartNum` = `prod_plan`.`Prp_PartNum`
AND `scanned`.`PtPart` = 'Barcode Scan'
GROUP BY
`prod_plan`.`Prp_ProdPlanId`,
`prod_plan`.`Prp_PartNum`
Here is what I have done. Below are 3 views that work correctly.
v_trans_printed:
SELECT
product_trans.PtPlanId AS PlanId,
product_trans.PtLot AS Lot,
product_trans.PtPartNum AS PartNum,
Sum(product_trans.PtQty) AS Printed
FROM
product_trans
WHERE
product_trans.PtPart = 'Barcode Print'
GROUP BY
product_trans.PtPlanId,
product_trans.PtLot,
product_trans.PtPartNum
v_trans_scanned:
SELECT
product_trans.PtPlanId AS PlanId,
product_trans.PtLot AS Lot,
product_trans.PtPartNum AS PartNum,
Sum(product_trans.PtQty) AS Scanned
FROM
product_trans
WHERE
product_trans.PtPart = 'Barcode Scan'
GROUP BY
product_trans.PtPlanId,
product_trans.PtLot,
product_trans.PtPartNum
And here I put them all together. This returns the correct results:
vSELECT
prod_plan.Prp_ProdPlanId AS PlanId,
prod_plan.Prp_Lot AS Lot,
prod_plan.Prp_PartNum AS PartNum,
Sum(prod_plan.Prp_Picked) AS Picked,
Printed.Printed AS Printed,
Scanned.Scanned AS Scanned
FROM
prod_plan
LEFT JOIN v_trans_printed AS Printed ON Printed.PlanId = prod_plan.Prp_ProdPlanId AND Printed.Lot = prod_plan.Prp_Lot AND Printed.PartNum = prod_plan.Prp_PartNum
LEFT JOIN v_trans_scanned AS Scanned ON Scanned.PlanId = prod_plan.Prp_ProdPlanId AND Scanned.Lot = prod_plan.Prp_Lot AND Scanned.PartNum = prod_plan.Prp_PartNum
GROUP BY
prod_plan.Prp_ProdPlanId,
prod_plan.Prp_Lot,
prod_plan.Prp_PartNum
But it would be better if I could use one view.
Note: I left out Lot originally by accident. But it didn't affect the results w. the sample data set.
I would like to use JOIN instead of IN in the following SQL query. I can't figure out how to do it.
SELECT * FROM shop_orders WHERE
id IN (SELECT orders_id FROM shop_orders_data WHERE closed='1' /*AND backorder='0'*/ AND exhibition_id='389' AND
exhibition_id IN (SELECT id FROM shop_exhibitions WHERE
country_id IN (SELECT id FROM countries WHERE id='72')) AND in_country = '72' AND
exhibition_id IN (SELECT id FROM shop_exhibitions WHERE start<=1336946400 AND end>1336600800)) AND
id IN (SELECT orders_id FROM shop_orders_products WHERE
products_id IN (SELECT id FROM shop_products WHERE artno='120000' OR name LIKE '%120000%')) AND created>=1333231200 AND created<1333663200 ORDER BY created DESC
I tried this:
SELECT
s.*
FROM
shop_orders s
INNER JOIN shop_orders_data od ON s.id=od.orders_id
INNER JOIN shop_exhibitions se ON od.exhibition_id=se.id
INNER JOIN countries co ON se.country_id=co.id
INNER JOIN shop_orders_products sop ON s.id=sop.orders_id
INNER JOIN shop_products sp
ON sop.products_id=sp.id
WHERE od.closed=1
AND ( sp.artno='120000' or sp.name LIKE '%120000%' )
AND ( od.exhibition_id='389')
AND ( od.in_country = '72')
AND ( se.start <=1336946400)
AND ( se.end >1336600800)
AND ( se.created>=1333231200)
AND ( se.created<1333663200)
ORDER BY `s`.`created` DESC
I this correct??
See if this works (and study the code to learn how it works):
SELECT *
FROM shop_orders so
JOIN shop_orders_data sod ON (
(so.id = sod.orders_id)
AND (sod.closed = '1')
/*AND (sod.backorder = '0') */
AND (sod.exhibition_id = '389')
AND (sod.in_country = '72')
)
JOIN shop_exhibitions se ON (
(sod.exhibition_id = se.id)
AND (se.start <= 1336946400)
AND (se.end > 1336600800)
)
JOIN countries c ON (
(se.country_id = c.id)
AND (c.id = '72')
)
JOIN shop_orders_products sop ON (
(so.id = sop.orders_id)
)
JOIN shop_products sp ON (
(sop.products_id = sp.id)
AND ((sp.artno='120000') OR (sp.name LIKE '%120000%'))
)
WHERE (so.created >= 1333231200) AND (so.created < 1333663200)
ORDER BY so.created DESC;
The join syntax works like this:
SELECT field1,field2,field3
FROM FirstTable
JOIN SecondTable ON (FirstTable.PrimaryKey = SecondTable.ForeignKey)
JOIN ThirdTable ON (FirstTable.PrimaryKey = ThirdTable.ForeignKey)
Try applying this approach to your query.
I have a query (equivalent of View in ORACLE, SQL Server) that I saved in an Access database. Now I need to add another column to my select list. But whenever I change the SQL I get errors. Let's say I remove one comma and then put it back. I get errors like "Syntax error after From","Syntax error in Join operation". So if I change anything and then undo the change I still get errors. How can I avoid this?
Edit:Before editing
SELECT A_B.ALICI, Q.QAINOM, M.TIP, Q.QATARIX, Q.CEMIBORC, R2.ADI AS SAT_NOV, M.TAMADI, M.MARK, SA.MIQDAR, SA.SAQIYM, SA.CEMI, SA.FAIZ, A_B.BORC_SU, A_B.MEBLEG, A_B.FERQ, K_G.CEMI_ODEN, A_B.MOBTEL, SA.NOTE
FROM ([SELECT Round(Sum([DBKASSA].[MEBLEQ]),3) AS CEMI_ODEN, DBKASSA.KODAL_GT
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]) AND ((DBKASSA.TARIX)=[INTILLDATE]))
GROUP BY DBKASSA.KODAL_GT]. AS K_G RIGHT JOIN ([SELECT AL.KODALAN,AL.MOBTEL, IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC]) AS BORC_SU, IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]) AS MEBLEG, (IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]))-(IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC])) AS FERQ, AL!OBYEKT & (IIf(AL!NUMAY Is Not Null," - " & AL!NUMAY)) AS ALICI
FROM
(
SELECT DBKASSA.KODAL_GT, Sum(DBKASSA.MEBLEQ) AS Su_MEBLEQ
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]))
GROUP BY DBKASSA.KODAL_GT
) AS OD_AL
RIGHT JOIN (
(
SELECT DBQAIME.KODALAN, Sum(DBQAIME.CEMIBORC) AS Su_CEMIBORC
FROM DBQAIME
WHERE (((DBQAIME.KODALAN)=[INBUYERID]))
GROUP BY DBQAIME.KODALAN
) AS B_AL
RIGHT JOIN DBALAN AS AL ON B_AL.KODALAN = AL.KODALAN) ON OD_AL.KODAL_GT = AL.KODALAN
WHERE (((AL.KODALAN)=[INBUYERID]))
]. AS A_B INNER JOIN DBQAIME AS Q ON A_B.KODALAN = Q.KODALAN) ON K_G.KODAL_GT = A_B.KODALAN) INNER JOIN (DBMAL AS M INNER JOIN (DBSA AS SA INNER JOIN [SELECT R2.KOD, R2.VID, R2.ADI
FROM DBRAB2 AS R2
WHERE (((R2.VID)=3))]. AS R2 ON SA.KODEMLNO = R2.KOD) ON M.KODMAL = SA.KODMAL) ON Q.QAINOM = SA.QAINOM
WHERE (((Q.QAINOM)=[INSALEINVOICE]))
ORDER BY M.TIP, M.TAMADI;
After editing;
SELECT A_B.ALICI, Q.QAINOM, M.TIP, Q.QATARIX, Q.CEMIBORC, R2.ADI AS SAT_NOV, M.TAMADI, M.MARK, SA.MIQDAR, SA.SAQIYM, SA.CEMI, SA.FAIZ, A_B.BORC_SU, A_B.MEBLEG, A_B.FERQ, K_G.CEMI_ODEN, A_B.MOBTEL, SA.NOTE
FROM ([SELECT Round(Sum([DBKASSA].[MEBLEQ]),3) AS CEMI_ODEN, DBKASSA.KODAL_GT
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]) AND ((DBKASSA.TARIX)=[INTILLDATE]))
GROUP BY DBKASSA.KODAL_GT]. AS K_G RIGHT JOIN ([SELECT AL.KODALAN,AL.MOBTEL, IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC]) AS BORC_SU, IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]) AS MEBLEG, (IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]))-(IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC])) AS FERQ, AL!OBYEKT & (IIf(AL!NUMAY Is Not Null," - " & AL!NUMAY)) AS ALICI
FROM
(
SELECT DBKASSA.KODAL_GT, Sum(DBKASSA.MEBLEQ) AS Su_MEBLEQ
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]))
GROUP BY DBKASSA.KODAL_GT
) AS OD_AL
RIGHT JOIN (
(
SELECT DBQAIME.KODALAN, Sum(DBQAIME.CEMIBORC) AS Su_CEMIBORC
FROM DBQAIME
WHERE (((DBQAIME.KODALAN)=[INBUYERID]))
GROUP BY DBQAIME.KODALAN
) AS B_AL
RIGHT JOIN DBALAN AS AL ON B_AL.KODALAN = AL.KODALAN) ON OD_AL.KODAL_GT = AL.KODALAN
WHERE (((AL.KODALAN)=[INBUYERID]))
]. AS A_B INNER JOIN DBQAIME AS Q ON A_B.KODALAN = Q.KODALAN) ON K_G.KODAL_GT = A_B.KODALAN) INNER JOIN (DBMAL AS M INNER JOIN (DBSA AS SA INNER JOIN [SELECT R2.KOD, R2.VID, R2.ADI
FROM DBRAB2 AS R2
WHERE (((R2.VID)=3))]. AS R2 ON SA.KODEMLNO = R2.KOD) ON M.KODMAL = SA.KODMAL) ON Q.QAINOM = SA.QAINOM
WHERE (((Q.QAINOM)=[INSALEINVOICE]))
ORDER BY M.TIP, M.TAMADI;
By the way, it worked in MS Access 2010. Mine is Access 2003
What has happened is that your subquery or derived table is now bracketed like so
[stuff here].
This causes an error when editing.
The easiest thing to do is use notepad or such like, replace []. with () and paste back.
Try:
SELECT A_B.ALICI,
Q.QAINOM,
M.TIP,
Q.QATARIX,
Q.CEMIBORC,
R2.ADI AS SAT_NOV,
M.TAMADI,
M.MARK,
SA.MIQDAR,
SA.SAQIYM,
SA.CEMI,
SA.FAIZ,
A_B.BORC_SU,
A_B.MEBLEG,
A_B.FERQ,
K_G.CEMI_ODEN,
A_B.MOBTEL,
SA.NOTE
FROM ((SELECT Round(SUM([DBKASSA].[MEBLEQ]), 3) AS CEMI_ODEN,
DBKASSA.KODAL_GT
FROM DBKASSA
WHERE ( ( ( DBKASSA.TIP ) = 1 )
AND ( ( DBKASSA.KODAL_GT ) = [INBUYERID] )
AND ( ( DBKASSA.TARIX ) = [INTILLDATE] ) )
GROUP BY DBKASSA.KODAL_GT) AS K_G
RIGHT JOIN ((SELECT AL.KODALAN,
AL.MOBTEL,
Iif([Su_CEMIBORC] IS NULL, 0, [Su_CEMIBORC])
AS
BORC_SU,
Iif([Su_MEBLEQ] IS NULL, 0, [Su_MEBLEQ])
AS
MEBLEG,
( Iif([Su_MEBLEQ] IS NULL, 0, [Su_MEBLEQ]) ) - (
Iif([Su_CEMIBORC] IS NULL, 0, [Su_CEMIBORC]) )
AS
FERQ,
AL ! OBYEKT & ( Iif(AL ! NUMAY IS NOT NULL,
" - " & AL ! NUMAY) ) AS
ALICI
FROM (SELECT DBKASSA.KODAL_GT,
SUM(DBKASSA.MEBLEQ) AS Su_MEBLEQ
FROM DBKASSA
WHERE
( ( ( DBKASSA.TIP ) = 1 )
AND ( ( DBKASSA.KODAL_GT ) = [INBUYERID] ) )
GROUP BY DBKASSA.KODAL_GT) AS OD_AL
RIGHT JOIN ( (SELECT DBQAIME.KODALAN,
SUM(DBQAIME.CEMIBORC) AS
Su_CEMIBORC
FROM DBQAIME
WHERE ((
( DBQAIME.KODALAN ) = [INBUYERID] ))
GROUP BY DBQAIME.KODALAN) AS B_AL
RIGHT JOIN DBALAN AS AL
ON B_AL.KODALAN = AL.KODALAN)
ON OD_AL.KODAL_GT = AL.KODALAN
WHERE (( ( AL.KODALAN ) = [INBUYERID] ))) AS A_B
INNER JOIN DBQAIME AS Q
ON A_B.KODALAN = Q.KODALAN)
ON K_G.KODAL_GT = A_B.KODALAN)
INNER JOIN (DBMAL AS M
INNER JOIN (DBSA AS SA
INNER JOIN (SELECT R2.KOD,
R2.VID,
R2.ADI
FROM DBRAB2 AS R2
WHERE (( ( R2.VID ) = 3 ))) AS R2
ON SA.KODEMLNO = R2.KOD)
ON M.KODMAL = SA.KODMAL)
ON Q.QAINOM = SA.QAINOM
WHERE (( ( Q.QAINOM ) = [INSALEINVOICE] ))
ORDER BY M.TIP,
M.TAMADI;