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.
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 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
I have the following query and would like to convert it to using a left outer join instead of a not in to see if it would run faster that way. It's currently taking this query about 40 seconds to run on our database. I'm not familiar enough with using outer joins for this type of thing to convert it myself.
select
c.contact_id as contact_id,
c.orgid as organization_id,
c.first_name as first_name,
c.last_name as last_name,
a.address_state as state
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
where a.address_state = 'OH'
and (c.orgid = 45 or c.orgid = 55)
and c.contact_id NOT IN (
select pc.contact_id
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
inner join cnx_contact_group_participant as gp on c.contact_id = gp.contact_id
inner join cnx_contact_participant_role as cr on gp.participant_role_uid = cr.participant_role_uid
inner join cnx_contact_group as cg on gp.group_uid = cg.group_uid
inner join cnx_contact_group_participant as pgp on cg.primary_participant_uid = pgp.participant_uid
inner join cnx_contact as pc on pgp.contact_id = pc.contact_id
where (c.orgid = 45 or c.orgid = 55)
and cr.name = 'Applicant'
);
select
c.columns
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
LEFT JOIN
(Subquery goes here) x
ON x.contact _id = c.contact_id
where a.participant_state = 'OH'
and c.orgid IN(45,55)
and x.contact_id IS NULL;
I am trying to run a select query joining multiple tables. Ont of the tables has got a column coordinate with point type. Everything works correctly but in joins it does not allow me to select X(coordinate) or X(point(coordinate)).
I can select coordinate in the join and can select X(coordinate) directly on the table but both together dont work.
select x(coordinate) from location_coordinate
The above one works
select ca.campus_id,
ca.campus_name,
ca.status_code,
ca_loc.location_id,
ca_loc.address,
ca_coo.coordinate,
ca_loc.locality_id,
ca_loc.area_id,
ca_loc.city_id,
ca_loc.state_id,
loc_locality.name as locality_name,
loc_area.name as area_name,
loc_city.name as city_name,
loc_state.name as state_name
from campus_account ca
left join location ca_loc
on ca_loc.location_id=ca.location_id
left join location_coordinate ca_coo
on ca_loc.location_id=ca_coo.location_id
left join location_master loc_locality
on(ca_loc.locality_id = loc_locality.location_master_id)
left join location_master loc_area
on(ca_loc.area_id = loc_area.location_master_id)
left join location_master loc_city
on(ca_loc.city_id = loc_city.location_master_id)
left join location_master loc_state
on(ca_loc.state_id = loc_state.location_master_id);
This also works. But if i try to do
select ca.campus_id,
ca.campus_name,
ca.status_code,
ca_loc.location_id,
ca_loc.address,
ca_coo.X(coordinate),
ca_loc.locality_id,
ca_loc.area_id,
ca_loc.city_id,
ca_loc.state_id,
loc_locality.name as locality_name,
loc_area.name as area_name,
loc_city.name as city_name,
loc_state.name as state_name
from campus_account ca
left join location ca_loc
on ca_loc.location_id=ca.location_id
left join location_coordinate ca_coo
on ca_loc.location_id=ca_coo.location_id
left join location_master loc_locality
on(ca_loc.locality_id = loc_locality.location_master_id)
left join location_master loc_area
on(ca_loc.area_id = loc_area.location_master_id)
left join location_master loc_city
on(ca_loc.city_id = loc_city.location_master_id)
left join location_master loc_state
on(ca_loc.state_id = loc_state.location_master_id);
It does not select and gives me an error saying X is not a column.
Please help
It should be X(ca_coo.coordinate) not ca_coo.X(coordinate).
I have a query like this
SELECT
tbl_products.*,
GROUP_CONCAT(tags.name)
FROM
tbl_page_collections_products,
(SELECT page_collection_name as name
FROM tbl_page_collections
LEFT JOIN tbl_pages ON tbl_page_collections.page_id = tbl_pages.page_id
WHERE tbl_pages.page_name LIKE '%friends%') tags
LEFT JOIN tbl_page_collections
ON tbl_page_collections.page_collection_id = tbl_page_collections_products.colID
LEFT JOIN tbl_pages
ON tbl_page_collections.page_id = tbl_pages.page_id
LEFT JOIN tbl_products
ON tbl_products.product_id = tbl_page_collections_products.product
WHERE
tbl_pages.page_name LIKE '%friends%'
The error I get is Unknown column 'tbl_page_collections_products.colID in on clause but I don't get that error when the subquery isn't there and that column exists in that table.
Is something conflicting?
tbl_page_collections_products is not in you subquery from clause. Maybe this is what you want:
SELECT
tbl_products.*,
GROUP_CONCAT(tags.name)
FROM
tbl_products,
(SELECT page_collection_name as name
FROM tbl_page_collections
,tbl_page_collections_products
LEFT JOIN tbl_pages ON tbl_page_collections.page_id = tbl_pages.page_id
WHERE tbl_pages.page_name LIKE '%friends%') tags
LEFT JOIN tbl_page_collections
ON tbl_page_collections.page_collection_id = tbl_page_collections_products.colID
LEFT JOIN tbl_pages
ON tbl_page_collections.page_id = tbl_pages.page_id
LEFT JOIN tbl_products
ON tbl_products.product_id = tbl_page_collections_products.product
WHERE
tbl_pages.page_name LIKE '%friends%'