MySQL query - multiple having statements not working - mysql

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

Related

MAX(Date) is giving empty result

I have a table with exchange rate like below
And I am using the maxofdate to pick all these values based on currency code. But the query is giving blank.
Select USDAMOUNT * dbo.EXCHANGERATEAMT
from dbo.Amount_monthly
Left Join dbo.EXCHANGERATE on dbo.Amount_monthly.Currencycode=dbo.EXCHANGERATE.fromcurrencycode
WHERE ValidToDateTime = (Select MAX(ValidToDateTime) from dbo.EXCHANGERATE)
AND dbo.EXCHANGERATE.EXCHANGERATETYPECODE = 'DAY'
Using this statement
CONVERT(DATE,ValidToDateTime) = CONVERT(DATE,GETDATE()-1)
instead of subquery is giving me expected result.
Can someone correct this.
thanks in advance.
If I understand correctly, you need two things. First, the condition for the max() needs to match the condition in the outer query. Second, if you really want a left join, then conditions on the second table need to go in the on clause.
The resulting query looks like:
Select . . .
from dbo.Amount_monthly am Left Join
dbo.EXCHANGERATE er
on am.Currencycode = er.fromcurrencycode and
er.ValidToDateTime = (Select max(er2.ValidToDateTime)
from dbo.EXCHANGERATE er2
where er2.EXCHANGERATETYPECODE = 'DAY'
) and
er.EXCHANGERATETYPECODE = 'DAY';
I would write this using window functions, but that is a separate issue.
Try removing WHERE clause for ValidToDateTime and include it in the JOIN as AND condition
SELECT USDAMOUNT * dbo.EXCHANGERATEAMT
FROM dbo.Amount_monthly
LEFT JOIN dbo.EXCHANGERATE
ON dbo.Amount_monthly.Currencycode = dbo.EXCHANGERATE.fromcurrencycode
AND ValidToDateTime = (SELECT MAX(ValidToDateTime) --remove WHERE clause
FROM dbo.EXCHANGERATE)
AND dbo.EXCHANGERATE.EXCHANGERATETYPECODE = 'DAY';
I cleaned up your query a bit: as the other folks mentioned you needed to close the parentheses around the MAX(Date) sub-query, and if you reference a LEFT JOINed table in the WHERE clause, it behaves like an INNER JOIN, so I changed to in INNER. You also had "dbo" sprinkled in as a field prefix, but that (the namespace) only prefixes a database, not a field. I added the IS NOT NULL check just to avoid SQL giving the "null values were eliminated" SQL warning. I used the aliases "am" for the first table and "er" for the 2nd, which makes it more readable:
SELECT am.USDAMOUNT * er.EXCHANGERATEAMT
FROM dbo.Amount_monthly am
JOIN dbo.EXCHANGERATE er
ON am.Currencycode = er.fromcurrencycode
WHERE er.ValidToDateTime = (SELECT MAX(ValidToDateTime) FROM dbo.EXCHANGERATE WHERE ValidToDateTime IS NOT NULL)
AND er.EXCHANGERATETYPECODE = 'DAY'
If you're paranoid like I am, you might also want to make sure the exchange rate is not zero to avoid a divide-by-zero error.

Nesting COUNT in statement with JOIN

Really trying to figure out, why SQL query doesnt go through. I assume the structure is a bit wrong, but cant figure out where exactly. The references to tables are all correct.
SELECT tap_questionnaires.id,
tap_questionnaires.NAME,
tap_questionnaires.active,
tap_useranswers_ip.questionnaire_id,
Count(tap_useranswers_ip.ip)
FROM tap_questionnaires
LEFT JOIN tap_useranswers_ip
ON tap_questionnaires.id = tap_useranswers_ip.questionnaire_id
WHERE author_email = admin#admin.com
If you use count you need to use group by for the other columns in your select clause.
SELECT TAP_questionnaires.id, TAP_questionnaires.name, TAP_questionnaires.active, TAP_useranswers_ip.questionnaire_id, COUNT(TAP_useranswers_ip.ip) FROM TAP_questionnaires LEFT JOIN TAP_useranswers_ip on TAP_questionnaires.id=TAP_useranswers_ip.questionnaire_id WHERE author_email="admin#admin.com"
group by TAP_questionnaires.id, TAP_questionnaires.active
I think TAP_questionnaires.name it's not necessary because I suppose it depends on TAP_questionnaires.id. TAP_useranswers_ip.questionnaire_id is the same value as TAP_questionnaires.id
Hope that helps!
I think this version is clearer:
SELECT q.id, q.name, q.active, COUNT(a.ip)
FROM TAP_questionnaires q LEFT JOIN
TAP_useranswers_ip a
ON on q.id = a.questionnaire_id
WHERE author_email = 'admin#admin.com'
GROUP BY q.id, q.name, q.active;
Notes:
You need a GROUP BY.
You need single quotes around the string constant.
Table aliases make the query easier to write and to read.
There is no reason to include a.questionnaire_id. You already have q.id.

Magento - SQL query programmatically

I have a SQL query that i would like to write with Magento's collection methods but i don't know how to.
I know that i have to use the getSelect() and joinLeft() methods, but don't know how to put a select inside a left join.
The query is :
SELECT
p.photo_id,
p.photo_name,
s.step_id,
s.step_name
FROM Photo p
LEFT JOIN (
SELECT
photo_id, MAX(step_id) AS max_step_id
FROM photoStep
GROUP BY photo_id
) ps
ON ps.photo_id = p.photo_id
LEFT JOIN Steps s
ON s.step_id = ps.max_step_id
I think that your best option here would be to try to avoid using the subquery. So I would start by reworking the SQL query and then use the functions groupByField & addExpressionFieldToSelect.
->groupByField('photo_id')
->addExpressionFieldToSelect("max_step_id", 'MAX({{step_id}})', 'step_id')

MySQL COUNT on WHERE Statement

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 )

Using an "AS" clause in a sub-query than contains a "WHERE" clause

I want to store result in the variable generated by using "AS" clause in the MS Access,
and use this result in the sub-query with WHERE clause.
I tried this:
SELECT en_date AS date_en, (select sum(amount)
from main where
CrDb='Cr'
and
en_date=date_en) AS CR_AMT
FROM main
GROUP BY en_date;
I'm fairly certain you can't use an alias (the AS destination) in the same SELECT you defined it in.
I can't tell quite what you're trying to do, but it kind of looks like you want to be joining the table to itself.
SELECT
en_date,
SUM(amount)
FROM
main a
INNER JOIN
(
SELECT
en_date AS date_en,
CrDb
FROM
main
WHERE
CrDb='Cr'
)b
ON
a.en_date = b.date_en
AND a.CrDb = b.CrDb
GROUP BY
en_date
select m.en_date date_en, sum(m.amount)
from main m
where CrDb = 'Cr'
group by m.en_date
In other words, I dont think you even need a sub-query to get the results you are looking for.