I have a complex access sql query to transform in linq
This is what I do :
var query = from p in tbl_multiIntervenants.AsEnumerable()
join q in
(
from s in tbl_contrats.AsEnumerable()
join t in
(
from u in tbl_traitement_gapp.AsEnumerable()
join v in tbl_TP.AsEnumerable()
on u.Field<string>("Identifiant tâche") equals v.Field<string>("Identifiant tâche") into joined
from jointe in joined.DefaultIfEmpty()
select jointe
)
)
Here the SQL query :
SELECT Tbl_TP.Société, Tbl_TP.Région, Tbl_TP.[N° Site], Tbl_TP.[Nom Site],
Tbl_TP.[Identifiant intervention], Tbl_TP.[Gamme intervention],
Tbl_TP.[Description de l'intervention], Tbl_TP.[Identifiant tâche], Tbl_TP.Description,
Tbl_TP.[Etat administratif intervention], Tbl_TP.[Date début prévu tâche], Tbl_TP.[Date fin prévu tâche], Tbl_TP.Detrompeur,
Tbl_TP.[Entité demandeur], Tbl_TP.[Infos Demandeur], Tbl_TP.[Indicateur accompagnement], Tbl_TP.[Code Journalier], Tbl_TP.Coupure,
Tbl_TP.[Validé ?], Tbl_TP.[Accompagnement ?], Tbl_TP.[Observation ?], Tbl_TP.Accès, Tbl_TP.[Contrainte de sécurité], Tbl_TP.V_Couleur,
Tbl_TP.[Caractéristiques de l'intervention], Tbl_CONTRATS.[Sensibilité du site], Tbl_CONTRATS.[Approbation automatique AS], Tbl_TP.[Chef de projet SFR], Tbl_TP.[Date création intervention (hs)],
Tbl_TP.[Identifiant demande], Tbl_TP.PlanPrevention, Tbl_CONTRATS.Appartenance
FROM Tbl_Multi_Intervenants
INNER JOIN (Tbl_CONTRATS
INNER JOIN (Tbl_TP LEFT JOIN Tbl_Traitement_GAPP ON Tbl_TP.[Identifiant tâche] = Tbl_Traitement_GAPP.[Identifiant tâche])
ON Tbl_CONTRATS.Emplacement = Tbl_TP.[N° Site])
ON Tbl_Multi_Intervenants.[Identifiant tâche] = Tbl_TP.[Identifiant tâche]
WHERE (((Tbl_Traitement_GAPP.[Identifiant tâche]) Is Null))
GROUP BY Tbl_TP.Société, Tbl_TP.Région, Tbl_TP.[N° Site], Tbl_TP.[Nom Site], Tbl_TP.[Identifiant intervention], Tbl_TP.[Gamme intervention], Tbl_TP.[Description de l'intervention],
Tbl_TP.[Identifiant tâche], Tbl_TP.Description, Tbl_TP.[Etat administratif intervention], Tbl_TP.[Date début prévu tâche], Tbl_TP.[Date fin prévu tâche], Tbl_TP.Detrompeur,
Tbl_TP.[Entité demandeur], Tbl_TP.[Infos Demandeur], Tbl_TP.[Indicateur accompagnement], Tbl_TP.[Code Journalier], Tbl_TP.Coupure, Tbl_TP.[Validé ?],
Tbl_TP.[Accompagnement ?], Tbl_TP.[Observation ?], Tbl_TP.Accès, Tbl_TP.[Contrainte de sécurité], Tbl_TP.V_Couleur,
Tbl_TP.[Caractéristiques de l'intervention], Tbl_CONTRATS.[Sensibilité du site], Tbl_CONTRATS.[Approbation automatique AS], Tbl_TP.[Chef de projet SFR],
Tbl_TP.[Date création intervention (hs)], Tbl_TP.[Identifiant demande], Tbl_TP.PlanPrevention, Tbl_CONTRATS.Appartenance;
To simplify here is the sql query :
SELECT Tbl_TP.Société, Tbl_CONTRATS.Appartenance
FROM Tbl_Multi_Intervenants
INNER JOIN (Tbl_CONTRATS
INNER JOIN (Tbl_TP LEFT JOIN Tbl_Traitement_GAPP ON Tbl_TP.Identifiant tâche] = Tbl_Traitement_GAPP.[Identifiant tâche])
ON Tbl_CONTRATS.Emplacement = Tbl_TP.[N° Site])
ON Tbl_Multi_Intervenants.[Identifiant tâche] = Tbl_TP.[Identifiant tâche]
WHERE (((Tbl_Traitement_GAPP.[Identifiant tâche]) Is Null))
Related
thanks for reading it!
I am extracting data from a Json with operators in SQL, this is the code:
select lc.name as company,
lr.name as retailer,
ls.name as tienda,
sm.created_on as date,
sm.task_id as task_id,
ss.submission_data::json-> '¿Qué ''bancos'' se encuentran cerca de la tienda? De ser necesario, pregunta al gerente de tienda' AS banco,
ss.submission_data::json->> 'Indica la distancia aproximada entre la tienda y el ''banco'' más cercano' AS banco_distancia,
ss.submission_data::json->> '¿En la zona hay disponibilidad de ''transporte público''? Indica los tipos de transporte' AS transporte,
ss.submission_data::json->> 'Indica el precio estimado de un viaje en ''transporte público''' AS transporte_precio,
ss.submission_data::json->> '¿En la zona hay servicio de ''aplicaciones de envíos''?' AS app_envio
from submission_submissionmetadata sm
left join submission_submission ss on sm.submission_id = ss.id
left join location_store ls on ls.id = sm.store_id
left join location_retailer lr on lr.id = ls.retailer_id
left join location_company lc on lc.id = lr.company_id
where sm.brand_id = 293
order by date desc
In The results I get the columns bank, transport and app_ship come as in a list type, I have tried to use functions to flatten this but I have not been successful. Do you know what I can do?
https://www.postgresql.org/docs/current/functions-json.html
Waiting on your response to comment above, but you can see all the ways to extract json in the above doc. For instance,if you want only the first element of the app_envio array, it would be:
ss.submission_data::json #>> '{'¿En la zona hay servicio de ''aplicaciones de envíos''?',0}' AS app_envio
I have to do a recursive function in pSQL to get the following query:
I have a table called tb_route with from_city and to_city
I have another column with the distance in km between different cities.
The table is builded recursively. I have to make a recursive CTE to show the route between two cities (i.e., from 'Santiago de compostela' to 'Saint Jean Pied de Port') showing the total km of the route and the cities where it goes through.
The output has to be something like this:
This is what I've tried:
WITH RECURSIVE cities AS (
SELECT *
FROM textil.tb_route
WHERE to_city_name = 'Santigo de Compostela'
UNION ALL
SELECT e.from_city, e.to_city, e.route, e.km
FROM textil.tb_route e
INNER JOIN cities tb_route ON tb_route.from_city_name = e.from_city
)
SELECT *
FROM cities;
And I had an error like:
ERROR: column e.from_city does not exist
LINE 8: ...JOIN cities tb_route ON tb_route.from_city_name = e.from_cit...
Table:
CREATE TABLE textil.tb_route
(
from_city_name CHARACTER VARYING(120) NOT NULL ,
to_city_name CHARACTER VARYING(120) NOT NULL ,
km_distance_num NUMERIC(5,2) NOT NULL ,
CONSTRAINT pk_route PRIMARY KEY (from_city_name, to_city_name)
);
Data:
INSERT INTO textil.tb_route VALUES
('Saint Jean Pied de Port','Roncesvalles',25.7),
('Somport','Jaca',30.5),
('Roncesvalles','Zubiri',21.5),
('Jaca','Arrés',25),
('Zubiri','Pamplona/Iruña',20.4),
('Arrés','Ruesta',28.7),
('Pamplona/Iruña','Puente la Reina/Gares',24),
('Ruesta','Sangüesa',21.8),
('Puente la Reina/Gares','Estella/Lizarra',22),
('Sangüesa','Monreal',27.25),
('Estella/Lizarra','Torres del Río',29),
('Monreal','Puente la Reina/Gares',31.1),
('Torres del Río','Logroño',20),
('Logroño','Nájera',29.6),
('Nájera','Santo Domingo de la Calzada',21),
('Santo Domingo de la Calzada','Belorado',22.7),
('Belorado','Agés',27.4),
('Agés','Burgos',23),
('Burgos','Hontanas',31.1),
('Hontanas','Boadilla del Camino',28.5),
('Boadilla del Camino','Carrión de los Condes',24.6),
('Carrión de los Condes','Terradillos de los Templarios',26.6),
('Terradillos de los Templarios','El Burgo Ranero',30.6),
('El Burgo Ranero','León',37.1),
('León','San Martín del Camino',25.9),
('San Martín del Camino','Astorga',24.2),
('Astorga','Foncebadón',25.9),
('Foncebadón','Ponferrada',27.3),
('Ponferrada','Villafranca del Bierzo',24.1),
('Villafranca del Bierzo','O Cebreiro',28.4),
('O Cebreiro','Triacastela',21.1),
('Triacastela','Sarria',18.3),
('Sarria','Portomarín',22.4),
('Portomarín','Palas de Rei',25),
('Palas de Rei','Arzúa',28.8),
('Arzúa','Pedrouzo',19.1),
('Pedrouzo','Santiago de Compostela',20),
('Bayona','Ustaritz',14.3),
('Ustaritz','Urdax',21.2),
('Urdax','Elizondo',18.8),
('Elizondo','Berroeta',9.7),
('Berroeta','Olagüe',20.4),
('Olagüe','Pamplona/Iruña',25),
('Irún','Hernani',26.6),
('Hernani','Tolosa',18.9),
('Tolosa','Zerain',33),
('Zerain','Salvatierra/Agurain',28),
('Salvatierra/Agurain','Vitoria/Gasteiz',27.4),
('Vitoria/Gasteiz','La Puebla de Arganzón',18.5),
('La Puebla de Arganzón','Haro',31),
('Haro','Santo Domingo de la Calzada',20),
('Bayona','Irún',33.8),
('Tolosa','Zegama',37.9),
('Zegama','Salvatierra/Agurain',20.1),
('La Puebla de Arganzón','Miranda del Ebro',22.3),
('Miranda del Ebro','Pancorbo',16.7),
('Pancorbo','Briviesca',23.4),
('Briviesca','Monasterio de Rodilla',19.8),
('Monasterio de Rodilla','Burgos',28.5);
```
Here I leave the solution I've get finally:
with recursive caminos(from_city_name, to_city_name, path, total_distance, terminar, ultima_ciudad) as (
-- Consulta base
select to_city_name
,'Saint Jean Pied de Port' -- Cambiar Destino
,concat(to_city_name, concat(' -> ', from_city_name))
,cast(km_distance_num as numeric(8,2))
,0 --No terminar
,from_city_name
from textil.tb_route
where to_city_name = 'Santiago de Compostela' -- Cambiar Origen
union all
-- Consulta recursiva
select caminos.from_city_name
,caminos.to_city_name
,concat(caminos.path, concat( ' -> ', tr.from_city_name))
,cast(caminos.total_distance + tr.km_distance_num as numeric(8,2))
,case when tr.from_city_name = caminos.to_city_name then 1 else 0 end
,tr.from_city_name
from caminos inner join textil.tb_route tr on tr.to_city_name = caminos.ultima_ciudad and caminos.terminar != 1
)
select from_city_name, to_city_name, path, total_distance
from caminos
where 1 = 1
and from_city_name = 'Santiago de Compostela' --Cambiar Origen
and ultima_ciudad = 'Saint Jean Pied de Port' -- Cambiar Destino
;
I understand your question as a graph-walking problem. As described in your questions, edges are directed (meaning that you can travel from from_city_name to to_city_name, but not the other way around).
Here is an approach using a recursive CTE. The idea is to start from a given city, and then follow all possible routes, while keeping track of the overall travel path in an arry. The recursion stops either when a circle is detected, or when the target city is reached. Then, the outer query filters on the successful paths (there may be none, one or several).
with recursive cte as (
select
from_city_name,
to_city_name,
km_distance_num,
array[from_city_name::text, to_city_name::text] path
from tb_route
where from_city_name = 'Saint Jean Pied de Port'
union all
select
r.from_city_name,
r.to_city_name,
c.km_distance_num + r.km_distance_num,
c.path || r.to_city_name::text
from tb_route r
inner join cte c on c.to_city_name = r.from_city_name
where
not r.to_city_name = any(c.path)
and c.from_city_name <> 'Santiago de Compostela'
)
select
path[1] from_city_name,
to_city_name,
km_distance_num,
array_to_string(path, ' > ') path
from cte
where to_city_name = 'Santiago de Compostela';
Demo on DB Fiddle:
from_city_name | to_city_name | km_distance_num | path
:---------------------- | :--------------------- | --------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Saint Jean Pied de Port | Santiago de Compostela | 775.3 | Saint Jean Pied de Port > Roncesvalles > Zubiri > Pamplona/Iruña > Puente la Reina/Gares > Estella/Lizarra > Torres del Río > Logroño > Nájera > Santo Domingo de la Calzada > Belorado > Agés > Burgos > Hontanas > Boadilla del Camino > Carrión de los Condes > Terradillos de los Templarios > El Burgo Ranero > León > San Martín del Camino > Astorga > Foncebadón > Ponferrada > Villafranca del Bierzo > O Cebreiro > Triacastela > Sarria > Portomarín > Palas de Rei > Arzúa > Pedrouzo > Santiago de Compostela
I wrote my first CLIPS code for a school project but I am not familiar with CLIPS (I use C# and python as main languages).
This is my code and the errors I am gettin:
(defrule determine-closing-date
(not (day-to-close ?))
(billing-size ?)
(unpaid-invoices-number ?)
=>
(if
(or
(< billing-size 1000000)
(< unpaid-invoices-number 1000000)
)
then (assert (day-to-close MtTh))
else (assert (day-to-close friday))
)
(defrule determine-billing-size ""
(not (billing-size ?))
(not (day-to-close ?))
=>
(printout t "¿Cuál es el tamaño de la facturacion?")
(assert (billing-size ?size (read))))
(defrule determine-unpaid-invoices-number ""
(not (unpaid-invoices-number ?))
(not (day-to-close ?))
=>
(printout t "¿Cuál es la cantidad de facturas no pagadas")
(assert (unpaid-invoices-number ?size (read))))
(defrule determine-friday-load ""
(day-to-close friday)
(not (friday-load ?))
=>
(printout t "¿Cuál es la carga de cierres para el viernes?")
(assert (friday-load ?load (read))))
(defrule determine-saturday-closing ""
(day-to-close friday)
(not(< friday-load 1000000))
=>
(assert (day-to-close saturday)))
(defrule day-to-close-conclulssion ""
(day-to-close ?)
=>
(if (eq day-to-close MtTh)
then (printout t "Se puede cerrar de Lunes a Jueves")
else (
if (eq day-to-close friday)
then (printout t "Se debe cerrar viernes.")
else (printout t "Se debe cerrar sabado.")
)
))
The errores are:
[ARGACCES5] Function < expected argument #1 to be of type integer or float
[PRCCODE3] Undefined variable size referenced in RHS of defrule.
[CSTRCPSR1] WARNING: Redefining defrule: determine-saturday-closing +j+j+j
[CSTRCPSR1] WARNING: Redefining defrule: day-to-close-conclulssion +j+j
Some suggest revisions:
(defrule determine-closing-date
(not (day-to-close ?))
(billing-size ?billing-size)
(unpaid-invoices-number ?unpaid-invoices-number)
=>
(if (or (< ?billing-size 1000000)
(< ?unpaid-invoices-number 1000000))
then (assert (day-to-close MtTh))
else (assert (day-to-close friday))))
(defrule determine-billing-size ""
(not (billing-size ?))
(not (day-to-close ?))
=>
; What is the size of the billing?
(printout t "¿Cuál es el tamaño de la facturacion? ")
(bind ?size (read))
(assert (billing-size ?size)))
(defrule determine-unpaid-invoices-number ""
(not (unpaid-invoices-number ?))
(not (day-to-close ?))
=>
; What is the amount of unpaid bills?
(printout t "¿Cuál es la cantidad de facturas no pagadas? ")
(bind ?size (read))
(assert (unpaid-invoices-number ?size)))
(defrule determine-friday-load ""
(day-to-close friday)
(not (friday-load ?))
=>
; What is the burden of closures for Friday?
(printout t "¿Cuál es la carga de cierres para el viernes? ")
(bind ?load (read))
(assert (friday-load ?load)))
(defrule determine-saturday-closing ""
?dtc <- (day-to-close friday)
(friday-load ?load&:(< ?load 1000000))
=>
(retract ?dtc)
(assert (day-to-close saturday)))
(defrule day-to-close-conclusion ""
(declare (salience -10))
(day-to-close ?day-to-close)
=>
(switch ?day-to-close
(case MtTh
; Can be closed from Monday to Thursday.
then (printout t "Se puede cerrar de Lunes a Jueves." crlf))
(case friday
; Must be closed Fridays.
then (printout t "Se debe cerrar viernes." crlf))
(default
; Must be closed Saturday.
then (printout t "Se debe cerrar sabado." crlf))))
And the output it produces:
CLIPS> Loading Buffer...
******
CLIPS> (reset)
CLIPS> (run)
¿Cuál es el tamaño de la facturacion? 10
¿Cuál es la cantidad de facturas no pagadas? 10
Se puede cerrar de Lunes a Jueves.
CLIPS> (reset)
CLIPS> (run)
¿Cuál es el tamaño de la facturacion? 3000000
¿Cuál es la cantidad de facturas no pagadas? 3000000
¿Cuál es la carga de cierres para el viernes? 10
Se debe cerrar sabado.
CLIPS> (reset)
CLIPS> (run)
¿Cuál es el tamaño de la facturacion? 3000000
¿Cuál es la cantidad de facturas no pagadas? 3000000
¿Cuál es la carga de cierres para el viernes? 3000000
Se debe cerrar viernes.
CLIPS>
I'm getting the error:
syntax error (missing operator) in query expression '((dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id) AND (dbo_tblSale.sale_id IN (319))) LEFT JOIN
dbo_tblItem ON ((dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id) AND (dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number)'.
I've had this issue before and know that it doesn't like my parenthesis setup, but nothing I do seems to make MS Access happy.
SELECT dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2,
dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1,
dbo_tblMailList.mail_Comp_GenAm, dbo_tblBidder.bidder_number, SUM(dbo_tblItem.item_pr) AS SumOfitem_pr, SUM(dbo_tblItem.item_premium)
AS SumOfitem_premium, dbo_tblBidder.bidder_type, dbo_tblSale.sale_id
FROM (dbo_tblMailList LEFT JOIN
dbo_tblBidder ON ((dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id) AND (dbo_tblBidder.bidder_sale_id IN (319)))) LEFT JOIN
dbo_tblSale ON ((dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id) AND (dbo_tblSale.sale_id IN (319))) LEFT JOIN
dbo_tblItem ON ((dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id) AND (dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number))
GROUP BY dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2,
dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1,
dbo_tblMailList.mail_Comp_GenAm, dbo_tblBidder.bidder_number, dbo_tblBidder.bidder_type, dbo_tblMailList.mail_Comp_Art,
dbo_tblMailList.mail_Comp_IndArt, dbo_tblMailList.mail_Comp_Fire, dbo_tblMailList.mail_Comp_Ceramic, dbo_tblSale.sale_id
HAVING (dbo_tblMailList.mail_Comp_GenAm = 1)
If anyone has any ideas, please let me know.
Thanks,
James
EDIT
Using Gareth's FROM clause below I now have:
SELECT dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2,
dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1,
dbo_tblMailList.mail_Comp_GenAm, dbo_tblBidder.bidder_number, SUM(dbo_tblItem.item_pr) AS SumOfitem_pr, SUM(dbo_tblItem.item_premium)
AS SumOfitem_premium, dbo_tblBidder.bidder_type, dbo_tblSale.sale_id
FROM (
( dbo_tblMailList
LEFT JOIN dbo_tblBidder
ON dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id
AND dbo_tblBidder.bidder_sale_id IN (319)
)
LEFT JOIN dbo_tblSale
ON dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id
)
LEFT JOIN dbo_tblItem
ON dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id
AND dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number
GROUP BY dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2,
dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1,
dbo_tblMailList.mail_Comp_GenAm, dbo_tblBidder.bidder_number, dbo_tblBidder.bidder_type, dbo_tblMailList.mail_Comp_Art,
dbo_tblMailList.mail_Comp_IndArt, dbo_tblMailList.mail_Comp_Fire, dbo_tblMailList.mail_Comp_Ceramic, dbo_tblSale.sale_id
HAVING (dbo_tblMailList.mail_Comp_GenAm = 1)
But I am getting the error Extra ) in query expression ''.
I think it may have something to do with AND dbo_tblBidder.bidder_sale_id IN (319). If I change this to AND dbo_tblBidder.bidder_sale_id = 319, I get the error:
JOIN expression not supported.
And it highlights the dbo_tblBidder.bidder_sale_id = 319.
CONCLUSION
Here is my final working code:
SELECT mail_ID, mail_FirstName, mail_LastName, mail_Address1, mail_Address2,
mail_City, mail_State, mail_Zip, mail_Phone1, mail_Email1,
mail_Comp_GenAm, SUM(SumOfitem_pr) AS SumOfitem_price, SUM(SumOfitem_premium) AS SumOfitem_premiums
FROM (
SELECT dbo_tblMailList.mail_ID, dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2,
dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1,
dbo_tblMailList.mail_Comp_GenAm, SUM(dbo_tblItem.item_pr) AS SumOfitem_pr, SUM(dbo_tblItem.item_premium)
AS SumOfitem_premium
FROM (
( dbo_tblMailList
LEFT JOIN dbo_tblBidder
ON dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id
)
LEFT JOIN dbo_tblSale
ON dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id
)
LEFT JOIN dbo_tblItem
ON dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id
AND dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number
WHERE dbo_tblMailList.mail_Comp_GenAm = 1 AND dbo_tblBidder.bidder_sale_id IN (319)
GROUP BY dbo_tblMailList.mail_ID, dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2,
dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1,
dbo_tblMailList.mail_Comp_GenAm, dbo_tblMailList.mail_Comp_Art,
dbo_tblMailList.mail_Comp_IndArt, dbo_tblMailList.mail_Comp_Fire, dbo_tblMailList.mail_Comp_Ceramic
UNION
SELECT dbo_tblMailList.mail_ID, dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2,
dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1,
dbo_tblMailList.mail_Comp_GenAm, 0 AS SumOfitem_pr, 0
AS SumOfitem_premium
FROM (
( dbo_tblMailList
LEFT JOIN dbo_tblBidder
ON dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id
)
LEFT JOIN dbo_tblSale
ON dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id
)
LEFT JOIN dbo_tblItem
ON dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id
AND dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number
WHERE dbo_tblMailList.mail_Comp_GenAm = 1
GROUP BY dbo_tblMailList.mail_ID, dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2,
dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1,
dbo_tblMailList.mail_Comp_GenAm, dbo_tblMailList.mail_Comp_Art,
dbo_tblMailList.mail_Comp_IndArt, dbo_tblMailList.mail_Comp_Fire, dbo_tblMailList.mail_Comp_Ceramic
)
GROUP BY mail_ID, mail_FirstName, mail_LastName, mail_Address1, mail_Address2,
mail_City, mail_State, mail_Zip, mail_Phone1, mail_Email1,
mail_Comp_GenAm, SumOfitem_price, SumOfitem_premiums
Thanks Gareth!
In access you cannot have multiple joins without separating them with parentheses, i.e.
SELECT *
FROM A
INNER JOIN B
ON A.ID = B.AID
INNER JOIN C
ON B.ID = C.BID;
Is not valid, it would need to be:
SELECT *
FROM (A
INNER JOIN B
ON A.ID = B.AID)
INNER JOIN C
ON B.ID = C.BID;
So, your from clause would need to be:
FROM (
( dbo_tblMailList
LEFT JOIN dbo_tblBidder
ON dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id
AND dbo_tblBidder.bidder_sale_id IN (319)
)
LEFT JOIN dbo_tblSale
ON dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id)
AND dbo_tblSale.sale_id IN (319)
)
LEFT JOIN dbo_tblItem
ON dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id
AND dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number
N.B I have removed all the unnecessary parentheses from the joins to reduce the clutter in the query (it is not necessary to enclose every predicate in parentheses), and exaggerated the tab indentations to show clearly where parentheses open and close
EDIT
I forgot, you cannot apply the constant expression in the JOIN clause in Access, you would need to create a subselect,
dbo_tblMailList
LEFT JOIN dbo_tblBidder
ON dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id
AND dbo_tblBidder.bidder_sale_id IN (319)
You would need to do
dbo_tblMailList AS m
LEFT JOIN (SELECT * FROM dbo_tblBidder WHERE bidder_sale_id IN (319)) AS b
ON m.mail_ID = b.bidder_mail_id
So your full query would be:
SELECT m.mail_FirstName,
m.mail_LastName,
m.mail_Address1,
m.mail_Address2,
m.mail_City,
m.mail_State,
m.mail_Zip,
m.mail_Phone1,
m.mail_Email1,
m.mail_Comp_GenAm,
b.bidder_number,
SUM(i.item_pr) AS SumOfitem_pr,
SUM(i.item_premium) AS SumOfitem_premium,
b.bidder_type,
s.sale_id
FROM (
( dbo_tblMailList AS m
LEFT JOIN
( SELECT bidder_mail_id , bidder_number, bidder_type, bidder_sale_id
FROM dbo_tblBidder
WHERE bidder_sale_id IN (319)
) AS b
ON m.mail_ID = b.bidder_mail_id
)
LEFT JOIN dbo_tblSale AS s
ON b.bidder_sale_id = s.sale_id
)
LEFT JOIN dbo_tblItem AS i
ON b.bidder_sale_id = i.item_sale_id
AND b.bidder_number = i.item_bidder_number
GROUP BY
m.mail_FirstName, m.mail_LastName, m.mail_Address1, m.mail_Address2, m.mail_City, m.mail_State,
m.mail_Zip, m.mail_Phone1, m.mail_Email1, m.mail_Comp_GenAm, b.bidder_number, b.bidder_type, s.sale_id;
(I have used short table aliases to try and condense the code, not a convention you have to follow)
i have a database that I have populated.
I am now trying to write a SELECT statement:
SELECT + FULL ( fst) full(fs) COUNT(DISTINCT r1.CLIENT_ID || e.CODE || fst.TAB_ID) NB_TAB FROM RATTACHEMENT_DART rd, FLUX_SORTANT fs, FS_TABLEAU fst, equipement e, REFERENTIEL r1, referentiel r2, referentiel pere;
When I try to run the above case statement I recieve the following error:
Erreur commençant à la ligne 4 de la commande : SELECT + FULL ( fst)
full(fs) COUNT(DISTINCT r1.CLIENT_ID || e.CODE || fst.TAB_ID) NB_TAB
FROM RATTACHEMENT_DART rd, FLUX_SORTANT fs, FS_TABLEAU fst, equipement
e, REFERENTIEL r1, referentiel r2, referentiel pere Erreur à la ligne
de commande : 4, colonne : 26 Rapport d'erreur : Erreur SQL :
ORA-00923: mot-clé FROM absent à l'emplacement prévu
00923. 00000 - "FROM keyword not found where expected"
*Cause:
*Action:
If you want to use hints you should use /*+...*/ as below
SELECT /*+ FULL( fst) full(fs)*/ COUNT(DISTINCT r1.CLIENT_ID || e.CODE || fst.TAB_ID) NB_TAB
FROM RATTACHEMENT_DART rd, FLUX_SORTANT fs,
FS_TABLEAU fst, equipement e,
REFERENTIEL r1, referentiel r2, referentiel pere;
See documentation