Pleas help me on why COUNT condition on WHERE statement commits an error, and how could i fix it?.
SELECT jq.taskqueueid,jq.jobid
FROM (SELECT p.taskID `curentTaskID`,
p.taskName `currentTaskName`,
p.processingType `currentProcessingType`,
p1.taskID `prevTaskID`,
p1.taskName `prevTaskName`,
p1.processingType `prevProcessingType`
FROM projecttask p
LEFT JOIN projecttask p1
ON p.sequenceNo=p1.nextTaskSequence
AND p.projectID=p1.projectID
WHERE p.taskID=18) task
INNER JOIN taskslogs tl
ON tl.taskID=task.`prevTaskID`
AND tl.statusDefinitionID=1
INNER JOIN jobqueue jq
ON tl.taskqueueid=jq.taskqueueid
WHERE COUNT(jq.taskqueueid)=COUNT(tl.taskqueueid)
to use an aggregate function like COUNT() you need to do a grouping of data, if you want to use it as a condition you can't use WHERE for this, since WHERE conditions are considered before aggregation. Use GROUP BY with HAVING instead. (see also http://dev.mysql.com/doc/refman/5.0/en/select.html )
Related
I have the following SQL query for MySQL:
SELECT SQL_CALC_FOUND_ROWS objects.objects_no
FROM objects
LEFT JOIN finds ON (objects.objects_no = finds.objects_no)
LEFT JOIN ceramics ON (objects.objects_no = ceramics.objects_no)
WHERE 1=1
and (objects.objects_no) in (select DISTINCT objects_no from objects_materials where thesaurus_term_id in (18658))
and (objects.objects_no) in (select DISTINCT objects_no from objects_objects where thesaurus_term_id in (24193))
GROUP BY objects.objects_no
ORDER BY objects.objects_no
Instead of getting results that match both subqueries, I also get results that match one or the other. Does anyone have an idea why that is?
Thanks, Sandro
Try parenthesizing the conditions.
WHERE (
(1=1)
and ((objects.objects_no) in (select DISTINCT objects_no from objects_materials where thesaurus_term_id in (18658)))
and ((objects.objects_no) in (select DISTINCT objects_no from objects_objects where thesaurus_term_id in (24193)))
)
Thanks for all your help. It actually does work just fine. There was a problem within the data inside the thesaurus.
Sorry!!!
get this error when i am using oracle, but it works fine in mysql
left outer join
daybreak.contract_details expiring on
expiring.acd_id =
(select
max(lastexpire.acd_id)
from
daybreak.contract_details lastexpire
where lastexpire.acd_aad_id =acc_aad_id and
lastexpire.acd_itemization_tcd_code in ('IIN_5') and
lastexpire.acd_expiry_dt between to_date('2014-01-01','yyyy-mm-dd')
and to_date('2014-02-01','yyyy-mm-dd') and
lastexpire.acd_term > 0
)
thanks
What exactly is your question?
You cannot specify a subquery in the join condition of an outer join in Oracle. Doing so will result in an ORA-1799 error.
It's almost always possible to rewrite such a query though. For example, you could:
left outer join ( SELECT acd_aad_id, max(acd_id) max_acd_id FROM daybreak.contract_details lastexpire WHERE
lastexpire.acd_itemization_tcd_code in ('IIN_5') and
lastexpire.acd_expiry_dt between to_date('2014-01-01','yyyy-mm-dd')
and to_date('2014-02-01','yyyy-mm-dd') and
lastexpire.acd_term > 0 ) max_ids ON max_ids.acd_aad_id = aac_aad_id
left outer join
daybreak.contract_details expiring on
expiring.acd_id = max_ids.acd_aad_id
... the idea being get the value of the subquery for every possible value in an inline view and then outer-join to that.
In Oracle 12c, you could use a LATERAL inline view for faster-performing variants of that approach.
An alternate approach would be to just outer join to all the values of daybreak.contract_details.acd_id, not just the max() value, and then filter out any values that aren't the maximum, later on in the query (keeping in mind that some of the values might be null (i.e., if there were no matches in contract_details at all).
I have used the following query:
select tblclass.classname,tblattendance.id
from tblclass,tblattendance
where tblclass.classcode=tblattendance.classcode
and tblattendance.attdate='2013-07-01'
Output of this query is as follows:
Now what I want is rather than the above result I want count of different classes like IB-2,IC-5.
Please tell me what modifications do I need to made in my query to get the desired result
Use the Group By SQL clause and add the aggregate function Count
select tblclass.classname, Count(tblattendance.id) as counter
from tblclass,tblattendance
where tblclass.classcode=tblattendance.classcode and tblattendance.attdate='2013-07-01'
group by tblclass.classname
Try this
select count(tblattendance.id),tblclass.classname from tblclass,tblattendance
where tblclass.classcode=tblattendance.classcode and tblattendance.attdate='2013-07-01'
group by tblclass.classname
Use COUNT() function for that with GROUP BY. Also use JOIN.
SELECT tc.classname, COUNT(tc.classname) AS COUNTS
FROM tblclass tc
JOIN tblattendance tt
ON tc.classcode = tt.classcode
WHERE tt.attdate='2013-07-01'
GROUP BY tc.classname
can anyone help me on why the WHERE clause if not functioning? And what is the alternative solution on it?
SELECT jq.jobid
FROM jobqueue jq
LEFT JOIN taskslogs tl
ON jq.taskqueueid=tl.taskqueueid
LEFT JOIN (SELECT p1.taskID,p1.processingType
FROM projecttask p
LEFT JOIN projecttask p1
ON p.sequenceNo=p1.nextTaskSequence
AND p.projectID=p1.projectID
WHERE p.taskID=paramTaskID) prevTask
ON tl.taskID=prevTask.taskID
WHERE IF(prevTask.processingType='BATCH',jq.batchid!=0,IF(prevTask.processingType='DOCGROUP',jq.documentgroupid!=0,TRUE))
GROUP BY jq.jobid
Maybe something like this:
WHERE
(prevTask.processingType='BATCH' AND jq.batchid!=0)
OR (prevTask.processingType='DOCGROUP' AND jq.documentgroupid!=0)
I don't think you can use IF statements in a query. That is more for procedures.
You should probably look at using case statements.
http://dev.mysql.com/doc/refman/5.0/en/case-statement.html
I'm trying to use the following query, and if it only has one having statement it works as expected. If I add the second having statement it does not work.
SELECT candidate.first_name,
candidate.last_name,
qualification.code,
property.value AS funding_band_value,
qualification.funding_band,
property.value AS qualification_level_value,
qualification.qualification_level_id
FROM candidate_qualification, candidate, qualification, property
WHERE candidate_qualification.candidate_id=candidate.id and
candidate_qualification.qualification_id=qualification.id
HAVING funding_band_value = (select property.value from property where qualification.funding_band=property.id) and
HAVING qualification_level_value = (select property.value from property where qualification.qualification_level_id=property.id)
Could someone explain why this doesn't work and how I should do this.
HAVING acts similarly to WHERE or GROUP BY. You reference it once to start using it and combine multiple statements with AND or OR operators. An in depth look at the query parser might give you a more explicit answer.
You don't need HAVING here, just use AND so it is part of your WHERE clause.
The subqueries are not necessary, those tables are already joined.
Something like this should be closer to what you want:
SELECT c.first_name,
c.last_name,
q.code,
p.value AS funding_band_value,
q.funding_band,
p.value AS qualification_level_value,
q.qualification_level_id
FROM candidate_qualification cq
INNER JOIN candidate c ON cq.candidate_id=c.id
INNER JOIN qualification q ON cq.qualification_id=q.id
INNER JOIN property p ON q.funding_band=p.id
and q.qualification_level_id=p.id