Left Join to another select statement - mysql

I have an INNER JOIN query that runs perfectly fine by itself, which I want to LEFT JOIN to a another query/SELECT statement which contains WHERE clause.
I'm not able to join both the queries. It should link wt.tkinit = t.tkinit
Can you please suggest what I'm missing.
SELECT c.clnum, m.mmatter, ot.tkinit AS 'otkinit', wt.tkinit AS 'wtikint', t.tkrt01,
SUM(mt.mthrwkdb) AS 'whrs2010',
FROM client c, matter m, timekeep ot, timekeep wt, mattimhs mt, periodt p, timerate t
WHERE c.clnum = m.mclient
AND m.mmatter = mt.mtmatter
GROUP BY c.clnum, m.mmatter, ot.tkinit, wt.tkinit
SELECT t.tkinit, t.tkrt01
FROM timerate t
INNER JOIN (
SELECT tkinit, max(tkeffdate) as max_effdate
FROM timerate WHERE DATEPART(year, tkeffdate) = '2012'
GROUP BY tkinit) mt ON mt.tkinit = t.tkinit AND mt.max_effdate = t.tkeffdate

Did you try the following:
SELECT fields from (SELECT1) s1
LEFT JOIN
(SELECT2) s2
on s1.wt.tkinit = s2.t.tkinit.
Don't forget to get normal name for wt.tkinit and t.tkinit in their selects like wt_tkinit and t_tkinit because you can't do double aliases.

SELECT temp2.tkinit, temp2.tkrt01, temp.clnum, temp.mmatter, temp.otkinit, temp.tkrt01, temp.whrs2010
FROM
( SELECT c.clnum, m.mmatter, ot.tkinit AS 'otkinit', wt.tkinit AS 'wtikint', t.tkrt01,
SUM(mt.mthrwkdb) AS 'whrs2010',
FROM client c, matter m, timekeep ot, timekeep wt, mattimhs mt, periodt p, timerate t
WHERE c.clnum = m.mclient
AND m.mmatter = mt.mtmatter
GROUP BY c.clnum, m.mmatter, ot.tkinit, wt.tkinit ) temp
LEFT JOIN
SELECT t.tkinit, t.tkrt01
FROM timerate t
INNER JOIN (
SELECT tkinit, max(tkeffdate) as max_effdate
FROM timerate WHERE DATEPART(year, tkeffdate) = '2012'
GROUP BY tkinit) mt ON mt.tkinit = t.tkinit AND mt.max_effdate = t.tkeffdate ) temp2
ON temp.wtikint = temp2.tkinit
Be careful of your field names, but this may be a start

Related

How can i rewrite this sql script without using exists

I am new to sql. How can i rewrite the below script without using exists
select
distinct oe.*,
o.*,
so.*,
ro.*
from
ms_bvoip_order_extension oe
inner join ms_order o on oe.ms_order_id = o.ms_order_id
inner join ms_sub_order so on so.ms_order_id = o.ms_order_id
inner join ms_job j on j.entity_id = so.ms_sub_order_id
inner join ms_task t on t.wf_job_id = j.wf_job_id
where
o.order_type = 900
and o.entered_date between to_date('12/01/2018 00:00:00', 'mm/dd/yyyy hh24:mi:ss')
and to_date('12/31/2018 00:00:00', 'mm/dd/yyyy hh24:mi:ss')
and j.entity_type = 5
and exists (
select
'X'
from
ms_task t
where
(
(t.name like '%Error%')
or (t.name like '%Correct%')
or (t.name = '%Create AOTS Ticket%')
)
and t.job_id = hextoraw(j.wf_job_id)
)
order by
o.usrp_order_number;
As Jerry mentioned, you can use EXISTS but if you don't want to you could JOIN the same EXISTS subquery:
INNER JOIN (
SELECT DISTINCT hextoraw(j.wf_job_id) JOB_ID
FROM ms_task
WHERE t.name like '%Error%'
or t.name like '%Correct%'
or t.name like '%Create AOTS Ticket%'
) TASK ON t.job_id = TASK.JOB_ID
You could probably remove your other ms_task table and JOIN ON j.wf_job_id......

Distinct is not working in sql

Select Distinct _Ad.ad_id,_Ad.Ad_Name,ID.Image_Path,VM.year,VD.Vehicle_Transformation,VD.Vehicle_Fuel_Type,VD.Vehicle_Millege
from _Ad
inner join _Image_Details ID
on ID.ad_id = _Ad.ad_id
inner join _Vehicle_Model VM
on VM.vehicle_model_id = _AD.vehicle_model_id
inner join _Vehicle_Details VD
on _ad.ad_id=VD.ad_id
inner join _Vehicle_Make VMA
on VM.vehicle_make_id=VMA.vehicle_make_id
where
VMA.Vehicle_Make='' OR
VM.Vehicle_Model='' OR
VD.Vehicle_Fuel_Type='' OR
VD.Vehicle_Seats='' OR
_Ad.price>'0' OR _Ad.price<'8888888'
i am using this query but its showing 4 records not 1. I need only single record across 1 ad_id. Kindly help.
Instead of SELECT DISTINCT, try using SELECT TOP 1. So your code would look like
SELECT TOP 1 a.ad_id,
a.Ad_Name,
b.Image_Path,
c.year,
d.Vehicle_Transformation,
d.Vehicle_Fuel_Type,
d.Vehicle_Millege
FROM _Ad a, _Image_Details b,
_Vehicle_Model c,
_Vehicle_Details d,
_Vehicle_Make e
WHERE b.ad_id = a.ad_id
AND c.vehicle_model_id = a.vehicle_model_id
AND a.ad_id = d.ad_id
AND c.vehicle_make_id = e.vehicle_make_id
e.Vehicle_Make='' OR
c.Vehicle_Model='' OR
d.Vehicle_Fuel_Type='' OR
d.Vehicle_Seats='' OR
a.price>'0' OR a.price<'8888888'
If you want one of each a.ad_id as you say, you will need to get rid of the b.Image_Path. So it would look like this:
SELECT DISTINCT a.ad_id,
a.Ad_Name,
c.year,
d.Vehicle_Transformation,
d.Vehicle_Fuel_Type,
d.Vehicle_Millege
FROM _Ad a, _Image_Details b,
_Vehicle_Model c,
_Vehicle_Details d,
_Vehicle_Make e
WHERE b.ad_id = a.ad_id
AND c.vehicle_model_id = a.vehicle_model_id
AND a.ad_id = d.ad_id
AND c.vehicle_make_id = e.vehicle_make_id
e.Vehicle_Make='' OR
c.Vehicle_Model='' OR
d.Vehicle_Fuel_Type='' OR
d.Vehicle_Seats='' OR
a.price>'0' OR a.price<'8888888'

MySQL UNION where clause condition from the first select

How can union two selections of the same tables but the second select condition depends on the first select attribute. Here is my query
SELECT *
FROM tbl_preference_header h
LEFT JOIN tbl_preference_detail d
OJ h.id = d.superid
WHERE h.type = 'CP'
UNION
SELECT *
FROM tbl_preference_header h2
LEFT JOIN tbl_preference_detail d2
ON h2.id = d2.superid
WHERE h2.type = 'GP' AND d2.cat3code NOT IN (d.cat3code)
What I want is in the second select statement it will not contain all the cat3code from first select statement. There is error in my query d is not recognized in the second select statement.
How can I accomplish this ? What another method can I use other than union ?
You won't be able to reference the original query directly, but you could bring the original query into a subquery as follows:
SELECT * FROM tbl_preference_header h left join tbl_preference_detail d on h.id = d.superid where type = 'CP'
union
select *
from
tbl_preference_header h2
left join tbl_preference_detail d2 on h2.id = d2.superid
where type = 'GP' and d2.cat3code not in (
select d.cat3code
from
tbl_preference_header h
left join tbl_preference_detail d on h.id = d.superid
where type = 'CP'
)
It is a bit hard to figure out exactly what you want. If I assume that type is part of tbl_preference_detail, then your query is equivalent to:
SELECT *
FROM tbl_preference_header h left join
tbl_preference_detail d
on h.id = d.superid
WHERE d.type = 'CP' or
(d.type = 'GP' and
not exists (select 1
from tbl_preference_detail d2
where d2.cat3code = d.cat3code and
d2.type = 'CP'
)
)

How to deduct in group by query in sum in access

I have query like this ::
SELECT account.AccountNumber, account.NAME, Sum(agro.price * agro.qty) AS Expr1
FROM ((account
INNER JOIN data ON account.AccountNumber = data.acno)
INNER JOIN agro ON agro.BillNo = data.BillNo)
WHERE data.db='true'
GROUP BY account.AccountNumber, account.NAME;
I want to deduct another groupby query output in to Sum(agro.price * agro.qty) this
the another group by query is SELECT Sum(rs),acno
FROM jma group by acno;
i want to deduct Sum(agro.price * agro.qty)-Sum(rs) how its work please help me solve this
If I am understanding you correctly the following query may work for you:
SELECT subQ.AccountNumber, subQ.NAME, (subQ.subSum - jmaSum.jSum) AS FinalSum
FROM
(
SELECT a.AccountNumber, a.NAME, Sum(ag.price * ag.qty) AS subSum
FROM (account AS a
INNER JOIN data AS d ON a.AccountNumber = d.acno)
INNER JOIN agro AS ag ON ag.BillNo = d.BillNo
WHERE d.db = 'true'
GROUP BY a.AccountNumber, a.NAME
) AS subQ
LEFT JOIN
(
SELECT Sum(j.rs) AS jSum, j.acno
FROM jma AS j
GROUP BY j.acno
) AS jmaSum ON subQ.AccountNumber = jmaSum.acno

Trying to add one last SUM() column to my query in SQL Server 2008

I have the first query which is producing correct results. What I need is I need to add the sum of values as a last column grouped by surveyid. I can't insert Sum(c.value) into the first query because it is an aggregate function. I have the correct query as my second query below. I know there's pivot functionality but not sure if it can be used here. I do realize that there will be repetition but that's okay.
'first query
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question
FROM
dbo.Survey A, dbo.Patient B, [dbo].[SurveyQuestionAnswer] C, [dbo].[LookupQuestions] D
WHERE
A.PATIENTID = B.ID
and c.SurveyID = A.ID
and c.QuestionID = d.ID
and c.questionid <> 10
ORDER BY
A.PATIENTID
'second query
select
c.surveyid,SUM(c.value) as scores
from
dbo.SurveyQuestionAnswer c
group by
c.SurveyID
order by
SurveyID '---not important
You can use SUM if you add the OVER clause. In this case:
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question,
SUM(c.Value) OVER(PARTITION BY c.surveyid) scores
FROM
dbo.Survey A
INNER JOIN dbo.Patient B
ON A.PATIENTID = B.ID
INNER JOIN [dbo].[SurveyQuestionAnswer] C
ON c.SurveyID = A.ID
INNER JOIN [dbo].[LookupQuestions] D
ON c.QuestionID = d.ID
WHERE
c.questionid <> 10
ORDER BY
A.PATIENTID
You could use something like this:
SELECT
s.PATIENTID, p.STUDENTNUMBER, sqa.surveyid,
CONVERT(varchar, s.CreatedDate, 107),
sqa.QuestionID, sqa.Value, lq.Question,
Scores = (SELECT SUM(Value) FROM dbo.SurveyQuestionAnswer s2 WHERE s2.SurveyID = s.ID)
FROM
dbo.Survey s
INNER JOIN
dbo.Patient p ON s.PatientID = p.ID
INNER JOIN
[dbo].[SurveyQuestionAnswer] sqa ON sqa.SurveyID = s.ID
INNER JOIN
[dbo].[LookupQuestions] lq ON sqa.QuestionID = lq.ID
WHERE
sqa.questionid <> 10
ORDER BY
s.PATIENTID
By having a subquery with the SUM(...) you should be able to get that sum as a single value and you don't need to use any grouping function