Power Query SQL code issue - sql

I'm trying to run this SQL code in Power Query but keep getting the following error Message:
Incorrect syntax near the keyword 'GROUP'
The SQL code I'm using is
SELECT Groups.GroupName, AgentTeams.TeamName, Agent.Firstname, Agent.Lastname, CRC.Description, Count(History.HistoryID) AS CountOfHistoryID
FROM ((GroupAgent
INNER JOIN Groups ON GroupAgent.GroupID = Groups.GroupID)
INNER JOIN ((Agent LEFT JOIN History ON Agent.AgentID = History.AgentID)
LEFT JOIN CRC ON History.CRC = CRC.CRC) ON GroupAgent.AgentID = Agent.AgentID)
INNER JOIN (AgentTeams
INNER JOIN AgentStartDates ON AgentTeams.TeamID = AgentStartDates.TeamID) ON GroupAgent.AgentID = AgentStartDates.AgentID
WHERE (((History.CallDateTime) Between GetDate() And DateAdd(d,1,GetDate())
GROUP BY Groups.GroupName, AgentTeams.TeamName, Agent.Firstname, Agent.Lastname, CRC.Description
ORDER BY Groups.GroupName, AgentTeams.TeamName;
Could someone help me fix this or advise me where I'm going wrong?
Adam

Close the parentheses....
SELECT Groups.GroupName, AgentTeams.TeamName, Agent.Firstname, Agent.Lastname, CRC.Description, Count(History.HistoryID) AS CountOfHistoryID
FROM ((GroupAgent INNER JOIN Groups ON GroupAgent.GroupID = Groups.GroupID) INNER JOIN ((Agent LEFT JOIN History ON Agent.AgentID = History.AgentID) LEFT JOIN CRC ON History.CRC = CRC.CRC) ON GroupAgent.AgentID = Agent.AgentID) INNER JOIN (AgentTeams INNER JOIN AgentStartDates ON AgentTeams.TeamID = AgentStartDates.TeamID) ON GroupAgent.AgentID = AgentStartDates.AgentID
WHERE (((History.CallDateTime) Between GetDate() And DateAdd(d,1,GetDate())))
GROUP BY Groups.GroupName, AgentTeams.TeamName, Agent.Firstname, Agent.Lastname, CRC.Description
ORDER BY Groups.GroupName, AgentTeams.TeamName;
Hope it helps...

Related

Access Query - Group by and Max

I have been working on this for a few hours now and just can not figure it out.
How would I go about sorting this query by enco_id and only having the max of each clpr_id show up. for example:
Here is my MSAccess Sql Code
SELECT dbo_Client.med_rec_no, dbo_Encounter.episode_number, dbo_client_program.enco_id, dbo_client_program.clpr_id, dbo_client_program.prle_id, dbo_PROGRAM_LEVEL.prle_name, dbo_Client.fname, dbo_Client.lname, dbo_DISCHARGE_STATUS.dist_name, DS2.dist_name
FROM (((((dbo_Client INNER JOIN dbo_Encounter ON dbo_Client.client_id = dbo_Encounter.client_id) INNER JOIN dbo_Episode_info ON dbo_Encounter.enco_id = dbo_Episode_info.enco_id) INNER JOIN dbo_DISCHARGE_STATUS ON dbo_Episode_info.dist_id = dbo_DISCHARGE_STATUS.dist_id) INNER JOIN dbo_client_program ON dbo_Encounter.enco_id = dbo_client_program.enco_id) INNER JOIN dbo_DISCHARGE_STATUS AS DS2 ON dbo_client_program.dist_id = DS2.dist_id) INNER JOIN dbo_PROGRAM_LEVEL ON dbo_client_program.prle_id = dbo_PROGRAM_LEVEL.prle_id;
Also here is what my query looks like
EDIT:
This is the code I am trying to use now and it still is not working
SELECT dbo_Client.client_id, dbo_Client.med_rec_no, dbo_Encounter.episode_number, dbo_Encounter.start_date, dbo_Encounter.end_date, dbo_client_program.clpr_id, dbo_client_program.enco_id, dbo_PROGRAM_LEVEL.prle_name, dbo_Client.fname, dbo_Client.lname, dbo_DISCHARGE_STATUS.dist_name, DS2.dist_name
FROM (((((dbo_Client INNER JOIN dbo_Encounter ON dbo_Client.client_id = dbo_Encounter.client_id) INNER JOIN dbo_Episode_info ON dbo_Encounter.enco_id = dbo_Episode_info.enco_id) INNER JOIN dbo_DISCHARGE_STATUS ON dbo_Episode_info.dist_id = dbo_DISCHARGE_STATUS.dist_id) INNER JOIN dbo_client_program ON dbo_Encounter.enco_id = dbo_client_program.enco_id) INNER JOIN dbo_DISCHARGE_STATUS AS DS2 ON dbo_client_program.dist_id = DS2.dist_id) INNER JOIN dbo_PROGRAM_LEVEL ON dbo_client_program.prle_id = dbo_PROGRAM_LEVEL.prle_id
WHERE dbo_client_program.clpr_id IN
(SELECT TOP 1 clpr_id FROM dbo_client_program as New
WHERE New.clpr_id = dbo_client_program.clpr_id
ORDER by dbo_client_program.clpr_id DESC) AND (dbo_DISCHARGE_STATUS.dist_name <> DS2.dist_name) AND dbo_Encounter.start_date > #1/1/2020# AND dbo_Encounter.end_date > #1/1/2020#
ORDER BY dbo_Encounter.episode_number;
As you are still understanding the aggregate queries my suggestion is:
1-Create a query where you group by all fields and max(clpr_id). Save this as "Query1"
2-Create another query based on "Query1" and sort by "enco_id "

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

How to join 7 tables in SQL using INNER JOIN

I am creating a stored procedure to join 7 tables in SQL using INNER JOIN, however I am getting an error about incorrect syntax.
I've tried to adjust formatting but I cannot get the syntax correct. Does anyone know where I've gone wrong? I get an error stating
Incorrect syntax near "WHERE"
My code:
CREATE PROCEDURE [dbo].[spInvoicing]
#InvoiceId INT
AS
BEGIN
SELECT
tblInvoices.InvoiceId, tblInvoices.InvoiceDate,
tblClients.Firstname, tblClients.Surname, tblClients.Address, tblClients.City,
tblClients.Postcode, tblClients.Email, tblClients.Phone,
tblAppointments.AppointmentId, tblAppointments.DateOfAppointment,
tblProductsUsed.ProductsUsedId, tblProducts.ProductName, tblProductsUsed.Quantity,
tblPointofSale.SaleId, tblPointOfSale.CostOfProducts, tblPointOfSale.CostOfServices,
tblPointOfSale.VAT, tblInvoices.SaleAmount, tblInvoices.PaymentMethod
FROM
tblClients
INNER JOIN
tblAppointments ON tblClients.ClientId = tblAppointments.ClientId
INNER JOIN
tblPointOfSale
INNER JOIN
tblInvoices ON tblPointOfSale.SaleId = tblInvoices.SaleId
INNER JOIN
tblProductsUsed
INNER JOIN
tblProducts ON tblProductsUsed.ProductId = tblProducts.ProductId
INNER JOIN
tblServicesRendered
INNER JOIN
tblServices ON tblServicesRendered.ServiceId = tblServices.ServiceId ON tblServicesRendered.AppointmentId = tblAppointments.AppointmentId
ON tblPointOfSale.AppointmentId = tblAppointments.AppointmentId
AND tblProductsUsed.SaleId = tblPointOfSale.SaleId
AND tblPointOfSale.ClientId = tblClients.ClientId
WHERE
tblInvoices.InvoiceId = #InvoiceId
END
Your JOINs got a bit scrambled. Try this below, and see if that fixes the syntax error.
Always try to get your JOINs to follow the format INNER JOIN [Table2] ON [Table2].[Field1] = [Table1].[Field1]
FROM tblClients
INNER JOIN tblAppointments ON tblClients.ClientId = tblAppointments.ClientId
INNER JOIN tblPointOfSale ON tblPointOfSale.AppointmentId = tblAppointments.AppointmentId
AND tblPointOfSale.ClientId = tblClients.ClientId
INNER JOIN tblInvoices ON tblPointOfSale.SaleId = tblInvoices.SaleId
INNER JOIN tblProductsUsed ON tblProductsUsed.SaleId = tblPointOfSale.SaleId
INNER JOIN tblProducts ON tblProductsUsed.ProductId = tblProducts.ProductId
INNER JOIN tblServicesRendered ON tblServicesRendered.AppointmentId = tblAppointments.AppointmentId
INNER JOIN tblServices ON tblServicesRendered.ServiceId = tblServices.ServiceId
I just copy and pasted your code into this website - http://poorsql.com/
and it hightlighted the error for you (you're missing AND in the joins).
As #DBro said - get in the habit of putting your JOINs on the new line and format it a little differently, and you'll have far less trouble.

Use query with InnerJoin

I'm trying to pass this query to inner join, but it does not know how?
This is the query I want to use InnerJoin with these values ​​where
SELECT
ticket.id_ticket,
ticket.id_rede,
historico.id_historico,
historico.id_ticket,
centro.id_centro,
eqpto.id_eqpto,
eqpto.nome,
centro.sigla_centro,
interface.id_interface,
interface.id_eqpto,
interface.desig,
tecnologia.descricao,
interface.id_tecnologia,
tecnologia.id_tecnologia,
eqpto.id_centro,
eqpto.id_rede
FROM
app_gpa_ticket.ticket,
app_gpa_ticket.historico,
dados_v3.centro,
dados_v3.eqpto,
dados_v3.interface,
dados_v3.tecnologia
WHERE
ticket.id_ticket = historico.id_ticket AND
centro.id_centro = eqpto.id_centro AND
eqpto.id_eqpto = interface.id_eqpto AND
eqpto.id_rede = ticket.id_rede AND
tecnologia.id_tecnologia = interface.id_tecnologia;
Thank you!
I think you are trying to go to the standard (explicit) syntax. What you need to do is take what would be your JOIN operators in the WHERE clause and move them near the table itself. What you need to know is what tables on the left (Before the INNER JOIN operator you are joining to the right (After the INNER JOIN operator)
SELECT
ticket.id_ticket,
ticket.id_rede,
historico.id_historico,
historico.id_ticket,
centro.id_centro,
eqpto.id_eqpto,
eqpto.nome,
centro.sigla_centro,
interface.id_interface,
interface.id_eqpto,
interface.desig,
tecnologia.descricao,
interface.id_tecnologia,
tecnologia.id_tecnologia,
eqpto.id_centro,
eqpto.id_rede
FROM app_gpa_ticket.ticket
INNER JOIN app_gpa_ticket.historico ON ticket.id_ticket = historico.id_ticket
INNER JOIN dados_v3.eqpto ON eqpto.id_rede = ticket.id_rede
INNER JOIN dados_v3.interface ON eqpto.id_eqpto = interface.id_eqpto
INNER JOIN dados_v3.centro ON centro.id_centro = eqpto.id_centro
INNER JOIN dados_v3.tecnologia ON tecnologia.id_tecnologia = interface.id_tecnologia

syntax error in from clause in access

Below is my query and it says syntax error in from clause whereas it is perfectly working in SQL.After the error 'AS' is highlighted
SELECT
Table1.*,
emp_details_full1.*
FROM Table1
LEFT JOIN
((SELECT
iss_personal_detail.Specialization,
iss_personal_detail.New_rank,
iss_personal_detail.Induction_tr,
iss_personal_detail.Title,
iss_personal_detail.f_name,
iss_personal_detail.m_name,
iss_personal_detail.l_name,
iss_personal_detail.Father_Hus_Name,
iss_personal_detail.Category,
iss_personal_detail.Community,
iss_personal_detail.SEX,
iss_personal_detail.source_recruit,
iss_personal_detail.Pay_Parity,
iss_personal_detail.[Date_Pay_Parity],
iss_personal_detail.UPSC_Rank,
iss_personal_detail.dob,
iss_personal_detail.doj_govt,
iss_personal_detail.DOA_ISS,
iss_personal_detail.Batch,
iss_personal_detail.Year_of_Exam,
iss_personal_detail.Native_Distt,
iss_personal_detail.Native_State,
iss_personal_detail.[Highest Qualification],
iss_personal_detail.Languages_Known,
iss_personal_detail.Mother_Toung,
iss_personal_detail.Marital_Status,
iss_personal_detail.E_mail_ID,
iss_personal_detail.retire_reason,
iss_personal_detail.title_m,
Present_Posting.*,
ISS_MINISTRY_CODE_LIST.*,
ISS_DEPARTMENT_CODE_LIST.*,
ISS_CITY_CODE_LIST.*,
Desig_Code.*,
Grade_Code.Grade_code
FROM ISS_CITY_CODE_LIST
INNER JOIN( Grade_Code
INNER JOIN (Desig_Code
INNER JOIN (((iss_personal_detail
INNER JOIN Present_Posting
ON iss_personal_detail.OID = Present_Posting.OID)
INNER JOIN ISS_MINISTRY_CODE_LIST
ON Present_Posting.ministry = ISS_MINISTRY_CODE_LIST.MINISTRY_CODE)
INNER JOIN ISS_DEPARTMENT_CODE_LIST
ON Present_Posting.department = ISS_DEPARTMENT_CODE_LIST.DEPARTMENT_CODE)
ON Desig_Code.Code = Present_Posting.designation)
ON Grade_Code.Grade_code = Present_Posting.Grade)
ON ISS_CITY_CODE_LIST.city_code=Present_Posting.office_city
)) AS emp_details_full1 ON
(emp_details_full1.DEPARTMENT_CODE=Table1.department) AND
(emp_details_full1.MINISTRY_CODE=Table1.ministry) AND
(emp_details_full1.city_code=Table1.city) AND
(emp_details_full1.Grade_Code=Table1.grade)
WHERE Table1.grade='02';
The first thing I would do is take the inner select and create a view from it.
This will provide a simple, easy to read, easy to debug sql code.
CREATE VIEW emp_details_full1
AS
SELECT iss_personal_detail.Specialization, iss_personal_detail.New_rank,
iss_personal_detail.Induction_tr, iss_personal_detail.Title,
iss_personal_detail.f_name, iss_personal_detail.m_name,
iss_personal_detail.l_name, iss_personal_detail.Father_Hus_Name,
iss_personal_detail.Category, iss_personal_detail.Community,
iss_personal_detail.SEX, iss_personal_detail.source_recruit,
iss_personal_detail.Pay_Parity, iss_personal_detail.[Date_Pay_ Parity],
iss_personal_detail.UPSC_Rank, iss_personal_detail.dob,
iss_personal_detail.doj_govt, iss_personal_detail.DOA_ISS,
iss_personal_detail.Batch, iss_personal_detail.Year_of_Exam,
iss_personal_detail.Native_Distt, iss_personal_detail.Native_State,
iss_personal_detail.[Highest Qualification],
iss_personal_detail.Languages_Known,
iss_personal_detail.Mother_Toung, iss_personal_detail.Marital_Status,
iss_personal_detail.E_mail_ID, iss_personal_detail.retire_reason,
iss_personal_detail.title_m, Present_Posting., ISS_MINISTRY_CODE_LIST.,
ISS_DEPARTMENT_CODE_LIST., ISS_CITY_CODE_LIST., Desig_Code.*,
Grade_Code.Grade_code
FROM ISS_CITY_CODE_LIST INNER JOIN( Grade_Code INNER JOIN (Desig_Code INNER JOIN
(((iss_personal_detail INNER JOIN Present_Posting ON iss_personal_detail.OID =
Present_Posting.OID) INNER JOIN ISS_MINISTRY_CODE_LIST
ON Present_Posting.ministry = ISS_MINISTRY_CODE_LIST.MINISTRY_CODE)
INNER JOIN ISS_DEPARTMENT_CODE_LIST ON
Present_Posting.department = ISS_DEPARTMENT_CODE_LIST.DEPARTMENT_CODE) ON
Desig_Code.Code = Present_Posting.designation) ON Grade_Code.Grade_code =
Present_Posting.Grade)
ON ISS_CITY_CODE_LIST.city_code=Present_Posting.office_city
Then the rest of the sql would look like this:
SELECT Table1.,emp_details_full1.
FROM Table1 LEFT JOIN emp_details_full1
ON (emp_details_full1.DEPARTMENT_CODE=Table1.department) AND
(emp_details_full1.MINISTRY_CODE=Table1.ministry) AND
(emp_details_full1.city_code=Table1.city) AND
(emp_details_full1.Grade_Code=Table1.grade) WHERE Table1.grade='02';
Now, if you look closely, you can see that there is a closing bracket missing between the end of the ON clause and the start of the WHERE clause.
So to fix that:
SELECT Table1.,emp_details_full1.
FROM Table1 LEFT JOIN emp_details_full1
ON (emp_details_full1.DEPARTMENT_CODE=Table1.department) AND
(emp_details_full1.MINISTRY_CODE=Table1.ministry) AND
(emp_details_full1.city_code=Table1.city) AND
(emp_details_full1.Grade_Code=Table1.grade)) WHERE Table1.grade='02';
Isn't that much easier to work with?