In an MS Access database I try to make a join on three tables with conditions.
My current approach does not work:
SELECT
P.[COL1],
P.[COL2],
P.[COL3],
P.[COL4],
P.[COL5],
P.[PRGUID],
P.[COL6],
P.[COL7],
ARTIKEL.[COL1],
ARTIKEL.[NUMBER],
P.[PST_NR],
RECHNUNGEN.[RG_STORNO]
FROM P, A, R
WHERE R.[TIMEST] > #{from:yyyy-MM-dd HH:mm:ss}#
AND R.[TIMEST] < #{until:yyyy-MM-dd HH:mm:ss}#
AND A.[NUMBER] = VAL(P.[PLUX])
AND R.[RGUID] = P.[PRGUID] "
;
because the datasets in table P that do not have a corresponding entry in table A (condition: A.[NUMMER] = VAL(P.[PLUX])) are not returned.
I tried an approach with JOIN but I cannot find the right syntax.
Thank you #Gustav
The GUI designer helped (didn't even know it is there - just used SQL so far).
SELECT P.ColumnX, A.ColumnY, R.ColumnZ
FROM (P LEFT OUTER JOIN A.[NUMBER]) = VAL(P.[PLUX]))
INNER JOIN R ON P.[PRGUID] = R.[RGUID]
WHERE R.[TIMEST] > #yyyy-MM-dd HH:mm:ss#
AND R.[TIMEST] < #yyyy-MM-dd HH:mm:ss#
Related
I'm trying to update records through a single inner join with multiple criteria. My best effort so far is this:
UPDATE FormData d
INNER JOIN ProductGrowthDays g
ON d.ProductCode = g.ProductCode AND
ON d.ProductionLineCode = g.ProductionLineCode AND
ON g.MonthIndex = MONTH(d.SowingDate)
SET d.EstimatedDays = g.GrowingDays
WHERE
d.EventTypeId = 1
Access gives the error 'Syntax error (missing operator)' and highlights the 'r' in 'd.ProductCode'. The join is guaranteed to give a single row.
Could anyone give me pointers on how to fix this?
D'oh. The answer was as follows:
UPDATE FormData d
INNER JOIN ProductGrowthDays g
ON (d.ProductCode = g.ProductCode
AND d.ProductionLineCode = g.ProductionLineCode
AND g.MonthIndex = MONTH(d.SowingDate))
SET d.EstimatedDays = g.GrowingDays
WHERE
d.EventTypeId = 1
I was sure I tried that at one point, but obviously not. Well, leaving this here if someone else should need it.
I have a view with the following definition:
select
`cb_trans_detail`.`numero_partida` AS `numero_partida`,
`cb_trans_head`.`fecha_partida` AS `fecha_partida`,
`cb_trans_detail`.`concepto_partida` AS `concepto_partida`,
`cb_cuenta`.`nombre_cuenta` AS `nombre_cuenta`,
`cb_cuenta`.`codigo_mayor` AS `codigo_mayor`,
`cb_mayor`.`nombre_mayor` AS `nombre_mayor`,
`cb_mayor`.`categoria` AS `categoria`,
`cb_categoria`.`nombre` AS `nombre`,
`cb_categoria`.`presentacion` AS `presentacion`,
`cb_trans_detail`.`codigo_cuenta` AS `codigo_cuenta`,
sum(`cb_trans_detail`.`debito_partida`) AS `Debitos`,
sum(`cb_trans_detail`.`credito_partida`) AS `Creditos`,
`cb_cuenta`.`saldo_inicial` AS `saldo_inicial`,
((`cb_cuenta`.`saldo_inicial` +
sum(`cb_trans_detail`.`debito_partida`)) -
sum(`cb_trans_detail`.`credito_partida`)) AS `Saldo`,
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`)
AS `Codigo`
from
((((`cb_trans_detail` join `cb_cuenta`
on(((`cb_trans_detail`.`codigo_cuenta` = `cb_cuenta`.`codigo_cuenta`)
and (`cb_trans_detail`.`categoria` = `cb_cuenta`.`categoria`)
and (`cb_trans_detail`.`codigo_mayor` = `cb_cuenta`.`codigo_mayor`))))
join `cb_mayor` on(((`cb_cuenta`.`codigo_mayor` = `cb_mayor`.`codigo_mayor`)
and (`cb_cuenta`.`categoria` = `cb_mayor`.`categoria`))))
join `cb_categoria` on(((`cb_mayor`.`categoria` = `cb_categoria`.`categoria`)
and (`cb_trans_detail`.`categoria` = `cb_categoria`.`categoria`))))
left join `cb_trans_head` on((`cb_trans_detail`.`numero_partida` =
`cb_trans_head`.`numero_partida`)))
where
(`cb_categoria`.`presentacion` = '1')
group by concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,
`cb_trans_detail`.`codigo_cuenta`)
If I select from this view as follows:
SELECT
`balance_general_view`.`numero_partida`,
`balance_general_view`.`fecha_partida`,
`balance_general_view`.`concepto_partida`,
`balance_general_view`.`nombre_cuenta`,
`balance_general_view`.`codigo_mayor`,
`balance_general_view`.`nombre_mayor`,
`balance_general_view`.`categoria`,
`balance_general_view`.`nombre`,
`balance_general_view`.`presentacion`,
`balance_general_view`.`codigo_cuenta`,
`balance_general_view`.`Debitos`,
`balance_general_view`.`Creditos`,
`balance_general_view`.`saldo_inicial`,
`balance_general_view`.`Saldo`,
`balance_general_view`.`Codigo`
FROM
`balance_general_view`
WHERE
`balance_general_view`.`fecha_partida` BETWEEN '2014-01-01' AND '2014-01-31'
this yields a different result than if I execute the query as follows:
select
`cb_trans_detail`.`numero_partida` AS `numero_partida`,
`cb_trans_head`.`fecha_partida` AS `fecha_partida`,
`cb_trans_detail`.`concepto_partida` AS `concepto_partida`,
`cb_cuenta`.`nombre_cuenta` AS `nombre_cuenta`,
`cb_cuenta`.`codigo_mayor` AS `codigo_mayor`,
`cb_mayor`.`nombre_mayor` AS `nombre_mayor`,
`cb_mayor`.`categoria` AS `categoria`,
`cb_categoria`.`nombre` AS `nombre`,
`cb_categoria`.`presentacion` AS `presentacion`,
`cb_trans_detail`.`codigo_cuenta` AS `codigo_cuenta`,
sum(`cb_trans_detail`.`debito_partida`) AS `Debitos`,
sum(`cb_trans_detail`.`credito_partida`) AS `Creditos`,
`cb_cuenta`.`saldo_inicial` AS `saldo_inicial`,
((`cb_cuenta`.`saldo_inicial` + sum(`cb_trans_detail`.`debito_partida`)) - sum(`cb_trans_detail`.`credito_partida`)) AS `Saldo`,
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`) AS `Codigo`
from
((((`cb_trans_detail` join `cb_cuenta` on(((`cb_trans_detail`.`codigo_cuenta` = `cb_cuenta`.`codigo_cuenta`) and (`cb_trans_detail`.`categoria` = `cb_cuenta`.`categoria`) and (`cb_trans_detail`.`codigo_mayor` = `cb_cuenta`.`codigo_mayor`)))) join `cb_mayor` on(((`cb_cuenta`.`codigo_mayor` = `cb_mayor`.`codigo_mayor`) and (`cb_cuenta`.`categoria` = `cb_mayor`.`categoria`)))) join `cb_categoria` on(((`cb_mayor`.`categoria` = `cb_categoria`.`categoria`) and (`cb_trans_detail`.`categoria` = `cb_categoria`.`categoria`)))) left join `cb_trans_head` on((`cb_trans_detail`.`numero_partida` = `cb_trans_head`.`numero_partida`)))
where
(`cb_categoria`.`presentacion` = '1') and `cb_trans_head`.`fecha_partida` BETWEEN '2014-01-01' and '2014-01-31'
group by
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`)
My question is: How to get the result I need using the view and filtering programatically instead of hard-coding the where condition? Thank you. If you need the individual table definitions let me know. Much appreciated.
If you use a left join to a table X and a condition in the WHERE-clause to this table X, you change your left join to an inner one. If you want to restrict the results by the values of this left joined table, you must use this condition in the ON clause of the left join instead:
...
LEFT JOIN
cb_trans_head
ON
cb_trans_detail.numero_partida = cb_trans_head.numero_partida
AND
cb_trans_head.fecha_partida BETWEEN '2014-01-01' and '2014-01-31'
...
If there's another one that I overlook, tread it the same way.
I have created a table and need to update the entries by comparing the data from the view that I have created in SQL server
This action need to performed by a button click in MS Access forms
I have written the following query in MS Access - Query. Exported the view totaltemp1 from sql server.
update P_Payroll as p
inner join totaltemp1 as tp on p.emp_id = tp.emp_id
inner join P_Student_Supervisor as ss on tp.emp_id = ss.emp_id
set Number_of_Hours = tp.temp_drop_hours,
Total_Payment = tp.temp_drop_hours*ss.emp_hourlywage
where p.emp_id = tp.emp_id and ss.emp_id = p.emp_id;
Previously I got this working correctly in SQL server in stand alone mode. Code for that as follows.
update P_Payroll set Number_of_Hours = tp.temp_drop_hours, Total_Payment = tp.temp_drop_hours*ss.emp_hourlywage
from totaltemp1 as tp, P_Payroll as p, P_Student_Supervisor as ss
where p.emp_id = tp.emp_id and ss.emp_id = p.emp_id;
I have checked the data structure too. I am just getting an error
"Syntax error (missing operator) in query expression 'p.emp_id = tp.emp_id inner join P_Student_Supervisor as ss on tp.emp_id = ss.emp_i"
Thanks for your time
Pk.
Two things you can check... first, remove the WHERE clause - you're already specifying those conditions in the JOIN statements.
Then, put brackets around your joins...
UPDATE (P_Payroll AS p
INNER JOIN totaltemp1 AS tp on p.emp_id = tp.emp_id)
INNER JOIN P_Student_Supervisor AS ss on tp.emp_id = ss.emp_id
SET p.Number_of_Hours = tp.temp_drop_hours,
p.Total_Payment = tp.temp_drop_hours * ss.emp_hourlywage
Access like brackets...
If anyone can help me rewrite my query to work in mysql 5 I would be very grateful. If anyone can provide links to solid, simple tutorials on how to rewrite old queries that would also be great.
My current (version 4) query looks like this:
SELECT
course.course_code,
course.course_title_sv AS course_title,
course.course_u_credits,
course.course_successive_level_scb_id,
s.successive_level_scb_order,
s.successive_level_scb_code,
LEFT (education_level.edu_level_name_sv, 1) AS course_edu_level, course.course_level,
GROUP_CONCAT(DISTINCT h.head_area_hv_title_sv SEPARATOR ', ') AS head_area_hv
FROM
course, course_event, course_event_package_links, package, education_level
LEFT JOIN
course_has_head_area_hv ON(course.course_id = course_has_head_area_hv.course_id)
LEFT JOIN
head_area_hv h ON(h.head_area_hv_id = course_has_head_area_hv.head_area_hv_id)
LEFT JOIN
successive_level_scb s ON(s.successive_level_scb_id = course.course_successive_level_scb_id)
WHERE
course.course_edu_level=education_level.edu_level_id AND
course.course_id=course_event.course_id AND
course_event.course_event_id=course_event_package_links.course_event_id AND
course_event_package_links.package_id=package.package_id AND
course.course_successive_level_scb_id != '' AND
package.package_id='6318'
GROUP BY course.course_id
Simply replace
SELECT ...
FROM course, course_event, course_event_package_links
...
WHERE course.course_id=course_event.course_id
AND course_event.course_event_id=course_event_package_links.course_event_id
by
SELECT ...
FROM course
JOIN course_event ON course_event.course_id = course.course_id
JOIN course_event_package_links
ON course_event_package_links.course_event_id = course_event.course_event_id
...
About your error message, are you sure course.course_id exists? Maybe it was replaced by course.course_code?
I have had a read through a few of the other issues around joining on SQL Updates but haven't been able to finalise a query.
System is all in MySQL (INNODB table structure)
We are wanting to update an amount in one table that will increase an amount based on 2 variables from another table. There are a few constraints that need to be checked in the update to make sure the variables from the second table match the keys in the table to be updated
UPDATE as1
SET as1.amount = as1.amount + (b1.workers * b1.level)
FROM account_stock AS as1
INNER JOIN building AS b1 ON as1.accountID = b1.accountID
INNER JOIN building_seed AS bs1 ON bs1.buildingID = b1.buildingID
WHERE bs1.stockID = as1.stockID
AND b1.accountID = as1.accountID
AND b1.locID = as1.locID
AND b1.status = active
AND b1.gTime > 0
It's getting an error and I can't pick it. Sorry if it is a simple question, all my SQL is self taught so some of the habits I have aren't very good!
MySQL syntax for UPDATE is different. There is no FROM:
UPDATE
account_stock AS as1
INNER JOIN building AS b1 ON as1.accountID = b1.accountID
INNER JOIN building_seed AS bs1 ON bs1.buildingID = b1.buildingID
SET as1.amount = as1.amount + (b1.workers * b1.level)
WHERE bs1.stockID = as1.stockID
AND b1.locID = as1.locID
AND b1.status = active
AND b1.gTime > 0 ;
--- removed duplicate :
--- AND b1.accountID = as1.accountID
Also: is active a column or you meant to write?: AND b1.status = 'active'