Using If Else in Mysql - mysql

SELECT process.process_name, com_jobcard.job_card_num,
IF process.UOM = '1' THEN SET (com_jobcard.dept_qty) AS Total_qty
ELSEIF process.UOM = '2' THEN SET (com_jobcard.total_stones) AS Total_qty
FROM timer_completed
INNER JOIN PROCESS ON process.id = timer_completed.process_id
INNER JOIN com_jobcard ON timer_completed.job_card_id = com_jobcard.id
AND timer_completed.report_date = '2015-09-15'
Hi friends I would like to retrieve the value of department quantity When process.UOM = 1 else if process.UOM = '2' THEN SET (com_jobcard.total_stones) AS Total_qty , please help me with the code , thanks in advance

You can use case-when something as
SELECT
process.process_name,
com_jobcard.job_card_num,
case
when process.UOM = '1' then com_jobcard.dept_qty
when process.UOM = '2' then com_jobcard.total_stones
end as Total_qty
FROM timer_completed
INNER JOIN PROCESS ON process.id = timer_completed.process_id
INNER JOIN com_jobcard ON timer_completed.job_card_id = com_jobcard.id
AND timer_completed.report_date = '2015-09-15'
NOTE : Right now there is no else so its better to add an else if both condition fails
case
when process.UOM = '1' then com_jobcard.dept_qty
when process.UOM = '2' then com_jobcard.total_stones
else 'something you want'
end as Total_qty

IF(condition, true_value, false_value) can be used this way
SELECT process.process_name, com_jobcard.job_card_num,
IF(process.UOM = '1', com_jobcard.dept_qty, IF(process.UOM = '2', com_jobcard.total_stones, 0)) AS Total_qty
FROM timer_completed
INNER JOIN PROCESS ON process.id = timer_completed.process_id
INNER JOIN com_jobcard ON timer_completed.job_card_id = com_jobcard.id
AND timer_completed.report_date = '2015-09-15'

Related

SELECT Failed 3706: Syntax error: Expected something between

Novice would greatly appreciate assistance on the following 3706 error message:
expected something between the word 'Calls' and the 'case' keyword
SELECT distinct
b.emp_nbr,
b.prim_terr_desc,
a.sales_terr_nbr,
TRIM (B.first_nm) ||' '|| TRIM (B.last_nm) as AE_Name,
count (distinct call_nbr) as Calls
case (when a.sale_call_chanl_desc LIKE 'Face to Face%' then 'F2F'
when a.sale_call_chanl_desc = 'Telephone' then 'Phone'
when a.sale_call_chanl_desc LIKE 'Web Meeting%' then 'Web_Meeting'
end) as Channel
FROM isell_prod_view_db.sfdc_call_action a
LEFT OUTER JOIN isell_prod_view_db.sfdc_customer_info c ON (a.cust_acct_nbr = c.cust_acct_nbr and a.rec_delt_flg = 'N')
LEFT OUTER JOIN isell_prod_view_db.sfdc_opportunity d ON (d.cust_acct_nbr = c.cust_acct_nbr and d.delt_flg = 'N')
LEFT OUTER JOIN isell_prod_view_db.sfdc_user_profile b ON a.crte_by_user_key_nbr = b.user_key_nbr
LEFT OUTER JOIN isell_prod_view_db.sfdc_opportunity_item e ON (e.oprty_key_nbr = d.oprty_key_nbr and e.delt_flg = 'N')
where a.sale_call_stat_desc = 'Completed'
and a.sales_div_nbr = '8'
and a.sales_grp_nbr = '9'
and a.sales_org_nbr ='30'
and b.prim_terr_desc LIKE ('8-9-30%')
and a.priv_entr_flg='N'
and a.sale_call_chanl_desc is not null
and CAST(a.call_dt AS date format 'MM/DD/YYYY') between '06/01/2019' and '05/31/2020'
group by 1,2,3,4
I rejigged the code. This works. I think i needed a comma after "AS Channel"
SELECT distinct
b.emp_nbr,
TRIM (B.first_nm) ||' '|| TRIM (B.last_nm) as AE_Name,
b.prim_terr_desc,
b.prim_terr_seg_nbr,
case when CAST(a.call_dt AS date format 'MM/DD/YYYY') between '12/01/2019' and '02/29/2020' then 'Q3'
when CAST(a.call_dt AS date format 'MM/DD/YYYY') between '03/01/2020' and '05/31/2020' then 'Q4'
end as Qtr,
count (distinct call_nbr) as Calls
FROM isell_prod_view_db.sfdc_call_action a
LEFT OUTER JOIN isell_prod_view_db.sfdc_customer_info c ON (a.cust_acct_nbr = c.cust_acct_nbr and a.rec_delt_flg = 'N')
LEFT OUTER JOIN isell_prod_view_db.sfdc_opportunity d ON (d.cust_acct_nbr = c.cust_acct_nbr and d.delt_flg = 'N')
LEFT OUTER JOIN isell_prod_view_db.sfdc_user_profile b ON a.crte_by_user_key_nbr = b.user_key_nbr
LEFT OUTER JOIN isell_prod_view_db.sfdc_opportunity_item e ON (e.oprty_key_nbr = d.oprty_key_nbr and e.delt_flg = 'N')
where a.sale_call_stat_desc = 'Completed'
and a.sales_div_nbr = '8'
and a.sales_grp_nbr = '9'
and a.sales_org_nbr ='30'
and b.prim_terr_desc LIKE ('8-9-30%')
and A.rec_delt_flg = 'N'
and A.child_event_flg='N'
and a.priv_entr_flg='N'
and B.prim_terr_seg_desc not in ('No Coverage')
and A.sale_call_chanl_desc in ('Telephone', 'Phone', 'Face to Face - Appt', 'Face to Face - No Appt', 'Web Meeting')
and B.prim_terr_seg_nbr not in ('3', '8', '25')
and CAST(a.call_dt AS date format 'MM/DD/YYYY') between '12/01/2019' and '05/31/2020'
group by 1,2,3,4,5

Combine Union ALL with Inner Join Statement

I have a problem how to combine the sql statements (inner join and union all. as a Newbie in SQL. Hopefully all members can help me to solve the problems.attached herewith the SQL. The leave_Trx and leave_History contain same values that need to be union. Thank you.
select m.name, t.startdt, t.enddt,t.noday,t.createdDT,
(case when t.approveST = 'Y' then 'Approved' when t.approveST = 'N' then 'Not Approved' else 'Pending' end) as appST
from leave_Trx t
inner join leave_MType m on m.typeID = t.trxID
inner join hr_personaldata b on b.pers_ID = #pers_ID
where year(t.startdt) = #yyear
and b.pers_ID = #pers_ID and b.pers_name LIKE '%'+#pers_name+'%'
and b.pers_compID LIKE ''+#compID+''
union all
select * from leave_History h
where year(h.startdt) = #yyear and h.status = 'A'
ORDER BY t.startdt
select t.pers_ID,t.startdt, t.enddt,t.noday,t.createdDT,
(case when t.approveST = 'Y' then 'Approved' when t.approveST = 'N' then 'Not Approved' else 'Pending' end) as appST
from leave_Trx t
inner join leave_MType m on m.typeID = t.pers_ID
inner join hr_personaldata l on l.pers_ID = #pers_ID
where year(t.startdt) = #yyear and t.status = 'A'
union all
select h.pers_ID, h.startdt, h.enddt,h.noday,h.createdDT,
(case when h.approveST = 'Y' then 'Approved' when h.approveST = 'N' then 'Not Approved' else 'Pending' end) as appST
from leave_History h
inner join leave_MType m on m.typeID = h.pers_ID
inner join hr_personaldata b on b.pers_ID = #pers_ID
where year(h.startdt) = #yyear and h.status = 'A'

MySQL Query depending on column value define where statement

I have this SQL:
SELECT
`refnumbers`.`order_id`,
`refnumbers`.`deal_id`,
`refnumbers`.`claim_track_id`,
`deals`.`partner_count`
FROM `refnumbers`
JOIN `deals`
ON (`refnumbers`.`deal_id` = `deals`.`ID`)
JOIN `users`
ON (`users`.`id` = `deals`.`partner_id`)
WHERE `refnumbers`.`is_claimed` = '1'
AND `deals`.`partner_id` = '62039'
AND `refnumbers`.`claimed_at` BETWEEN '2013-01-17 00:00:00'
AND '2013-01-17 23:59:59'
ORDER BY `refnumbers`.`claimed_at` DESC
I would like to select where deals.partner_id = 62039 IF deals.partner_count is 1.
If partner_count isnt 1 then it should select where refnumbers.claim_track_id = 62039
How can this be done?
try
where
case when deals.partner_count = 1
then deals.partner_id = 62039
else refnumbers.claim_track_id = 62039
end
You need to have a condition enclose in a parenthesis which uses OR
SELECT `refnumbers`.`order_id`,
`refnumbers`.`deal_id`,
`refnumbers`.`claim_track_id`,
`deals`.`partner_count`
FROM `refnumbers`
INNER JOIN `deals`
ON (`refnumbers`.`deal_id` = `deals`.`ID`)
INNER JOIN `users`
ON (`users`.`id` = `deals`.`partner_id`)
WHERE `refnumbers`.`is_claimed` = '1' AND
`refnumbers`.`claimed_at` BETWEEN '2013-01-17 00:00:00' AND '2013-01-17 23:59:59'
AND
(
(deals.partner_id = 62039 AND deals.partner_count = 1)
OR
(refnumbers.claim_track_id = 62039 AND deals.partner_count <> 1)
)
ORDER BY `refnumbers`.`claimed_at` DESC
Try this:
SELECT r.order_id, r.deal_id,r.claim_track_id, d.partner_count
FROM refnumbers r
INNER JOIN deals d ON r.deal_id = d.ID
INNER JOIN users u ON d.partner_id = u.id
WHERE IF(d.partner_count = 1, d.partner_id = '62039', r.claim_track_id = 62039) AND
r.is_claimed = '1' AND r.claimed_at BETWEEN '2013-01-17 00:00:00' AND '2013-01-17 23:59:59'
ORDER BY r.claimed_at DESC

DATEDIFF Subquery returns more than 1 row in mysql

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)

MYSQL CASE WHEN PROBLEM

SELECT `profiles`.*
FROM `profiles`
INNER JOIN `friendships`
ON `profiles`.id = `friendships`.(CASE WHEN friendships.profile_id = 1
THEN`friend_id` ELSE `profile_id` END)
How can i make the inner join like profile.id = friendships.(here will select the one key that is needed) but it doesnt work. please help :P
it cant be:
`profiles`.id = (CASE WHEN friendships.profile_id = 1
THEN `friendships`.`friend_id` ELSE `friendships`.`profile_id` END)
SELECT `profiles`.*
FROM `profiles`
INNER JOIN `friendships`
ON `profiles`.id = (CASE WHEN friendships.profile_id = 1
THEN friendships.`friend_id` ELSE friendships.`profile_id` END)
How about
SELECT `profiles`.*
FROM `profiles`
INNER JOIN `friendships`
ON (profiles.id = friendships.profile_id AND NOT (profile_id = 1))
OR ((profiles_id =friendships.friend_id) AND (profile_id = 1))