I have 3 different table named job_party, job_party_details and job_party_delv. In which job_party is the main table and others are detailed tables. I am trying to gather data date by date from all these tables. I wrote the following query and getting perfect data but the problem is data of job_party_details comes first and job_party_delv comes latter. I want all data at once as per the date.
SELECT job_party.on_date
, SUM(job_party_details.qty) as detail_qty
, NULL as delv
FROM job_party_details d
JOIN job_party p
on d.jp_id = p.id
where p.party_id = 9
and d.i_id = 1
GROUP
BY p.on_date
UNION
SELECT p.on_date
, NULL as detail_qty
, SUM(d.d_qty) as delv
FROM job_party_delv d
JOIN job_party p
on d.jp_id = p.id
where p.party_id = 9
and d.i_id = 1
GROUP
BY p.on_date
Replace NULL with 0 then make the query as a sub-query and perform another SUM on the outer query GROUP BY on_date like this:
SELECT on_date,SUM(detail_qty) as detail_qty, SUM(delv) as delv
FROM
(SELECT job_party.on_date
, SUM(job_party_details.qty) as detail_qty
, 0 as delv
FROM job_party_details d
JOIN job_party p
on d.jp_id = p.id
where p.party_id = 9
and d.i_id = 1
GROUP
BY p.on_date
UNION
SELECT p.on_date
, 0 as detail_qty
, SUM(d.d_qty) as delv
FROM job_party_delv d
JOIN job_party p
on d.jp_id = p.id
where p.party_id = 9
and d.i_id = 1
GROUP
BY p.on_date) A GROUP BY on_date;
Related
I am writing a mysql select sub query, that is working fine, it returns 2 column is there way to select only 1 column.
My query is
SELECT sum(fl.qunt) as qunt,(
SELECT GROUP_CONCAT( xp.id
SEPARATOR ',' )
FROM prdt AS xp
LEFT JOIN prdt_fac AS pf ON pf.fk_product_children = xp.rowid
WHERE pf.prdt_fat = p.id
AND pf.prdt_ch = 6953
GROUP BY pf.prdt_fat
LIMIT 0 , 1
) AS prdt_chd
FROM fac_log AS fl
LEFT JOIN fac AS f ON fl.fac = f.id
LEFT JOIN prdt AS p ON f.prdt = p.id
GROUP BY prod_child
ORDER BY fl.tms DESC
LIMIT 0 , 1
This return two column qunt and prdt_ch.
But i want only column in result qunt.
Is there any way, becuase inner select query is must be used to get correct result.
Main purpose of this query is that i have to use this query as sub query in an other query, in this condition it throws error "operand should contain 1 column"
Thanks in advance
just select qunt using your select query as a table (t)
select qunt from (
SELECT sum(fl.qunt) as qunt,(
SELECT GROUP_CONCAT( xp.id
SEPARATOR ',' )
FROM prdt AS xp
LEFT JOIN prdt_fac AS pf ON pf.fk_product_children = xp.rowid
WHERE pf.prdt_fat = p.id
AND pf.prdt_ch = 6953
GROUP BY pf.prdt_fat
LIMIT 0 , 1
) AS prdt_chd
FROM fac_log AS fl
LEFT JOIN fac AS f ON fl.fac = f.id
LEFT JOIN prdt AS p ON f.prdt = p.id
GROUP BY prod_child
ORDER BY fl.tms DESC
LIMIT 0 , 1 ) t
Hi all I am having 3 tables as follows
Technology_table
Technology_ID Technology_Name
10 Asp.Net
20 C#
Question_table
QUESTION_ID Technology_ID QUESTION_DESCRIPTION
1 10 First ques in Asp.net
2 20 First ques in C#
Reply_table
QUESTION_ID Technology_ID Reply_Date Reply_Message
1 10 2016-01-23 I am first to post
1 10 2016-01-24 I am second to post
I have written the following query but not getting the result as expected
select
FI.QUESTION_ID,FI.QUESTION_TITLE,FI.USER_NAME,FI.DATE_POSTED,
FI.[DATE_REPLIED],FI.RepliedName,FI.VIEW_COUNT,FI.REPLY_COUNT,
FI.REPLY_MESSAGE,TT.TECHNOLOGY_ID,TT.TECHNOLOGY_NAME
from FORUM_TECHNOLOGY TT,
( select distinct
TQ.TECHNOLOGY_ID,TQ.QUESTION_ID,TQ.QUESTION_TITLE,TQ.USER_NAME,
TQ.DATE_POSTED,
TR.[DATE_REPLIED],
TR.USER_NAME as RepliedName,
TQ.VIEW_COUNT,TQ.REPLY_COUNT,TR.REPLY_MESSAGE
from FORUM_QUESTIONS TQ
LEFT OUTER JOIN FORUM_REPLIES TR
ON TR.TECHNOLOGY_ID=TQ.TECHNOLOGY_ID
and TR.QUESTION_ID = TQ.QUESTION_ID
and TR.[DATE_REPLIED] in (
select MAX(TR.[DATE_REPLIED])
from FORUM_REPLIES TR
group by TR.QUESTION_ID
)
) FI
where FI.TECHNOLOGY_ID =TT.TECHNOLOGY_ID
and TT.TECHNOLOGY_ID = #TechID
I also tried in this way
select t1.QUESTION_ID,oa.USER_NAME,oa.REPLY_MESSAGE
from FORUM_QUESTIONS t1
cross apply(select top 1 * from FORUM_REPLIES
where QUESTION_ID = t1.QUESTION_ID
order by DATE_REPLIED desc)oa
join FORUM_TECHNOLOGY t2 on oa.TECHNOLOGY_ID = t2.TECHNOLOGY_ID
AND oa.QUESTION_ID = t1.QUESTION_ID
I would like to display only one instead of duplicates
Can some one help me
This assumes that your DATE_REPLIED has a valid time component and isn't just omitted or defaulted to midnight.
SELECT FQ.QUESTION_ID
, FQ.QUESTION_TITLE
, FQ.USER_NAME
, FQ.DATE_POSTED
, FR.DATE_REPLIED
, FR.RepliedName
, FQ.VIEW_COUNT
, FI.REPLY_COUNT
, FR.REPLY_MESSAGE
, TT.TECHNOLOGY_ID
, TT.TECHNOLOGY_NAME
FROM FORUM_TECHNOLOGY AS TT
INNER JOIN FORUM_QUESTION AS FQ
ON FQ.TECHNOLOGY_ID = TT.TECHNOLOGY_ID
LEFT OUTER JOIN (
SELECT QUESTION_ID
, COUNT(*) AS REPLY_COUNT
, MAX(DATE_REPLIED) AS DATE_REPLIED
FROM FORUM_REPLIES
GROUP BY QUESTION_ID
) AS FI
ON FI.QUESTION_ID = FQ.QUESTION_ID
INNER JOIN FORUM_REPLIES AS FR
ON FR.QUESTION_ID = FI.QUESTION_ID
AND FR.DATE_REPLIED = FI.DATE_REPLIED
Hi I'm having an issue with a query that was once working. My SQL skills aren't all that great, not sure what I'm missing. Or if this is the correct approach. Maybe use a temp table instead?
The basic gist is given a certain time frame, I need to calculate the highest aggregate of points over 5 classes.
trialScores - keeps scores/points,
trials, dog, people and member tables are just meta data
classId 5 requires a different date range
Here is my Query on MySQL
select
t.id,
d.id,
p.id,
p.firstname,
p.lastname,
d.id dogId,
d.dogName,
c.id,
c.class,
t.trialStartDate,
s.points,
if(c.id = 5, '2012-08-01', '2012-11-18') as startDate,
if(c.id = 5, '2013-07-31', '2013-12-31') as endDate,
SUM(ts.points) AS pointsAggregate
from trialScores ts
inner join trials t on t.id = ts.trialId
inner join dogs d on d.id = ts.dogId
inner join people p on p.id = ts.personId
inner join classes c on c.id = ts.classId
where t.deletedAt is null
and ts.deletedAt is null
and ts.memberAtTrial = 1
and d.omitFromTripleCrownDOY = 0
and t.associationId = 1
GROUP BY p.id, ts.dogId, ts.classId
having t.trialStartDate between startDate and endDate
order by ts.classId, pointsAggregate desc
Looks like this fixed it, too much in the select and not in Group by?:
select
d.dogName,
c.class,
p.firstName,
p.lastName,
SUM(ts.points) AS pointsAggregate
from trialScores ts
inner join trials t on t.id = ts.trialId
inner join dogs d on d.id = ts.dogId
inner join people p on p.id = ts.personId
inner join classes c on c.id = ts.classId
where t.deletedAt is null
and ts.deletedAt is null
and ts.memberAtTrial = 1
and d.omitFromTripleCrownDOY = 0
and t.associationId = 1
and t.trialStartDate between if(c.id = 5, '2012-08-01', '2012-11-18') and if(c.id = 5, '2013-07-31', '2013-12-31')
GROUP BY ts.dogId, ts.classId
order by ts.classId, pointsAggregate desc
Can you try below query and let if it's work or not ?
select
d.dogName,
c.class,
p.firstName,
p.lastName,
SUM(ts.points) AS pointsAggregate
from trialScores ts
inner join trials t on t.id = ts.trialId
inner join dogs d on d.id = ts.dogId
inner join people p on p.id = ts.personId
inner join classes c on c.id = ts.classId
where t.deletedAt is null
and ts.deletedAt is null
and ts.memberAtTrial = 1
and d.omitFromTripleCrownDOY = 0
and t.associationId = 1
and t.trialStartDate between if(c.id = 5, '2012-08-01', '2012-11-18') and if(c.id = 5, '2013-07-31', '2013-12-31')
GROUP BY ts.classId,p.firstName,p.lastName
order by ts.classId, pointsAggregate desc
The following query on my MySQL tables returns rows from the purchaseorder table that have corresponding entries in the deliveryorder table. How do I construct this query so that I get rows from the purchaseorder table even if no corresponding rows exist in the deliveryorder table? If the users want to see sql table CREATE statements, I can post those, but I'm not posting now as it really makes the question too big.
SELECT
`purchaseorder`.`id` AS `po_id`,
`purchaseorder`.`order_quantity` AS `po_order_quantity`,
`purchaseorder`.`applicable_approved_unit_rate` AS `po_unit_rate`,
`purchaseorder`.`applicable_sales_tax_rate` AS `po_tax_rate`,
`purchaseorder`.`order_date` AS `po_order_date`,
`purchaseorder`.`remarks` AS `po_remarks`,
`purchaseorder`.`is_open` AS `po_is_open`,
`purchaseorder`.`is_active` AS `po_is_active`,
`purchaseorder`.`approved_rate_id` AS `po_app_rate_id`,
`supplier`.`name` AS `sup_name`,
SUM(`deliveryorder`.`quantity`) AS `total_ordered`
FROM `purchaseorder`
LEFT JOIN `deliveryorder` ON (`deliveryorder`.`purchase_order_id` = `purchaseorder`.`id`)
INNER JOIN `approvedrate` ON (`purchaseorder`.`approved_rate_id` = `approvedrate`.`id`)
INNER JOIN `supplier` ON (`approvedrate`.`supplier_id` = `supplier`.`id`)
WHERE (
`purchaseorder`.`is_active` = 1
AND `purchaseorder`.`is_open` = 1
AND `deliveryorder`.`is_active` = 1
AND `approvedrate`.`material_id` = 2
)
HAVING `purchaseorder`.`order_quantity` >= `total_ordered` + 1
You have an aggregating function but no GROUP BY clause, which is wierd, but anyway - something like this? Oops - edited...
SELECT po.id po_id
, po.order_quantity po_order_quantity
, po.applicable_approved_unit_rate po_unit_rate
, po.applicable_sales_tax_rate po_tax_rate
, po.order_date po_order_date
, po.remarks po_remarks
, po.is_open po_is_open
, po.is_active po_is_active
, po.approved_rate_id po_app_rate_id
, s.name sup_name
, SUM(do.quantity) total_ordered
FROM purchaseorder po
LEFT
JOIN deliveryorder do
ON do.purchase_order_id = po.
AND do.is_active = 1
LEFT
JOIN approvedrate ar
ON ar.id = po.approved_rate_id
AND ar.material_id = 2
LEFT
JOIN supplier s
ON s.id = ar.supplier_id
WHERE po.is_active = 1
AND po.is_open = 1
HAVING po.order_quantity >= total_ordered + 1
I couldn't work out how to get the desired results all in one query, but ended up using the following two queries to fulfill my requirements: -
1st query
SELECT
pot.`id` AS `po_id`,
pot.`order_quantity` AS `po_order_quantity`,
pot.`applicable_approved_unit_rate` AS `po_unit_rate`,
pot.`applicable_sales_tax_rate` AS `po_tax_rate`,
pot.`is_open` AS `po_is_open`,
pot.`is_active` AS `po_is_active`,
st.`id` AS `sup_id`,
st.`name` AS `sup_name`,
SUM(dot.`quantity`) AS `total_ordered`
FROM `purchaseorder` pot
INNER JOIN `deliveryorder` dot ON (dot.`purchase_order_id` = pot.`id`)
INNER JOIN `approvedrate` art ON (pot.`approved_rate_id` = art.`id`)
INNER JOIN `supplier` st ON (art.`supplier_id` = st.`id`)
WHERE (
pot.`is_active` = 1
AND pot.`is_open` = 1
AND art.`material_id` = #materialid
AND art.`in_effect` = 1
AND art.`is_active` = 1
AND dot.`is_active` = 1
AND st.`is_active` = 1
)
HAVING pot.`order_quantity` >= `total_ordered` + #materialquantity
2nd query
SELECT
pot.`id` AS `po_id`,
pot.`order_quantity` AS `po_order_quantity`,
pot.`applicable_approved_unit_rate` AS `po_unit_rate`,
pot.`applicable_sales_tax_rate` AS `po_tax_rate`,
pot.`is_open` AS `po_is_open`,
pot.`is_active` AS `po_is_active`,
st.`id` AS `sup_id`,
st.`name` AS `sup_name`,
0 AS `total_ordered`
FROM `purchaseorder` pot
INNER JOIN `approvedrate` art ON (pot.`approved_rate_id` = art.`id`)
INNER JOIN `supplier` st ON (art.`supplier_id` = st.`id`)
WHERE (
pot.`is_active` = 1
AND pot.`is_open` = 1
AND art.`material_id` = #materialid
AND art.`in_effect` = 1
AND art.`is_active` = 1
AND st.`is_active` = 1
AND pot.`order_quantity` >= #materialquantity
AND pot.`id` NOT IN
(
SELECT dot.`purchase_order_id`
FROM `deliveryorder` dot
WHERE dot.is_active = 1
)
)
Something is wrong with my MySQL query below but I can't find the problem. It's not returning any errors but the query below should return 1 row, but it returns none.
The table 'fws_product' contains all products. The table 'webits_product_has_kenmerken' contains the product specifications.
SELECT fws_product.*
FROM webits_product_has_kenmerken
LEFT JOIN fws_product ON webits_product_has_kenmerken.product_id = fws_product.ID
WHERE fws_product.CATID = 11
AND (
(webits_product_has_kenmerken.kenmerk_id = 8 AND webits_product_has_kenmerken.kenmerk_value = 'Buddha to Buddha')
AND
(webits_product_has_kenmerken.kenmerk_id = 19 AND webits_product_has_kenmerken.kenmerk_value = '10 mm')
)
Thanks in advance!
It looks a bit nasty, but the following should do as you have requested
SELECT
p.*
FROM fws_product AS p
INNER JOIN webits_product_has_kenmerken AS ps8
ON ps8.product_id = p.ID
AND ps8.kenmerk_id = 8
AND ps8.kenmark_value = 'Buddha to Buddha'
INNER JOIN webits_product_has_kenmerken AS ps19
ON ps19.product_id = p.ID
AND ps19.kenmerk_id = 19
AND ps19.kenmark_value = '10 mm'
WHERE p.CATID = 11
This is another potential option which may do the job, but still feels very nasty
SELECT
p.*
FROM fws_product AS p
INNER JOIN (
SELECT
product_id,
COUNT(*) AS numMatches
FROM webits_product_has_kenmerken
WHERE (kenmerk_id,kenmerk_value) IN (
(8,'Buddha to Buddha'),
(19,'10 mm')
)
GROUP BY product_id
HAVING numMatches = 2
) AS ps
ON ps.product_id = p.ID
WHERE p.CATID = 11
i think you need the following:
SELECT fws_product.*
FROM webits_product_has_kenmerken
LEFT JOIN fws_product ON webits_product_has_kenmerken.product_id = fws_product.ID
WHERE fws_product.CATID = 11
AND (
(webits_product_has_kenmerken.kenmerk_id = 8 AND webits_product_has_kenmerken.kenmerk_value = 'Buddha to Buddha')
OR
(webits_product_has_kenmerken.kenmerk_id = 19 AND webits_product_has_kenmerken.kenmerk_value = '10 mm')
)
checke these columns for NULL values:
fws_product.CATID
webits_product_has_kenmerken.kenmerk_id
webits_product_has_kenmerken.kenmerk_value
every comparison with NULL will exclude the row from the reult