I have this query:
SELECT DISTINCT
enc.id, enc.cus_id, enc.createdon, enc.status, enc.segment, enc_task.orderid,
enc_task.taskid, enc.currentstep, enc.groupid, enc.fdprotocol,
enc_task.linkfile, cus.fname, cus.lname, login.first_name, login.last_name,
login.username, login.user_code, login.id as assigned_id, fp.protocol,
init.typename, table4.allowtotal
FROM
mob_encounters enc
JOIN mob_encounters_task enc_task ON enc_task.encounterid=enc.id
JOIN mob_customer cus ON cus.id=enc_task.cus_id
JOIN mob_login login ON login.id=enc.createdby
LEFT JOIN mob_protocol_type fp ON fp.id=enc.fdprotocol
LEFT JOIN initiation_type init ON init.id=enc.groupid
LEFT JOIN mob_table4 table4 ON table4.encid=enc.id
GROUP BY
enc.id
The query is working, except I need it to return the latest row of enc_task.encounterid when doing this match: enc_task.encounterid = enc.id. Is it possible?
A subquery might do what you're looking for:
SELECT DISTINCT
enc.id, enc.cus_id, enc.createdon, enc.status, enc.segment, enc_task.orderid, enc_task.taskid,
enc.currentstep, enc.groupid, enc.fdprotocol, enc_task.linkfile, cus.fname,
cus.lname, login.first_name, login.last_name, login.username, login.user_code,
login.id as assigned_id, fp.protocol, init.typename, table4.allowtotal
FROM
mob_encounters enc
JOIN
(select encounterid, orderid, taskid, linkfile from mob_encounters_task order by [FIELD THAT DESIGNATES LATEST VALUE] desc) AS enc_task ON enc_task.encounterid=enc.id
JOIN
mob_customer cus ON cus.id=enc_task.cus_id
JOIN
mob_login login ON login.id=enc.createdby
LEFT JOIN
mob_protocol_type fp ON fp.id=enc.fdprotocol
LEFT JOIN
initiation_type init ON init.id=enc.groupid
LEFT JOIN
mob_table4 table4 ON table4.encid=enc.id
GROUP BY
enc.id
Related
I have this query:
SELECT work_orders.id,work_orders.create_datetime,work_orders.location,
work_orders.description,user_accounts.function as function,
user_accounts.name as username,work_order_states.name as state,
work_orders_history.state_id,max(work_order_states.id)
FROM `work_orders`
LEFT JOIN work_orders_history
on work_orders.id = work_orders_history.work_order_id
LEFT JOIN user_accounts
on work_orders.create_user_id = user_accounts.id
LEFT JOIN work_order_type
on work_orders_history.type_id = work_order_type.id
LEFT JOIN work_order_states
on work_orders_history.state_id = work_order_states.id
... and the result is this:
But what i want is only get the rows with max state_id.
I already tried various queries, but this is the best result I can get.
EDIT: Since it's more than 65k results it's hard to provide a fiddle. The desireable result is like this image:
Thanks in advance.
You appear to be very close in your query. You need to add a GROUP BY and take away one of your select columns.
SELECT work_orders.id,work_orders.create_datetime,work_orders.location,
work_orders.description,user_accounts.[function] as [function],
user_accounts.name as username,work_order_states.name as state,
max(work_order_states.id)
FROM `work_orders`
LEFT JOIN work_orders_history
on work_orders.id = work_orders_history.work_order_id
LEFT JOIN user_accounts
on work_orders.create_user_id = user_accounts.id
LEFT JOIN work_order_type
on work_orders_history.type_id = work_order_type.id
LEFT JOIN work_order_states
on work_orders_history.state_id = work_order_states.id
GROUP BY work_orders.id,work_orders.create_datetime,work_orders.location,
work_orders.description,user_accounts.function,
user_accounts.name,work_order_states.name
HAVING work_orders_states.ID = (SELECT MAX(state_id) FROM work_orders_history)
I think what you are looking for is the HAVING and GROUP BY clause combined..
SELECT work_orders.id,work_orders.create_datetime,work_orders.location,
work_orders.description,user_accounts.function as function,
user_accounts.name as username,work_order_states.name as state,
work_orders_history.state_id,max(work_order_states.id)
FROM `work_orders`
LEFT JOIN work_orders_history
on work_orders.id = work_orders_history.work_order_id
LEFT JOIN user_accounts
on work_orders.create_user_id = user_accounts.id
LEFT JOIN work_order_type
on work_orders_history.type_id = work_order_type.id
LEFT JOIN work_order_states
on work_orders_history.state_id = work_order_states.id
GROUP BY work_orders.id,work_orders.create_datetime,work_orders.location,
work_orders.description,user_accounts.function,
user_accounts.name,work_order_states.name,
work_orders_history.state_id
HAVING work_orders_history.state_id=max(work_order_states.id)
I have a query
SELECT DISTINCT c.camp_id AS camp_id,
c.camp_key AS camp_key,
c.camp_active AS camp_active,
c.camp_deleted AS camp_deleted,
c.camp_name AS camp_name,
c.camp_cpc AS camp_cpc,
c.camp_destination AS camp_destination,
camp_token1,
camp_token2,
camp_token3,
camp_token4,
camp_token5,
camp_token6,
camp_token7,
camp_token8,
camp_token9,
camp_token10,
token1_field,
token2_field,
token3_field,
token4_field,
token5_field,
token6_field,
token7_field,
token8_field,
token9_field,
token10_field,
group_name,
group_id,
source_id,
source_name,
user_name
FROM mt_campaigns c
LEFT JOIN mt_offers USING (camp_id)
LEFT JOIN mt_groups USING (group_id)
LEFT JOIN mt_traffic_sources USING (source_id)
LEFT JOIN mt_account WHERE c.owner_id = mt_account.user_id
WHERE camp_deleted=0
Now I want to join another table(mt_account) which has a column called user_id and it matches with the the owner_id column of the mt_canpaigns.
How can i edit the join query?
replace
LEFT JOIN mt_account WHERE c.owner_id=mt_account.user_id
for
LEFT JOIN mt_account ON c.owner_id=mt_account.user_id
You need to use ON to define the fields you want to join on instead of WHERE
Is there a way that I can combine these two queries:
FIRST QUERY
select top 100
WORK.pzInsKey,
WORK.pyID,
PARTY.MacID,
PARTY.OtherPartyID,
PARTY.CustomerEmail,
ACCOUNT.AccountNumber,
ACCOUNT.AccountName,
ACCOUNT.AdviserCode,
ACCOUNT.AdviserName,
ACCOUNT.DealerCode,
ACCOUNT.DealerName,
ACCOUNT.PrimaryAccount,
ACCOUNT.ProductCategory,
ACCOUNT.ProductCode,
ACCOUNT.ProductDescription,
ACCOUNT.RegisteredState,
DOCUMENT.UDOCID
from
workTable WORK,
partyTable PARTY,
accountTable ACCOUNT,
documentTable DOCUMENT,
notesTable NOTES
where WORK.pzInsKey = PARTY.pxInsIndexedKey
and WORK.pzInsKey = ACCOUNT.pxInsIndexedKey
and WORK.pyID = DOCUMENT.CaseID
and SECOND QUERY
SELECT top 100
BusinessAreaTbl.businessarea,
ProcessTbl.process,
SubProcessTbl.subprocess
FROM workTable WORK
LEFT OUTER JOIN (SELECT DISTINCT Product_ID businessarea_id, Product businessarea from CaseTypesTable) BusinessAreaTbl
ON WORK.RequestBusinessArea#1 = BusinessAreaTbl.businessarea_id
LEFT OUTER JOIN (SELECT DISTINCT Process_ID, Process, Product_ID businessarea_id from CaseTypesTable) ProcessTbl
ON WORK.RequestProcess#1 = ProcessTbl.process_id
AND ProcessTbl.businessarea_id = WORK.RequestBusinessArea#1
LEFT OUTER JOIN (SELECT DISTINCT SubProcess_ID, SubProcess, Product_ID businessarea_id, Process_ID from CaseTypesTable) SubProcessTbl
ON WORK.RequestSubProcess#1 = SubProcessTbl.subprocess_id
AND SubProcessTbl.businessarea_id = WORK.RequestBusinessArea#1
AND SubProcessTbl.process_id = WORK.RequestProcess#1
It's basically two queries which produce separate results, but each query includes data from the workTable. In the 2nd query, the workTable data is derived from the CaseTypesTable.
I essentially just want the businessarea, process, and subprocess fields to be included with the results of the first query.
Thanks in advance for any help.
This should work:
(SELECT top 100
w.pzInsKey,
w.pyID,
p.MacID,
p.OtherPartyID,
p.CustomerEmail,
a.AccountNumber,
a.AccountName,
a.AdviserCode,
a.AdviserName,
a.DealerCode,
a.DealerName,
a.PrimaryAccount,
a.ProductCategory,
a.ProductCode,
a.ProductDescription,
a.RegisteredState,
d.UDOCID
FROM workTable w
LEFT JOIN partyTable p
ON w.pzInsKey = p.pxInsIndexedKey
LEFT JOIN accountTable a
ON w.pzInsKey = a.pxInsIndexedKey
LEFT JOIN documentTable d
ON w.pyID = d.CaseID)
UNION
(SELECT top 100
ba.businessarea,
pr.process,
spr.subprocess
FROM workTable w
LEFT OUTER JOIN (SELECT DISTINCT Product_ID businessarea_id, Product businessarea from CaseTypesTable) BusinessAreaTbl ba
ON w.RequestBusinessArea#1 = ba.businessarea_id
LEFT OUTER JOIN (SELECT DISTINCT Process_ID, Process, Product_ID businessarea_id from CaseTypesTable) ProcessTbl pr
ON w.RequestProcess#1 = pr.process_id
AND pr.businessarea_id = w.RequestBusinessArea#1
LEFT OUTER JOIN (SELECT DISTINCT SubProcess_ID, SubProcess, Product_ID businessarea_id, Process_ID from CaseTypesTable) SubProcessTbl spr
ON w.RequestSubProcess#1 = spr.subprocess_id
AND spr.businessarea_id = w.RequestBusinessArea#1
AND spr.process_id = w.RequestProcess#1))
Use the keyword UNION to combine two or more seperate SELECT statements.
I'm trying to make a count within several table with JOIN, but when I made several JOINs the COUNTs got wrongly counted.
Basically I've got 4 tables, named:
predective_search
predective_to_product
predective_to_category
predective_to_manufacturer
I want to count the total number of products, categories and manufacturer which has same id in table predective_search.
Here's my code:
SELECT * ,
COUNT(pp.predictive_id) AS total_products,
COUNT(pc.predictive_id) AS total_categories,
COUNT(pm.predictive_id) AS total_manufacturers
FROM predictive_search ps
LEFT JOIN predictive_to_product pp ON (ps.predictive_id = pp.predictive_id)
LEFT JOIN predictive_to_category pu ON (ps.predictive_id = pc.predictive_id)
LEFT JOIN oc_predictive_to_manufacturer pm ON (ps.predictive_id = pm.predictive_id)
GROUP BY ps.predictive_id
Also the GROUP BY is needed I think. I'm stuck at this as I'm not getting any way to do this
SELECT
ps.*,
agg_pp.total_products,
agg_pc.total_categories,
agg_pm.total_manufacturers
FROM predictive_search ps
LEFT JOIN (
SELECT pp.predictive_id, COUNT(*) AS total_products
FROM predictive_to_product pp
GROUP BY pp.predictive_id
) agg_pp ON ps.predictive_id = agg_pp.predictive_id
LEFT JOIN (
SELECT pc.predictive_id, COUNT(*) AS total_categories
FROM predictive_to_category pc
GROUP BY pc.predictive_id
) agg_pc ON ps.predictive_id = agg_pc.predictive_id
LEFT JOIN (
SELECT pm.predictive_id, COUNT(*) AS total_manufacturers
FROM predictive_to_category pm
GROUP BY pm.predictive_id
) agg_pm ON ps.predictive_id = agg_pm.predictive_id
Here is a portion of my query
(SUM(dr.drv)/100)*
(st.suppliers1_value+st.suppliers2_value+st.suppliers3_value+st.suppliers4_value)
This will generate multiple rows. I want to get SUM of all of them. I tried
SUM(SUM(dr.drv)/100)*
(st.suppliers1_value+st.suppliers2_value+st.suppliers3_value+st.suppliers4_value)
as total drv_amount
But it's throwing an error. Can someone guide me to the right direction?
Here is the whole query:
SELECT SUM(dr.tickets_sold) as tickets_total_amount,
SUM(dr.drv) as drv_total_amount,
FORMAT(((SUM(dr.drv)/100) * (st.suppliers1_value+st.suppliers2_value+st.suppliers3_value+st.suppliers4_value) ),2 ) as supplier_commission,
FORMAT(((SUM(dr.drv)/100) * st.exhibitor_value),2) as exhibitor_commission,
FORMAT(((SUM(dr.drv)/100) * st.circuit_value),2) as distributer_commission,
FORMAT(((SUM(dr.drv)/100) * (st.suppliers1_value+st.suppliers2_value+st.suppliers3_value+st.suppliers4_value)+((SUM(dr.drv)/100) * st.circuit_value )),2) as film_hire_total
FROM com_pro_dr as dr
INNER JOIN com_pro_ratecard_rates AS rt
ON (dr.movie_id=rt.movie_id and
dr.theater_id=rt.theater_id and
dr.showtime_id=rt.showtime_id and
dr.category_id=rt.category_id and
dr.applied_date = rt.date_apply)
INNER JOIN com_pro_ratecard as rc ON (rc.id=rt.ratecard_id)
INNER JOIN com_pro_movie as m ON (m.id=dr.movie_id)
INNER JOIN com_pro_theater as t ON (t.id=dr.theater_id)
INNER JOIN com_pro_share as st ON (st.id=rc.share_id)
INNER JOIN com_pro_theater_dr as td ON (td.theater_id=dr.theater_id)
INNER JOIN com_pro_circuit as c ON (c.id =td.circuit_id)
WHERE 1
You can always do something like this:
SELECT SUM(val)
FROM
(
SELECT (SUM(dr.drv)/100)*
(st.suppliers1_value+st.suppliers2_value+st.suppliers3_value+st.suppliers4_value)
AS val
/* the rest of your current query */
) AS a