join on its own in Jet - sql

I am joining a query with it self and my code is working ok on ase isql. However when I want to use it in access 2007 I get the following error "The derived table expression is missing a correlation name. Check derived table syntax in the reference manual"
The original code does something like this:
select TT.name, TT.lastname, max(amount) as maxsalecurrentweek
from sales
inner join
(select s1.id_employee, e.name, e.lastname, e.address, e.age, e.id_employee
from employee e
join sales s1 on e.id_emplyoee = s1.id_employee
where
some conditions here) as TT
on sales.id_employee = TT.id_employee
group by
TT.name, TT.lastname
In the original code I join more tables in the inner query as well as some where conditions. But the above code should illustrate what I do.
It looks like the way I join the table with it self is the problem in access. Does anyone know what How is the correct sysntaxis? Or if access/JET/ACE support this inner join with it self approach?
Here is the original code:
select max(tort140.BEL_GRLAG_AP) as MaxPensjonGr, TT.IDE_KUNDE_PRSNR,
TT.DAT_KUNDE_FOEDT_NUM, TT.AvtaleID, TT.Orgnr, TT.Arbeidsgiver,
TT.Sivilstatus, TT.Polisestatus, TT.Årslønn from tort140
inner join
(select distinct tort128.NUM_AVTALE_ID as AvtaleID,
tort009.IDE_ARBGIV_NR as Orgnr,
tort134.NVN_ARBGIV as Arbeidsgiver,
tort127.DAT_KUNDE_FOEDT_NUM as DAT_KUNDE_FOEDT_NUM,
tort127.IDE_KUNDE_PRSNR as IDE_KUNDE_PRSNR,
tort001.STA_SIVILSTATUS as Sivilstatus,
tort128.typ_status as Polisestatus,
tort128.rte_polisegrad as Polisegrad,
tort140.BEL_LOENN_AAR as Årslønn,
tort138.IDE_SEKV_TORT138"
from tort140 left join (tort138 join (tort128 join (tort134 join (tort009 join (tort001 join tort127
on tort127.DAT_KUNDE_FOEDT_NUM=tort001.DAT_KUNDE_FOEDT_NUM and tort127.IDE_KUNDE_PRSNR=tort001.IDE_KUNDE_PRSNR)
on tort127.DAT_KUNDE_FOEDT_NUM=tort009.DAT_KUNDE_FOEDT_NUM and tort127.IDE_KUNDE_PRSNR=tort009.IDE_KUNDE_PRSNR)
on tort009.IDE_ARBGIV_NR=tort134.IDE_ARBGIV_NR)
on tort128.IDE_SEKV_TORT127 = tort127.IDE_SEKV_TORT127)
on tort128.IDE_SEKV_TORT128 = tort138.IDE_SEKV_TORT128)
on tort140.IDE_SEKV_TORT138 = tort138.IDE_SEKV_TORT138)
where tort128.NUM_AVTALE_ID = '102356' and tort128.DAT_GYLDIG_FOM <= 20120101
and (tort128.DAT_GYLDIG_TOM >= 19520000 or tort128.DAT_GYLDIG_TOM is null
and tort128.DAT_HISTORISK is null and tort128.TYP_STATUS= 'akt' and tort127.DAT_KUNDE_FOEDT_NUM >= 19650000
and tort127.DAT_KUNDE_FOEDT_NUM <= 19550000 and tort127.DAT_TERMINERT is null and tort127.DAT_REGISTRERT<= 19550000
and tort009.DAT_SLUTT is null and tort134.DAT_HISTORISK is null
and tort138.DAT_AKSJON=(select max(p.DAT_AKSJON) from tort138 p where 1=1 and p.IDE_SEKV_TORT128=tort128.IDE_SEKV_TORT128)
) as TT
on TT.IDE_SEKV_TORT138 = tort140.IDE_SEKV_TORT138
Group by TT.IDE_KUNDE_PRSNR, TT.DAT_KUNDE_FOEDT_NUM, TT.AvtaleID, TT.Orgnr,
TT.Arbeidsgiver, TT.Sivilstatus, TT.Polisestatus, TT.Årslønn from tort140
The inner query works on access 2007 without any problem. I got the error message when I write the inner join.
Do you think this query can be suited into access?? Am I missing some brakets or something?

You have to add a join type in MS Access, Join on its own will not work.
from employee e
INNER join sales s1 on e.id_emplyoee = s1.id_employee
Edit per comments, the line above is taken from the posted SQL and fits back in like so:
select TT.name, TT.lastname, max(amount) as maxsalecurrentweek
from sales
inner join
(select s1.id_employee, e.name, e.lastname, e.address, e.age, e.id_employee
from employee e
INNER join sales s1 on e.id_emplyoee = s1.id_employee
where
some conditions here) as TT
on sales.id_employee = TT.id_employee
group by
TT.name, TT.lastname

Related

Why is SQL not letting me access information in inner queries?

I'm writing a query to find solve the following question:
"For those customer – product combinations where the product belongs to one of the product lines that have ‘Ethernet’ in their name, list the name of the customer, name of the product, the sales last year and total sales year to date."
Now, I have four tables that I need to use to solve this: xproduct, xprodline, and xsales, and xcustomer. These are related in the following ways:
And they have the following columns:
Since xcustomer and xproduct are not directly related, I'm using xsales to join them, but I'm having issues accessing the information I get from inner queries. This is the code I have so far, but it throws "ORA-00904: "S"."SALES_YEAR_TO_DATE": invalid identifier":
SELECT xcustomer.cust_name, PL.prod_name, S.sales_last_year, S.sales_year_to_date FROM xcustomer
JOIN(
SELECT xsales.sales_cust_nbr FROM xsales
JOIN (
SELECT xproduct.prod_name, xprodline.prodline_pyear_sales, xprodline.prodline_ytd_sales, xproduct.prod_nbr FROM xproduct
INNER JOIN xprodline
ON xproduct.prod_prodline = xprodline.prodline_nbr
WHERE prod_prodline= 1
) PL
ON xsales.sales_prod_nbr = PL.prod_nbr
)S
ON S.sales_cust_nbr = xcustomer.cust_nbr;
Try this:
SELECT xcustomer.cust_name, PL.prod_name, S.sales_last_year, S.sales_year_to_date FROM xcustomer
JOIN(
SELECT xsales.sales_cust_nbr, xsales.sales_last_year, xsales.sales_year_to_date FROM xsales
JOIN (
SELECT xproduct.prod_name, xprodline.prodline_pyear_sales, xprodline.prodline_ytd_sales, xproduct.prod_nbr FROM xproduct
INNER JOIN xprodline
ON xproduct.prod_prodline = xprodline.prodline_nbr
WHERE prod_prodline= 1
) PL
ON xsales.sales_prod_nbr = PL.prod_nbr
)S
ON S.sales_cust_nbr = xcustomer.cust_nbr;
You were missing selecting the columns you needed.
You are missing one more column (PROD_NAME) in your subquery. Here is the updated query:
SELECT xcustomer.cust_name,
S.prod_name,
S.sales_last_year,
S.sales_year_to_date
FROM xcustomer
JOIN(
SELECT xsales.sales_cust_nbr, xsales.sales_last_year, xsales.sales_year_to_date, pl.prod_name
FROM xsales
JOIN (
SELECT xproduct.prod_name, xprodline.prodline_pyear_sales, xprodline.prodline_ytd_sales, xproduct.prod_nbr
FROM xproduct
INNER JOIN xprodline
ON xproduct.prod_prodline = xprodline.prodline_nbr
WHERE prod_prodline= 1
) PL
ON xsales.sales_prod_nbr = PL.prod_nbr
)S
ON S.sales_cust_nbr = xcustomer.cust_nbr;
Also, I think you can directly join the tables and simplify your query as follows:
SELECT C.cust_name,
PL.prod_name,
S.sales_last_year,
S.sales_year_to_date
FROM xcustomer C
JOIN xsales S
ON C.cust_nbr = S.sales_cust_nbr
JOIN xprodline PL
ON S.sales_prod_nbr = PL.prod_nbr
JOIN xproduct P
ON PL.prodline_nbr = P.prod_prodline
WHERE P.prod_prodline= 1

PROGRESS ODBC GROUP BY CLAUSE

Im still improving my query writing skills and i have little problem with select which uses stastical function like average max etc. Firstly i wrote simple query based on single table and it works perfect:
select e.Article, avg(e.Price) as 'Average', max(e.Price) as 'Max', min(e.Price) as 'Min' from pub.E_ArtPrice e
group by e.Article
order by e.Article
Next step was to create something little more complicated to get information which is really useful for me:
Select m.Part, m.Quantity, m.OnHand, m.postingdate, m.storagearea, p.MEM_PostingCode_ID, koss.Description, ko.Carrier, avg(e.price) as 'Average', max(e.price) as 'Max', min(e.price) as 'Min'
from pub.MLL_Movements m inner join pub.MEM_PostingCode p on (m.MEM_PostingCode_Obj = p.MEM_PostingCode_Obj)
left outer join pub.s_Carrier ko on (m.CostAccObject_Obj = ko.S_Carrier_Obj)
left outer join pub.s_CostCentre kos on (m.CostAccObject_Obj = kos.S_CostCentre_Obj)
left outer join pub.S_CostCentreSpr koss on (kos.CostCentre = koss.CostCentre)
left outer join pub.E_ArtPrice e on (m.Part = e.Article)
where p.MEM_PostingCode_ID = 'AUKJ'
I didn't put the group by clause here because after many tries it didnt work.
enter image description here
All your select columns other than the aggregate columns should be in your group by clause hence try the below
Select m.Part, m.Quantity, m.OnHand, m.postingdate, m.storagearea, p.MEM_PostingCode_ID, koss.Description, ko.Carrier, avg(e.price) as 'Average', max(e.price) as 'Max', min(e.price) as 'Min'
from pub.MLL_Movements m inner join pub.MEM_PostingCode p on (m.MEM_PostingCode_Obj = p.MEM_PostingCode_Obj)
left outer join pub.s_Carrier ko on (m.CostAccObject_Obj = ko.S_Carrier_Obj)
left outer join pub.s_CostCentre kos on (m.CostAccObject_Obj = kos.S_CostCentre_Obj)
left outer join pub.S_CostCentreSpr koss on (kos.CostCentre = koss.CostCentre)
left outer join pub.E_ArtPrice e on (m.Part = e.Article)
where p.MEM_PostingCode_ID = 'AUKJ'
group by m.Part, m.Quantity, m.OnHand, m.postingdate, m.storagearea, p.MEM_PostingCode_ID, koss.Description, ko.Carrier

SQL Multiple inner joins with max() for latest recorded entry

Attempting to build SQL with INNER JOIN's. The INNER JOIN's work ok, now I need to add the MAX() function for limiting the rows to just most recent. Added this INNER JOIN client_diagnosis_record ON SELECT cr.PATID, cr.date_of_diagnosis, cr.most_recent_diagnosis...
Received this SQL code error, need some help, I'm sure it a simple oversight but my eyes are getting dim from looking so long...
Syntax error: [SQLCODE: <-4>:
SQLCODE: <-4>:<A term expected, beginning with one of the following: identifier, constant, aggregate, %ALPHAUP, %EXACT, %MVR, %SQLSTRING, %
[%msg: < The SELECT list of the subquery
SELECT pd.patient_name,
cr.PATID,
cr.date_of_diagnosis,
cr.EPISODE_NUMBER,
ce.diagnosing_clinician_value,
ce.data_entry_user_name,
most_recent_diagnosis
FROM client_diagnosis_record cr
INNER JOIN patient_current_demographics pd ON cr.patid = pd.patid
INNER JOIN client_diagnosis_entry ce ON ce.patid = pd.patid
AND cr.ID = ce.DiagnosisRecord
INNER JOIN client_diagnosis_record ON (SELECT cr.PATID,
cr.date_of_diagnosis,
cr.most_recent_diagnosis
FROM ( SELECT patid,
date_of_diagnosis,
MAX(ID) AS most_recent_diagnosis
FROM client_diagnosis_record) cr
INNER JOIN RADplus_users ru ON ru.staff_member_id = ce.diagnosing_clinician_code
WHERE cr.PATID <> '1'
AND ce.diagnosis_status_value ='Active'
AND (ru.user_description LIKE '%SOA' OR ru.user_description LIKE '%OA')
GROUP BY cr.PATID
I tried to re-format you query and it seems your query syntax is not correct. You may try below query -
SELECT pd.patient_name,
cr.PATID,
cr.date_of_diagnosis,
cr.EPISODE_NUMBER,
ce.diagnosing_clinician_value,
ce.data_entry_user_name,
most_recent_diagnosis
FROM client_diagnosis_record cr
INNER JOIN (SELECT patid,
date_of_diagnosis,
MAX(ID) AS most_recent_diagnosis
FROM client_diagnosis_record
GROUP BY patid,
date_of_diagnosis) cr2 ON cr.PATID = cr2.PATID
AND cr.date_of_diagnosis = cr2.date_of_diagnosis
AND cr.ID = cr2.most_recent_diagnosis
INNER JOIN patient_current_demographics pd ON cr.patid = pd.patid
INNER JOIN client_diagnosis_entry ce ON ce.patid = pd.patid
AND cr.ID = ce.DiagnosisRecord
INNER JOIN RADplus_users ru ON ru.staff_member_id = ce.diagnosing_clinician_code
WHERE cr.PATID <> '1'
AND ce.diagnosis_status_value ='Active'
AND (ru.user_description LIKE '%SOA' OR ru.user_description LIKE '%OA')
GROUP BY cr.PATID

Looking to add in a Count query with Group by INTO an existing working query

Goal:
I wish to get the Count of how many times a WorkItem was re-assigned
From what I understand the proper query is the following:
SELECT
WorkItemDimvw.Id,
COUNT(WorkItemAssignedToUserFactvw.WorkItemAssignedToUser_UserDimKey) AS Assignments
FROM WorkItemDimvw INNER JOIN WorkItemAssignedToUserFactvw
ON WorkItemDimvw.WorkItemDimKey = WorkItemAssignedToUserFactvw.WorkItemDimKey
GROUP BY WorkItemDimvw.Id
The EXISTING query is below and I'm wondering / forgeting if I should:
Just add in COUNT(WorkItemAssignedToUserFactvw.WorkItemAssignedToUser_UserDimKey) AS Assignments since joins are existing, except it is group by WorkItemDimvw.Id
Should it instead be a subquery in the Select below?
Query:
SELECT
SRD.ID,
SRD.Title,
SRD.Description,
SRD.EntityDimKey,
WI.WorkItemDimKey,
IATUFact.DateKey
FROM
SLAConfigurationDimvw
INNER JOIN SLAInstanceInformationFactvw
ON SLAConfigurationDimvw.SLAConfigurationDimKey = SLAInstanceInformationFactvw.SLAConfigurationDimKey
RIGHT OUTER JOIN ServiceRequestDimvw AS SRD
INNER JOIN WorkItemDimvw AS WI
ON SRD.EntityDimKey = WI.EntityDimKey
LEFT OUTER JOIN WorkItemAssignedToUserFactvw AS IATUFact
ON WI.WorkItemDimKey = IATUFact.WorkItemDimKey
AND IATUFact.DeletedDate IS NULL
The trick is to aggregate the data on a sub query, before you join it.
SELECT
SRD.ID,
SRD.Title,
SRD.Description,
SRD.EntityDimKey,
WI.WorkItemDimKey,
IATUFact.DateKey,
IATUFact.Assignments
FROM
SLAConfigurationDimvw
INNER JOIN
SLAInstanceInformationFactvw
ON SLAConfigurationDimvw.SLAConfigurationDimKey = SLAInstanceInformationFactvw.SLAConfigurationDimKey
RIGHT OUTER JOIN
ServiceRequestDimvw AS SRD
ON <you're missing something here>
INNER JOIN
WorkItemDimvw AS WI
ON SRD.EntityDimKey = WI.EntityDimKey
LEFT OUTER JOIN
(
SELECT
WorkItemDimKey,
DateKey,
COUNT(WorkItemAssignedToUser_UserDimKey) AS Assignments
FROM
WorkItemAssignedToUserFactvw
WHERE
DeletedDate IS NULL
GROUP BY
WorkItemDimKey,
DateKey
)
IATUFact
ON WI.WorkItemDimKey = IATUFact.WorkItemDimKey

SQL Join only if all records have a match

I have 3 tables:
CP_carthead (idOrder)
CP_cartrows (idOrder, idCartRow)
CP_shipping (idCartRow, idShipping, dateShipped)
There can be multiple idCartRows per idOrder.
I want to get all orders where all its idCartRows exist in CP_shipping. This seems like it should be simple, but I haven't found much on the web.
Here's my query now:
SELECT
s.idOrder
, s.LatestDateShipped
FROM
CP_carthead o
LEFT OUTER JOIN (
SELECT
MAX(s.dateShipped) [LatestDateShipped]
, r.idOrder
FROM
CP_shipping s
LEFT OUTER JOIN CP_cartrows r ON s.idCartRow = r.idCartRow
GROUP BY
r.idOrder
) s ON o.idOrder = s.idOrder
Your query is returning rows from "s" and not the orders. Based on your question, I came up with this query:
select o.*
from CP_Carthead o
where o.orderId in (select cr.idOrder
from cp_cartrows cr left outer join
cp_shipping s
on cr.idCartRow = s.IdCartrow
group by cr.idOrder
having count(s.idCartRow) = COUNT(*)
)
The subquery in the in statement is getting orders all of whose cartrows are in shipping.