The below join works in Access, but I need to add a fourth join.
FROM ((Agents
LEFT JOIN Resignation_Pool ON Agents.PF = Resignation_Pool.PF)
LEFT JOIN Teams ON Agents.Team = Teams.ID)
LEFT JOIN Skills ON Agents.PF = Skills.PF
When I add the fourth join, it doesn't work. I know Access is weird about the parenthesis, but I think I have them where they belong. The query just runs forever doing nothing (it shouldn't run long at all) and I end up cancelling it. Any suggestions?
FROM (((Agents
LEFT JOIN Resignation_Pool ON Agents.PF = Resignation_Pool.PF)
LEFT JOIN Teams ON Agents.Team = Teams.ID)
LEFT JOIN Skills ON Agents.PF = Skills.PF)
LEFT JOIN OneMore ON Agents.OM = OneMore.OM
Here is the code that works - my actual query instead of one I found that looked similar.
SELECT DISTINCT A02.PID, A02.PS, A02.PN, A02.PM, C01.RC, C01.IC, C01.RD
INTO AutoCR
FROM ((02_CorrectResults A02
LEFT OUTER JOIN dbo_pol_PGI C01
ON (A02.PID = C01.PID and C01.PS = '999' and C01.PEDate >= #04/01/2012#))
LEFT OUTER JOIN dbo_pol_IL C02
ON C01.PID = C02.PID)
LEFT OUTER JOIN dbo_pol_UN C03
ON C02.ILID = C03.ILID
I add another join and this doesn't work. I tried using inner join instead but Access doesn't like that.
SELECT DISTINCT A02.PID
, A02.PS
, A02.PN
, A02.PM
, C01.RC
, C01.IC
, C01.RD
, C04.CCode
, C04.PCode
, C04.CForm,
INTO AutoCR
FROM (((02_CorrectResults A02
LEFT OUTER JOIN dbo_rol_PGI C01
ON (A02.PID = C01.PID and C01.PS = '999' and C01.PEDate >= #04/01/2012#))
LEFT OUTER JOIN dbo_rol_IL C02
ON C01.PID = C02.PID)
LEFT OUTER JOIN dbo_rol_UN C03
ON C02.ILID = C03.ILID)
LEFT OUTER JOIN dbo_rol_HO C04
ON C03.UnID = C04.UnID
You have a trailing , in your query. Remove it.
SELECT DISTINCT A02.PID
, A02.PS
, A02.PN
, A02.PM
, C01.RC
, C01.IC
, C01.RD
, C04.CCode
, C04.PCode
, C04.CForm, <--- this was the problem
INTO AutoCR
FROM (((02_CorrectResults A02
LEFT OUTER JOIN dbo_rol_PGI C01
ON (A02.PID = C01.PID and C01.PS = '999' and C01.PEDate >= #04/01/2012#))
LEFT OUTER JOIN dbo_rol_IL C02
ON C01.PID = C02.PID)
LEFT OUTER JOIN dbo_rol_UN C03
ON C02.ILID = C03.ILID)
LEFT OUTER JOIN dbo_rol_HO C04
ON C03.UnID = C04.UnID
Related
i have 2 queries that pull same fields with only diff is what the subquery in the where clause returns.
first query pulls based on the code and insurance_group_name
SELECT
cpt_codes.code,
ii.insurance_group_name,
ii.insurance_group_id,
CAST(AVG(dcc_total_view.allowedAmount) AS decimal(10, 2)) `AVG Allowed by Group Name`,
CAST(AVG(rc_total_view.runningCollectedReceivedByProvider) AS decimal(10, 2)) `AVG Collected by Group Name`,
GROUP_CONCAT(DISTINCT file_acceptance) AS `FAN`
FROM ds
LEFT OUTER JOIN dd
ON ds.dos_detail_id = dd.id
LEFT OUTER JOIN ii
ON ds.ii_id = ii.id
LEFT OUTER JOIN dcc
ON ds.id = dcc.dos_id
LEFT OUTER JOIN cpt_codes
ON dcc.cpt_id = cpt_codes.id
LEFT OUTER JOIN bi
ON ds.billing_id = bi.id
LEFT OUTER JOIN cs
ON bi.claim_status = cs.id
LEFT OUTER JOIN rc_total_view
ON ds.payment_information_id = rc_total_view.id
LEFT OUTER JOIN dcc_total_view
ON ds.id = dcc_total_view.dos_id
WHERE (cpt_codes.code, ii.insurance_group_name) IN (SELECT
cpt_codes.code,
ii.insurance_group_name
FROM ds
LEFT OUTER JOIN dd
ON ds.dos_detail_id = dd.id
LEFT OUTER JOIN ii
ON ds.ii_id = ii.id
LEFT OUTER JOIN dcc
ON ds.id = dcc.dos_id
LEFT OUTER JOIN cpt_codes
ON dcc.cpt_id = cpt_codes.id
LEFT OUTER JOIN bi
ON ds.billing_id = bi.id
LEFT OUTER JOIN cs
ON bi.claim_status = cs.id
WHERE cs.status = 'Pending'
AND date_of_service BETWEEN '2017/01/01' AND '2018/08/31'
AND (dcc.rev_code = '0490'
OR dcc.rev_code = '0360'))
AND date_of_service BETWEEN '2017/01/01' AND '2018/08/31'
AND (dcc.rev_code = '0490'
OR dcc.rev_code = '0360')
AND dcc_total_view.allowedAmount > 0
GROUP BY code ,insurance_group_name
this would return a line like this
code
insurance_group_name
insurance_group_id
AVG Allowed by Group Name
AVG Collected by Group Name
FAN
20553
AE
215825
1440.97
889.48
DELA081518,MOLDGILR919,SHIC060618,MANS072718,DELA053018,DELS072518
the Second one pulls based on insurance_group_id
SELECT
cpt_codes.code,
ii.insurance_group_name,
ii.insurance_group_id,
CAST(AVG(dcc_total_view.allowedAmount) AS decimal(10, 2)) `AVG Allowed by Group Name`,
CAST(AVG(rc_total_view.runningCollectedReceivedByProvider) AS decimal(10, 2)) `AVG Collected by Group Name`,
GROUP_CONCAT(DISTINCT file_acceptance) AS `FAN`
FROM ds
LEFT OUTER JOIN dd
ON ds.dos_detail_id = dd.id
LEFT OUTER JOIN ii
ON ds.ii_id = ii.id
LEFT OUTER JOIN dcc
ON ds.id = dcc.dos_id
LEFT OUTER JOIN cpt_codes
ON dcc.cpt_id = cpt_codes.id
LEFT OUTER JOIN bi
ON ds.billing_id = bi.id
LEFT OUTER JOIN cs
ON bi.claim_status = cs.id
LEFT OUTER JOIN rc_total_view
ON ds.payment_information_id = rc_total_view.id
LEFT OUTER JOIN dcc_total_view
ON ds.id = dcc_total_view.dos_id
WHERE (cpt_codes.code, ii.insurance_group_id) IN (SELECT
cpt_codes.code,
ii.insurance_group_id
FROM ds
LEFT OUTER JOIN dd
ON ds.dos_detail_id = dd.id
LEFT OUTER JOIN ii
ON ds.ii_id = ii.id
LEFT OUTER JOIN dcc
ON ds.id = dcc.dos_id
LEFT OUTER JOIN cpt_codes
ON dcc.cpt_id = cpt_codes.id
LEFT OUTER JOIN bi
ON ds.billing_id = bi.id
LEFT OUTER JOIN cs
ON bi.claim_status = cs.id
WHERE cs.status = 'Pending'
AND date_of_service BETWEEN '2017/01/01' AND '2018/08/31'
AND (dcc.rev_code = '0490'
OR dcc.rev_code = '0360'))
AND date_of_service BETWEEN '2017/01/01' AND '2018/08/31'
AND (dcc.rev_code = '0490'
OR dcc.rev_code = '0360')
AND dcc_total_view.allowedAmount > 0
GROUP BY code ,insurance_group_id
this would return a line like
code
insurance_group_name
insurance_group_id
AVG Allowed by Group Name
AVG Collected by Group Name
FAN
20553
AE
10198703100012
250.25
150.36
MOLDGILR919
when doing union on these 2 i find some data is duplicated where the FAN # that the second query returns is included in the output from the first as you can see the FAN# is part of the ones that make up the results of the first script.
how can i join together the results of these 2 queries and make sure if the FAN# in the second is already included in results from the first for a given code (like 20553 in the example) then that row from the second will not be included in the final output ?
I hope i have included enough info and detail to help me out here, I really appreciate any help to figure this out.
Put the output of your first statemen in a temporary table, lets name that #temp1, and the output of your second query in a temporary table named #temp2.
After that you can do:
SELECT *
FROM #temp1
UNION ALL
SELECT *
FROM #temp2
WHERE code NOT IN (SELECT code FROM #temp1)
EDIT: Because of the FAN problem..
I think you should change your queries, in this way:
Do not use GROUP_CONCAT(...) to combine the results of different FAN's in one row, and add file_acceptance to the GROUP BY-clause
This way you will get indivdual records for every FAN, annd, after the UNION ALL you can GROUP_CONCAT() then again.
This might need some more think/tweak work because of the AVG() fields....
I have an SQL query like below which has a where clause as WHERE pg.active=1 AND tcg.active=1 AND tpg.active=1,issue right now is that one or all of pg.active tcg.active ,tpg.active is set to 1 at any time,in the case where one of these is not set to 1 query doesn't return anything.
How do I change the where clause to return if any of pg.active tcg.active ,tpg.active is set to 1?
SELECT
..........
FROM software_products_software_images spsi
INNER JOIN software_images si ON si.software_image_id = spsi.software_image_id
INNER JOIN software_products sp ON sp.id = spsi.software_product_id
LEFT JOIN software_products_software_images_testplan_gate tpg ON tpg.software_products_software_images_id = spsi.Id
LEFT JOIN test_suites tp ON tp.id = tpg.testplan_id
LEFT JOIN software_products_software_images_testcase_gate tcg ON tcg.software_products_software_images_id = spsi.Id
LEFT JOIN test_cases tc ON tc.id = tcg.testcase_id
LEFT JOIN test_suites ts ON ts.id = tc.test_suite_id
LEFT JOIN software_products_software_images_percentage_gate pg ON pg.software_products_software_images_id = spsi.Id
...........................
WHERE pg.active=1 AND tcg.active=1 AND tpg.active=1
AND si.software_image='NHSS.QSDK.7.0.1' ORDER BY si.software_image, ts.suite_name
I believe you need to encapsulate the OR part of the WHERE statement in round brackets so that it returns a TRUE on any OR match along with your other conditions.
SELECT
..........
FROM software_products_software_images spsi
INNER JOIN software_images si ON si.software_image_id = spsi.software_image_id
INNER JOIN software_products sp ON sp.id = spsi.software_product_id
LEFT JOIN software_products_software_images_testplan_gate tpg ON tpg.software_products_software_images_id = spsi.Id
LEFT JOIN test_suites tp ON tp.id = tpg.testplan_id
LEFT JOIN software_products_software_images_testcase_gate tcg ON tcg.software_products_software_images_id = spsi.Id
LEFT JOIN test_cases tc ON tc.id = tcg.testcase_id
LEFT JOIN test_suites ts ON ts.id = tc.test_suite_id
LEFT JOIN software_products_software_images_percentage_gate pg ON pg.software_products_software_images_id = spsi.Id
...........................
WHERE (pg.active=1 OR tcg.active=1 OR tpg.active=1)
AND si.software_image='NHSS.QSDK.7.0.1' ORDER BY si.software_image, ts.suite_name
SELECT jadwal_ajar.hari, jadwal_ajar.waktu, jenjang.nama_jenjang, jurusan.nama_jurusan, kelas.nama_kelas, mapel.nama_mapel, guru.nama_guru
FROM jadwal_ajar,guru , jenjang , jurusan , kelas , mapel
LEFT JOIN jadwal_ajar as a5 ON guru.kode_guru = a5.kode_guru
LEFT JOIN jadwal_ajar as a1 ON kelas.kode_kelas = a1.kode_kelas
LEFT JOIN jadwal_ajar as a2 ON mapel.kode_mapel = a2.kode_mapel
LEFT JOIN jadwal_ajar as a3 ON j.kode_jenjang = a3.kode_jenjang
LEFT JOIN jadwal_ajar as a4 ON jurusan.kode_jurusan = a4.kode_jurusan
The error you got is because of a missing field in the table guru, you should check it out.
And, from what I can see, I think that you are trying to join tables in the from, here is the way to achieve that:
SELECT jadwal_ajar.hari, jadwal_ajar.waktu,
jenjang.nama_jenjang, jurusan.nama_jurusan, kelas.nama_kelas,
mapel.nama_mapel, guru.nama_guru
FROM jadwal_ajar
LEFT JOIN guru ON guru.kode_guru = jadwal_ajar.kode_guru
LEFT JOIN kelas ON kelas.kode_kelas = jadwal_ajar.kode_kelas
LEFT JOIN mapel ON mapel.kode_mapel = jadwal_ajar.kode_mapel
LEFT JOIN jenjang ON jenjang.kode_jenjang = jadwal_ajar.kode_jenjang
LEFT JOIN jurusan ON jurusan.kode_jurusan = jadwal_ajar.kode_jurusan
I understand about how to use the count with the group by, but I need a specific use of this reverse, I try to do this query:
SELECT
COUNT(pac.strid_paciente)
FROM tb_paciente AS pac
LEFT JOIN tb_entidades AS ent
ON pac.strid_entidad = ent.strid_entidad
LEFT JOIN tb_escolaridad AS esc
ON pac.strid_escolaridad = esc.strID_Escolaridad
LEFT JOIN tb_etnia AS etn
ON pac.strid_etnia = etn.strID_Etnia
LEFT JOIN tb_raza AS raza
ON pac.strid_raza = raza.strid_raza
LEFT JOIN tb_religion AS rel
ON pac.strid_religion = rel.strid_religion
LEFT JOIN tb_ocupacion AS ocu
ON pac.strOfi_Paciente = ocu.strID_Ocupacion
LEFT JOIN tb_consulta AS con
ON pac.strid_paciente = con.strid_paciente
LEFT JOIN tb_usuarios AS usu
ON usu.strid_usuario = con.strid_medico
GROUP BY pac.strid_paciente
And it gives me:
COUNT(pac.strid_paciente)
1
1
2
3
4
5
2
I need the result of every count: 18, the problem is that I cant delete the group by because give a different result. Something like that:
COUNT(pac.strid_paciente)
18
Have you tried SUM( COUNT(pac.strid_paciente) ) OR if that fails then the following will work:
SELECT SUM( t.cnt ) FROM(
SELECT
COUNT(pac.strid_paciente) AS cnt
FROM tb_paciente AS pac
LEFT JOIN tb_entidades AS ent
ON pac.strid_entidad = ent.strid_entidad
LEFT JOIN tb_escolaridad AS esc
ON pac.strid_escolaridad = esc.strID_Escolaridad
LEFT JOIN tb_etnia AS etn
ON pac.strid_etnia = etn.strID_Etnia
LEFT JOIN tb_raza AS raza
ON pac.strid_raza = raza.strid_raza
LEFT JOIN tb_religion AS rel
ON pac.strid_religion = rel.strid_religion
LEFT JOIN tb_ocupacion AS ocu
ON pac.strOfi_Paciente = ocu.strID_Ocupacion
LEFT JOIN tb_consulta AS con
ON pac.strid_paciente = con.strid_paciente
LEFT JOIN tb_usuarios AS usu
ON usu.strid_usuario = con.strid_medico
GROUP BY pac.strid_paciente
) AS t
Need a little help with subqueries
If I have a 1 query like this:
SELECT Learner.Learner_Id, Max(LearnerEmploymentStatus.DateEmpStatApp) AS LatestEmpDate, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON LearnerEmploymentStatus.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
WHERE EmploymentStatusMonitoring.ESMType="BSI"
GROUP BY Learner.Learner_Id, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
...and another like this:
SELECT Learner.Learner_Id, LearnerEmploymentStatus.DateEmpStatApp, EmploymentStatusMonitoring.ESMCode
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON Learner.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
...and I wanted to do a join between the 2 queries (LEFT JOIN on the common Learner_Id and LatestEmpDate / DateEmpStatApp fields), how would I go about doing all this work in a single query where the 2 queries above would be subqueries?
My attempt below is not being accepted (JOIN expression not supported):
SELECT sQ1.Learner_Id, sQ1.LearnRefNumber, sQ1.FamilyName, sQ1.GivenNames, sQ1.LatestEmpDate, sQ1.ESMType, sQ2.ESMCode
FROM
(SELECT Learner.Learner_Id, Max(LearnerEmploymentStatus.DateEmpStatApp) AS LatestEmpDate, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON LearnerEmploymentStatus.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
WHERE EmploymentStatusMonitoring.ESMType="BSI"
GROUP BY Learner.Learner_Id, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType) As sQ1
LEFT JOIN
(SELECT Learner.Learner_Id, LearnerEmploymentStatus.DateEmpStatApp, EmploymentStatusMonitoring.ESMCode
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON Learner.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id) As sQ2
ON (sQ1.Learner_Id = sQ2.Learner_Id) AND (sQ1.LatestEmpDate = sQ2.DateEmpStatApp);
Would something like this get you what you want...?
SELECT l.Learner_Id, d.LatestEmpDate, l.LearnRefNumber, l.FamilyName, l.GivenNames, m.ESMType, m.ESMCode
FROM ((Learner AS l
LEFT JOIN (
SELECT s.Learner_Id, MAX(s.DateEmpStatApp) AS LatestEmpDate
FROM LearnerEmploymentStatus AS s
GROUP BY s.Learner_Id) AS d ON d.Learner_Id = l.Learner_Id)
LEFT JOIN LearnerEmploymentStatus AS ls ON (ls.Learner_Id = d.Learner_Id) AND (ls.DateEmpStatApp = d.LatestEmpDate))
LEFT JOIN EmploymentStatusMonitoring AS m ON m.LearnerEmploymentStatus_Id = ls.LearnerEmploymentStatus_Id
WHERE m.ESMType = 'BSI'
Assumes the same learner won't have the same DateEmpStatApp twice, which may or not be valid.