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
Related
I am having 3 tables named customers,cars,carrent. All i want is to multiply rentorder.days with cardetail.rentday and show the value in rentorder.totalrent. I am unable to achieve this. How can i do this.
Any suggestions please.
SQL
SELECT customers.*,
cardetail.carname,
cardetail.model,
cardetail.company,
cardetail.color,
cardetail.rentday,
rentorder.days,
rentorder.totalrent
FROM rentorder
INNER JOIN customers
ON customers.custid = rentorder.custid
INNER JOIN cardetail
ON cardetail.id = rentorder.carid
Just multiply rentday with days to get the computed value totalrent in Select list.
Try this
SELECT customers.*, -- Not sure this is allowed in [Mysql]
cardetail.carname,
cardetail.model,
cardetail.company,
cardetail.color,
cardetail.rentday,
rentorder.days,
cardetail.rentday * rentorder.days AS totalrent
FROM rentorder
INNER JOIN customers
ON customers.custid = rentorder.custid
INNER JOIN cardetail
ON cardetail.id = rentorder.carid
To save the data you need to use update from Inner Join syntax
update rentorder
INNER JOIN customers
ON customers.custid = rentorder.custid
INNER JOIN cardetail
ON cardetail.id = rentorder.carid
SET
rentorder.totalrent = cardetail.rentday * rentorder.days
I have been working on the mimic II database and I don't understand how to use joins between 3 tables.
Currently I have:
SELECT p.hospital_expire_flg,
COUNT (*)
FROM poe_med m, poe_order o, d_patients p
WHERE m.poe_id=o.poe_id
AND o.subject_id=p.subject_id
AND drug_name_generic = 'Metoprolol'
GROUP BY p.hospital_expire_flg
I'm guessing I have to use JOIN AS for the 3 tables. But not sure how to.
You can do:
SELECT p.hospital_expire_flg
FROM poe_med `m`
INNER JOIN poe_order `o`
ON m.poe_id = o.poe_id
INNER JOIN d_patients p
ON o.subject_id = p.subject_id
WHERE drug_name_generic = 'Metroprolol';
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.
Using this query
SELECT DISTINCT(job_primary.id) AS id
FROM `job_primary`
LEFT JOIN `job_skill` ON `job_skill`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_facts` ON `job_facts`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_location` ON `job_location`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_people` ON `job_people`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_sec` ON `job_sec`.`job_id` = `job_primary`.`id`
AND job_sec.exp_start_date > UNIX_TIMESTAMP()
INNER JOIN `user_skills` ON `user_skills`.`skill_id` = `job_skill`.`skill_id`
AND user_skills.user_id = 1
WHERE job_primary.posted_by != 1
I am getting only the id value. But I want to get the rest of the values with distinct id. Don't think about rest of the codes. The problem is here, if I use:
select *, job_primary.id as id
to select code then I get all data. But it is not distinct. So if I used
SELECT *, distinct(job_primary.id) as id
or
SELECT distinct(job_primary.id), * as id
but it is showing errors.
I think your confussion is what to query itself, nixon1333. What do you want to ask to the database? If you want just one row per id, group'em by with SELECT * FROM ... GROUP BY job_primary.id
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