I am having trouble with my query below
SELECT DATE(jc.`date_in`) DAY,
CASE
WHEN jb.`modified_fr` <> 0 THEN ROUND(SUM(jb.`modified_fr`),1)
ELSE ROUND(SUM(jd.`fr1`),1)
END AS AmountOfHoursBooked
,ds.`name` AS dealership, d.`name` AS Department
FROM jobcard jc
LEFT JOIN job jb
ON jc.`id` = jb.`jobcard`
LEFT JOIN job_definition jd
ON jb.`job_definition` = jd.`id`
LEFT JOIN department d
ON jc.`department` = d.`id`
JOIN dealership ds
ON d.`dealership` = ds.`id`
WHERE MONTH(jc.`date_in`) = MONTH(CURRENT_DATE())
AND YEAR(jc.`date_in`) = YEAR(CURRENT_DATE())
AND jd.`fr1` <> 0
AND jc.`status` <> 5
GROUP BY DATE(jc.`date_in`),jc.`department`;
what i need it to do is if the case comes back true it should use that modified fr instead of the original fr from jb table ?
what am I missing ?
thanks in advance
Related
Error while creating table but the SQL is retrieving result
Please find the below code, the below SQL by itself is working fine retrieving results!
SELECT
CASE WHEN J.JOBID = DJ.JOBNUMBER
THEN DJ.JOBID ELSE J.JOBID
END AS JOBID,
J.DISTRICTID,
CASE WHEN D.DISTRICTID = J.DISTRICTID
THEN D.NAME ELSE NULL
END AS DISTRICT_NAME,
D.SEGMENTID,
CASE WHEN D.SEGMENTID = S.SEGMENTID AND D.DISTRICTID = J.DISTRICTID
THEN S.NAME ELSE NULL
END AS SEGMENT,
J.STARTTIME,
J.ENDTIME,
SJ.COMCATJOBTYPEID,
J.JOBTYPEID,
CASE WHEN J.JOBTYPEID = JT.JOBTYPEID
THEN JT.NAME ELSE NULL
END AS JOBTYPENAME,
TS.SSSEGMENT AS SUBPL_NAME,
CONCAT(D.DISTRICTID,TS.SSSEGMENT) AS DISTRICTSUBPLID
FROM `EAR-AA-242`.JOB J
LEFT JOIN `EAR-AA-242`.DISTRICT D ON J.DISTRICTID = D.DISTRICTID
LEFT JOIN `EAR-AA-242`.DMJOB DJ ON DJ.JOBNUMBER = J.JOBID
LEFT JOIN `EAR-AA-242`.JOBTYPE JT ON JT.JOBTYPEID = J.JOBTYPEID
LEFT JOIN `EAR-AA-242`.STANDARDJOBTYPE SJ ON JT.STANDARDJOBTYPEID = SJ.STANDARDJOBTYPEID
LEFT JOIN `EAR-AA-239`.TBLJOBTYPE TJ ON SJ.COMCATJOBTYPEID = TJ.JOBTYPEID
LEFT JOIN (SELECT SS.SSSEGMENT, SS.NODEID FROM `EAR-AA-239`.TBLSSSEGMENT S,
`EAR-AA-239`.TBLSSSEGMENT SS WHERE S.SSS = SS.PARENTSSS AND SS.DELETED = 0) TS ON TJ.SSSEGMENT =TS.NODEID
LEFT JOIN `EAR-AA-242`.SEGMENT S ON S.SEGMENTID = D.SEGMENTID
WHERE DATE(J.STARTTIME) > DATE_SUB(DATE(SYSDATE()), INTERVAL 367 DAY)
ORDER BY J.JOBID;
The same code code , while creating a table is throwing an
Error Code: 1292. Truncated incorrect DOUBLE value: '11COC0011'
CREATE TABLE ODS.JOB_LOCATION_IDISTRICT AS
SELECT
CASE WHEN J.JOBID = DJ.JOBNUMBER
THEN DJ.JOBID ELSE J.JOBID
END AS JOBID,
J.DISTRICTID,
CASE WHEN D.DISTRICTID = J.DISTRICTID
THEN D.NAME ELSE NULL
END AS DISTRICT_NAME,
D.SEGMENTID,
CASE WHEN D.SEGMENTID = S.SEGMENTID AND D.DISTRICTID = J.DISTRICTID
THEN S.NAME ELSE NULL
END AS SEGMENT,
J.STARTTIME,
J.ENDTIME,
SJ.COMCATJOBTYPEID,
J.JOBTYPEID,
CASE WHEN J.JOBTYPEID = JT.JOBTYPEID
THEN JT.NAME ELSE NULL
END AS JOBTYPENAME,
TS.SSSEGMENT AS SUBPL_NAME,
CONCAT(D.DISTRICTID,TS.SSSEGMENT) AS DISTRICTSUBPLID
FROM `EAR-AA-242`.JOB J
LEFT JOIN `EAR-AA-242`.DISTRICT D ON J.DISTRICTID = D.DISTRICTID
LEFT JOIN `EAR-AA-242`.DMJOB DJ ON DJ.JOBNUMBER = J.JOBID
LEFT JOIN `EAR-AA-242`.JOBTYPE JT ON JT.JOBTYPEID = J.JOBTYPEID
LEFT JOIN `EAR-AA-242`.STANDARDJOBTYPE SJ ON JT.STANDARDJOBTYPEID = SJ.STANDARDJOBTYPEID
LEFT JOIN `EAR-AA-239`.TBLJOBTYPE TJ ON SJ.COMCATJOBTYPEID = TJ.JOBTYPEID
LEFT JOIN (SELECT SS.SSSEGMENT, SS.NODEID FROM `EAR-AA-239`.TBLSSSEGMENT S,
`EAR-AA-239`.TBLSSSEGMENT SS WHERE S.SSS = SS.PARENTSSS AND SS.DELETED = 0) TS ON TJ.SSSEGMENT =TS.NODEID
LEFT JOIN `EAR-AA-242`.SEGMENT S ON S.SEGMENTID = D.SEGMENTID
WHERE DATE(J.STARTTIME) > DATE_SUB(DATE(SYSDATE()), INTERVAL 367 DAY)
ORDER BY J.JOBID;
Any help absolutely appreciated!!!
You should specify the datatypes of the columns explicitly. Without that, MySQL has to figure out what the types are. When you're using a CASE expression to return the values for a column, it assumes that the column datatype will be the same as the type of the first THEN value, but this will be wrong if other cases return a different type. In your code, you have some CASE expressions where the first result is DOUBLE, but another result is a CHAR string that can't be converted to DOUBLE.
So it should be
CREATE TABLE ODS.JOB_LOCATION_IDISTRICT (
JOBID VARCHAR(30),
DISTRICTID INT,
...
) AS
SELECT
...
I have a query in below format:
SELECT ETM.etm_Taxonomy, COUNT( PE.pp_profileID ) AS total_counts
FROM expertise_taxonomymaster AS ETM
LEFT JOIN expertise_taxonomy AS ET ON ETM.etm_ID = ET.`et_Taxonomy`
LEFT JOIN expertise AS E ON E.et_Taxonomy = ET.`et_ID`
LEFT JOIN profile_expertise AS PE ON PE.pp_expertiseID = E.et_ID
WHERE PE.pp_profileID IN (
SELECT PJ.pj_profileID
FROM jobtitle_taxonomymaster AS JTM
LEFT JOIN jobtitle_taxonomy AS JT ON JTM.jtm_ID = JT.`jt_Taxonomy`
LEFT JOIN jobtitle AS J ON J.jt_taxonomy = JT.`jt_ID`
LEFT JOIN profile_jobtitle AS PJ ON PJ.pj_jobtitleID = J.jt_ID
WHERE JTM.jtm_Taxonomy = 'Associate'
OR JTM.jtm_Taxonomy = 'Partner'
)
AND et_lawfirmID in (195,196)
GROUP BY etm_Taxonomy
And I have results as follows:
etm_Taxonomy total_counts
Advertising 18
Antitrust 47
Banking 258
But I need below results, Count should be split based on the JTM.ttm_Taxonomy field
etm_Taxonomy Patners195 Partners196 Associates195 Associates196
Advertising 18 18 18 18
Antitrust 47 47 47 47
Banking 258 258 258 258
Try this way:
SELECT ETM.etm_Taxonomy,
SUM (CASE WHEN PJ_TAX.jtm_Taxonomy = 'Associate' THEN 1 ELSE 0 END) AS Associates,
SUM (CASE WHEN PJ_TAX.jtm_Taxonomy = 'Partner' THEN 1 ELSE 0 END) AS Partners,
SUM (CASE WHEN PJ_TAX.jtm_Taxonomy = 'Consultant' THEN 1 ELSE 0 END) AS Consultants,
SUM (CASE WHEN PJ_TAX.jtm_Taxonomy = 'Counsel' THEN 1 ELSE 0 END) AS Counsels,
COUNT(PE.pp_profileID ) AS total_counts
FROM expertise_taxonomymaster AS ETM
LEFT JOIN expertise_taxonomy AS ET ON ETM.etm_ID = ET.`et_Taxonomy`
LEFT JOIN expertise AS E ON E.et_Taxonomy = ET.`et_ID`
LEFT JOIN profile_expertise AS PE ON PE.pp_expertiseID = E.et_ID
INNER JOIN
(
SELECT DISTINCT PJ.pj_profileID,JTM.jtm_Taxonomy
FROM jobtitle_taxonomymaster AS JTM
LEFT JOIN jobtitle_taxonomy AS JT ON JTM.jtm_ID = JT.`jt_Taxonomy`
LEFT JOIN jobtitle AS J ON J.jt_taxonomy = JT.`jt_ID`
LEFT JOIN profile_jobtitle AS PJ ON PJ.pj_jobtitleID = J.jt_ID
WHERE JTM.jtm_Taxonomy = 'Associate'
OR JTM.jtm_Taxonomy = 'Partner'
OR JTM.jtm_Taxonomy = 'Consultant'
OR JTM.jtm_Taxonomy = 'Counsel'
) as PJ_TAX
ON PE.pp_profileID= PJ_TAX.pj_profileID
WHERE et_lawfirmID =195
GROUP BY etm_Taxonomy
First of all: Your left outer joins are no outer joins really, because in your WHERE clause you say you want certain ETs and PEs only.
Mainly you want to join everything, then see whether partner or associate and whether 195 or 196 and count accordingly. This can be done with a CASE construct inside COUNT. Only problem may be duplicates leading to incorrect counts. Im am not completely sure about your database structure. In case there can be duplicate profileIDs with your inner query, you need a derived query with distinct, rather than just joining everything directly. Check if this works for you:
select
etm.etm_taxonomy,
count(case when t.jtm_taxonomy = 'Partner' and et_lawfirmid = 195 then 1 end) as patners195,
count(case when t.jtm_taxonomy = 'Partner' and et_lawfirmid = 196 then 1 end) as patners196,
count(case when t.jtm_taxonomy = 'Associate' and et_lawfirmid = 195 then 1 end) as associates195,
count(case when t.jtm_taxonomy = 'Associate' and et_lawfirmid = 196 then 1 end) as associates196
from expertise_taxonomymaster as etm
join expertise_taxonomy as et on etm.etm_id = et.et_taxonomy
join expertise as e on e.et_taxonomy = et.et_id
join profile_expertise as pe on pe.pp_expertiseid = e.et_id
join
(
select distinct pj.pj_profileid, jtm.jtm_taxonomy
from jobtitle_taxonomymaster as jtm
join jobtitle_taxonomy as jt on jtm.jtm_id = jt.jt_taxonomy
join jobtitle as j on j.jt_taxonomy = jt.jt_id
join profile_jobtitle as pj on pj.pj_jobtitleid = j.jt_id
where jtm.jtm_taxonomy in ('Associate', 'Partner')
) as t on t.pj_profileid = pe.pp_profileid
where et.et_lawfirmid in (195,196);
group by etm.etm_taxonomy;
I have a table 'booking_summary' which stores the type of method (method = Air or Sea).
I have to join this table with one of the two other tables depending on the method column.
If the method is Air,then the table to join is booking_air,if sea then it is booking_sea.
I do not want to run multiple queries on this particular page.
This is my latest attempt,that has obviously failed.The table_name with alias is the table i want in the same query.
$sql = "select case when a.shipping_method = 'Sea' then 'booking_sea' else 'booking_air' end 'table_name',
case when a.user_id ='$active_id' then 'y' else 'no' end 'generate_access',
case when c.mbl is NULL then 'Pending' else c.mbl end 'mbl_status',
case when c.hbl is NULL then 'Pending' else c.hbl end 'hbl_status',
a.*,b.user_name
from booking_summary a
left join registered_users b
on a.user_id = b.user_id
left join table_name c
on a.id = c.id
where (a.user_id = '$active_id' or a.forwarder='$active_id')";
Any advice would be very helpful. Thanks
Om I'm not sure if this is going to work but, anyhow...
$sql = "select case
when a.user_id ='$active_id' then 'y'
else 'no' end 'generate_access',
if(a.shipping_method = 'Sea',
case when c.mbl is NULL then 'Pending' else c.mbl end,
case when d.mbl is NULL then 'Pending' else d.mbl end ) 'mbl_status',
if(a.shipping_method = 'Sea',
case when c.hbl is NULL then 'Pending' else c.hbl end,
case when d.hbl is NULL then 'Pending' else d.hbl end ) 'hbl_status',
a.*,b.user_name
from booking_summary a
left join registered_users b on a.user_id = b.user_id
left join booking_sea c on a.id = c.id
left join bookin_air d on a.id=d.id
where (a.user_id = '$active_id' or a.forwarder='$active_id')";
Without fully understanding your structure, I can think of this solution
SELECT a.shipping_method
FROM shipping_summary AS A
LEFT JOIN (SELECT *,
'AIR' AS METHOD
FROM shipping_air) AS B
ON A.shipping_method = B.method
LEFT JOIN (SELECT *,
'SEA' AS METHOD
FROM shipping_sea) AS C
ON A.shipping_method = C.method
This is a high level answer as I do not have the fields to be selected and more ways to optimise the query.
Having an issue with a specific section of a query using DATEDIFF:
select 1 as NumApps,
LenderInfo.Name as LenderName,
CASE WHEN ApplicationInfo.AEType IS NULL OR ApplicationInfo.AEType = 0 THEN 'Unknown' ELSECONCAT (AEContact.FirstName, ' ', AEContact.LastName) END As AEName,
CASE WHEN b.createdby > 0 THEN CONCAT (contactinfo.firstname, ' ', contactinfo.lastname) END AS LogActionBy,
CASE WHEN ApplicationInfo.RecertificationById IS NULL THEN CASE WHEN ApplicationInfo.IsCorrespondent IS NULL OR ApplicationInfo.IsCorrespondent = 0 THEN 'Wholesale' ELSE 'Correspondent' END ELSE CASE WHEN ApplicationInfo.IsCorrespondent IS NULL OR ApplicationInfo.IsCorrespondent = 0 THEN 'Wholesale - Recert' ELSE 'Correspondent- Recert' END END As AppType,
Applicationinfo.SubmissionDate,
ApplicationInfo.ApplicationID,
b.status AS LogStatus,
b.CreatedOn as LogDate,
companyinfo.Name,
companyinfo.NMLSEntityID,
applicationinfo.CreatedDate,
ApplicationInfo.Status as ApplicationStatus,
ApplicationInfo.StatusChangeDate,
DATEDIFF ((Select CreatedOn From ApplicationStatusChangeLog a where a.ApplicationId = b.ApplicationId
And a.status = 'Approved'),(Select CreatedOn From ApplicationStatusChangeLog a Where a.ApplicationId = b.ApplicationId And a.status = 'Pending Approval')) AS DaysToApprove
from applicationinfo
INNER JOIN CompanyInfo ON(ApplicationInfo.CompanyId = CompanyInfo.CompanyId)
left join CompanyInfo As LenderInfo ON (ApplicationInfo.LenderId = LenderInfo.CompanyId)
LEFT JOIN UserInfo ON (UserInfo.UserId = ApplicationInfo.LastModifiedById)
LEFT JOIN ContactInfo ON UserInfo.ContactId = ContactInfo.ContactId
LEFT JOIN ContactInfo AS Comergence ON(ApplicationInfo.ComergenceRepId = Comergence.ContactId)
LEFT JOIN UserInfo AS AEUser ON(AEUser.UserId = ApplicationInfo.AEType)
left join paymentinfo on applicationinfo.applicationid = paymentinfo.applicationid
Inner join applicationstatuschangelog b on applicationinfo.applicationid = b.applicationid
LEFT JOIN ContactInfo AS AEContact ON(AEContact.ContactId = AEUser.ContactId)
LEFT JOIN UserInfo AS StatusUser ON(StatusUser.UserId = ApplicationInfo.StatusChangeById)
LEFT JOIN ContactInfo AS StatusContact ON(StatusContact.ContactId = StatusUser.ContactId)
LEFT JOIN ApprovalStatus ON(ApplicationInfo.ApplicationId = ApprovalStatus.ApplicationId AND ApplicationInfo.CompanyId = ApprovalStatus.CompanyHQId
AND ApplicationInfo.CompanyId = ApprovalStatus.CompanyId)
Where ApplicationInfo.Status NOT In ('Approved Monitor Only')
AND LenderInfo.ActiveLenderContract = 1
AND COALESCE(ApplicationInfo.IsDeleted,0) <> 1
AND b.Status NOT IN ('Incomplete', 'Not Submitted')
Group BY b.ApplicationID`
I've seen some people mentioning using IN instead of = within the subquery, but I can't seem to get it to work. Any ideas out there? Thanks in advance.
I was able to get the query working by adding 'Limit 1' after the identified status:
(Select CreatedOn From ApplicationStatusChangeLog a where a.ApplicationId = b.ApplicationId
And a.status = 'Approved' LIMIT 1),(Select CreatedOn From ApplicationStatusChangeLog a Where a.ApplicationId = b.ApplicationId And a.status = 'Pending Approval' LIMIT 1)
I have a statement that looks like:
QUERY A
UNION
QUERY B
ORDER BY SomeColumn
Query A and Query B each take a nominal amount of time to run, but when I put them in the UNION, it take 7-9 seconds which is unacceptable. In this case Query A returns 6 rows, Query B returns 7. So confusing...
I have absolutely no idea what would cause this, help would be much appreciated!
Here is an anonymized version of the script (I did not write this, so don't hate):
SELECT
'XXX' l_t,
s.p,
srst.c_b,
srd.a_d_f,
s.s_c,
sf.c_s,
srst.p_f,
s.s_r_i,
s.s_i,
s.s_d,
srd.m_s_d_l s_d,
srd.m_e_d_l e_d,
CASE WHEN (srs.s_s_t IS NOT NULL AND srs.s_s_t <> srs.s_e_t)
THEN 1 ELSE 0 END 's_f',
CASE WHEN (srs.c_s_t IS NOT NULL AND srs.c_s_t <> srs.c_e_t)
THEN 1 ELSE 0 END 'c_f',
r.r_i
FROM
t_s_r_d srd
INNER JOIN t_s s WITH (NOLOCK)
ON s.s_i = srd.s_i
INNER JOIN i_s_r(12345) r
ON r.r_i = srd.r_i
INNER JOIN i_s_s_f() sf
ON (sf.s_i = srd.s_i)
INNER JOIN t_s_r_s srst WITH (NOLOCK)
ON (srst.s_i = srd.s_i AND srst.r_i = srd.r_i )
LEFT OUTER JOIN t_s_r_s srs WITH (NOLOCK)
ON (srs.s_i = srd.s_i AND srs.r_i = srd.r_i)
WHERE
srst.d_f = 0
AND ((srd.m_s_d_l >= someval AND srd.m_s_d_l < someotherval)
OR
(srd.m_s_d_l <= someval AND srd.m_e_d_l > someotherval))
AND r.o_f = 0
AND r.i_f = 0
AND r.v_f = 1
AND r.g_i = 180
AND NOT EXISTS(SELECT * FROM t_c_r cdr WITH (NOLOCK) WHERE cdr.r_i = r.r_i)
UNION
SELECT
'XXX' l_t,
s.p,
srst.c_b,
srd.a_d_f,
s.s_c,
sf.c_s,
srst.p_f,
s.s_r_i,
s.s_i,
s.s_d,
srd.m_s_d_l s_d,
srd.m_e_d_l e_d,
CASE WHEN (srs.s_s_t IS NOT NULL AND srs.s_s_t <> srs.s_e_t)
THEN 1 ELSE 0 END 's_f',
CASE WHEN (srs.c_s_t IS NOT NULL AND srs.c_s_t <> srs.c_e_t)
THEN 1 ELSE 0 END 'c_f',
c.c_i
FROM
(t_s_r_d srd
INNER JOIN t_s s WITH (NOLOCK)
ON s.s_i = srd.s_i
INNER JOIN i_s_s_f() sf
ON (sf.s_i = srd.s_i)
LEFT OUTER JOIN t_s_r_s srs WITH (NOLOCK)
ON (srs.s_i = srd.s_i AND srs.r_i = srd.r_i)
INNER JOIN t_s_r_s srst WITH (NOLOCK)
ON (srst.s_i = srd.s_i AND srst.r_i = srd.r_i)),
i_s_c(12345) c
WHERE
srst.d_f = 0
AND ((srd.m_s_d_l >= someval AND srd.m_s_d_l < someotherval)
OR
(srd.m_s_d_l <= someval AND srd.m_e_d_l > someotherval))
AND c.o_f = 0
AND c.i_f = 0
AND c.v_f = 1
AND c.g_i = 180
AND EXISTS(SELECT * FROM t_c_r cr WITH (NOLOCK) WHERE cr.r_i = srd.r_i and
cr.c_i = c.c_i)
ORDER BY s_d
Because UNION removes duplicates it has to sort the whole data set first...try to use UNION ALL instead....it is much faster since it doesn't need to remove the dups
Turns out it was the old style ansii style joins in conjunction with the newer style explicit joins that were causing the lengthy return. Quite interesting, if anyone could provide a reason why that would be fantastic!