How to multiple rows to column & show one row - mysql

I am facing a problem with the MySQL query result.
I want result rows to a column & get only one row
please suggest & help to solve my problem
I Done that in numeric values using sum() but in this case, data is string not numeric
SELECT
CASE WHEN IB.`IndicatorId` = 459 THEN AA.`Answer` ELSE '' END AS 'FO',
CASE WHEN IB.`IndicatorId` = 462 THEN AA.`Answer` ELSE '' END AS 'Main_Canal',
CASE WHEN IB.`IndicatorId` = 461 THEN AA.`Answer` ELSE '' END AS 'Parent_Canal',
CASE WHEN IB.`IndicatorId` = 473 THEN AA.`Answer` ELSE '' END AS 'Channel_length',
CASE WHEN IB.`IndicatorId` = 472 THEN AA.`Answer` ELSE '' END AS 'Design_Discharge_Cusecs',
CASE WHEN IB.`IndicatorId` = 474 THEN AA.`Answer` ELSE '' END AS 'GCA',
CASE WHEN IB.`IndicatorId` = 475 THEN AA.`Answer` ELSE '' END AS 'CCA',
CASE WHEN IB.`IndicatorId` = 471 THEN AA.`Answer` ELSE '' END AS 'Water_Courses',
A.`DataCollectorId`
FROM `answer_water_discharge` AS A
INNER JOIN `answers_answer_water_discharge` AS AA ON AA.`AnswerId` = A.`AnswerId`
INNER JOIN `activity_sections_indicators` AS ASI ON ASI.`SectionIndicatorId` = AA.`SectionIndicatorId`
INNER JOIN `indicators_bank` AS IB ON IB.`IndicatorId` = ASI.`IndicatorId`
WHERE 1=1 AND A.AnswerId = 57
I want one row with rows to column

Related

temporay table update mysql

I am migrating my stored procedures from Sql Server to MySql.
Somewhere I am facing problem to transform update queryfrom MS sql server to Mysql for temporary variable. Here is my MS sql server query:
update sampleStoreTV set felony = c.fel,misdemeanor = c.mis,violation = c.vio
from (select srcCL.id as 'storeId',
case when SUM(case when srcCL.cl='FELONY' then 1 else 0 end)>0 then 1 else 0 end as 'fel',
case when SUM(case when srcCL.cl='MISDEMEANOR' then 1 else 0 end)>0 then 1 else 0 end as 'mis',
case when SUM(case when srcCL.cl='VIOLATION' then 1 else 0 end)>0 then 1 else 0 end as 'vio'
from
(select DM_Sample_Store.DM_Sample_Store_ID as 'id',
Charge_Level.Mapped_Charge_Level as 'cl'
from DM_Sample_Store
left join screening_result on Screening_Result.Screening_Result_id = DM_Sample_Store.Screening_Result_id
left join Crim_Case on Screening_Result.Screening_Result_id = Crim_Case.Screening_Result_id
left join Crim_Charge on Crim_Case.Crim_case_id = Crim_Charge.Crim_case_id
left join PDL_DataSource on DM_Sample_Store.DataSource_Id = PDL_DataSource.DataSource_Id
left join DM_Sample_Search_Param on DM_Sample_Store.DM_Sample_Store_ID = DM_Sample_Search_Param.DM_Sample_Store_ID
left join Charge_Level on Crim_Charge.Sanitized_Charge_Level_id = Charge_Level.Charge_Level_id
where PDL_DataSource.DataSource_Id = dataSourceId
and DM_Sample_Store.Active = 1
and DM_Sample_Search_Param.Global_Search_Parameter_ID in (sosGspId, freqGspId, clGspId, dtGspId)
and DM_Sample_Search_Param.Active = 1
-- and Crim_Charge.AOR_Days is not null
) srcCL
group by id
having SUM(case when cl='FELONY' then 1 else 0 end)>0
or SUM(case when cl='MISDEMEANOR' then 1 else 0 end)>0
or SUM(case when cl='VIOLATION' then 1 else 0 end)>0) c
where sampleId = c.storeId
I am not sure what youre trying to do but your query is completely wrong for many different reasons.
You need to join your sampleStoreTV with your other tables. I guess it might be something like that:
update sampleStoreTV
inner join (your query) as c on sampleStoreTV.sampleId = c.storeId
set sampleStoreTV.felony = c.fel,
sampleStoreTV.misdemeanor = c.mis,
sampleStoreTV.violation = c.vio
I hope it helps

Having trouble finding the error in my code

I am not sure what is wrong with my sql code here. it tells me there is an error at the end but say that its at ''. Thanks for the help!
SELECT PatientID, HCPCCode, HCPCCodeCount, DischargeStatus, State, Gender, Race, Age, County, ProcedureDate, ClaimThroughDate, ICDAccessCode, ClaimID,
CASE WHEN DiagnosisCodeType = 4 THEN 4 END code4,
CASE WHEN DiagnosisCodeType = 4 THEN `Procedures` END procedures4,
CASE WHEN DiagnosisCodeType = 4 THEN `ICD9Codes` END ICD94,
CASE WHEN DiagnosisCodeType = 6 THEN 6 END code6,
CASE WHEN DiagnosisCodeType = 6 THEN `Procedures` END procedures6,
CASE WHEN DiagnosisCodeType = 7 THEN 7 END code7,
CASE WHEN DiagnosisCodeType = 7 THEN `Procedures` END procedures7,
CASE WHEN DiagnosisCodeType = 8 THEN 8 END code8,
CASE WHEN DiagnosisCodeType = 8 THEN `Procedures` END procedures8,
CASE WHEN DiagnosisCodeType = 9 THEN 9 END code9,
CASE WHEN DiagnosisCodeType = 9 THEN `Procedures` END procedures9
FROM ((SELECT subquery2.*, GROUP_CONCAT(CONCAT (ICDAccessCode) SEPARATOR ', ') AS ICD9Codes
FROM (SELECT subquery.*, GROUP_CONCAT(CONCAT (ProcedureID) SEPARATOR ', ') AS Procedures
FROM(SELECT revenue.HCPCCode, revenue.HCPCCodeCount, patient.*, icd9.DiagnosisCodeType, procedures.ProcedureID, procedures.ProcedureDate, claims.ClaimThroughDate, icd9.ICDAccessCode
FROM `cmsdata-outpatient-2012`.revenue revenue
RIGHT OUTER JOIN `cmsdata-outpatient-2012`.patient patient
ON revenue.ClaimID = patient.ClaimID
JOIN `cmsdata-outpatient-2012`.diagnosisicd9 icd9
ON revenue.ClaimID = icd9.ClaimID
JOIN `cmsdata-outpatient-2012`.claims claims
ON revenue.ClaimID = claims.ClaimID
JOIN `cmsdata-outpatient-2012`.procedures procedures
ON revenue.ClaimID = procedures.ClaimID
AND revenue.HCPCCode IN (659, 698, 5116, 5118, 5122, 5123, 5127, 12164, 12171, 16976, 17283)
GROUP BY ClaimID, DiagnosisCodeType) AS subquery
) subquery2)) GROUP BY ClaimID AS subquery3;

How can I integrate a MySQL subquery

I have a MySQL subquery that I need to incorporate into my main query but i'm not sure how. My main query is shown below. I want to run that and as part of the same query run the subquery and get the result from that (which happens to be a sum) and then compare the result of the subquery in the main MySQL statement.
Main query
select * from employees_timesheet where
(sign_off_1 = 'Director' or sign_off_2 = 'Director' or sign_off_3 = 'Director')
and
(case when sign_off_1 <> 'Director' and sign_off_1_status = 'Compete'
then 1
else 0
end
+ case when sign_off_2 <> 'Director' and sign_off_2_status = 'Compete'
then 1
else 0
end
+ case when sign_off_3 <> 'Director' and sign_off_3_status = 'Compete'
then 1
else 0
end) = {{{$sign_offs_required}}}
Sub query
select
SUM(
case when employees_timesheet.sign_off_1 <> 'Director'
then 1
else 0
end
+ case when employees_timesheet.sign_off_2 <> 'Director'
then 1
else 0
end
+ case when employees_timesheet.sign_off_3 <> 'Director'
then 1
else 0
end
) as sign_offs_required
from employees_timesheet
Any help is mch appreciated.
I managed to get it to work using the following query:
select et.* from employees_timesheet et where
(et.sign_off_1 = 'Director' or et.sign_off_2 = 'Director' or et.sign_off_3 = 'Director')
and
(case when et.sign_off_1 <> 'Director' and et.sign_off_1_status = 'Approved'
then 1
else 0
end
+ case when et.sign_off_2 <> 'Director' and et.sign_off_2_status = 'Approved'
then 1
else 0
end
+ case when et.sign_off_3 <> 'Director' and et.sign_off_3_status = 'Approved'
then 1
else 0
end) =
(case when et.sign_off_1 <> 'Director'
then 1
else 0
end
+ case when et.sign_off_2 <> 'Director'
then 1
else 0
end
+ case when et.sign_off_3 <> 'Director'
then 1
else 0
end
)
group by et.id

SQL Conditional Statement in Where Clause

I would like to write the following IF statement in SQL. Most of the where clause is constructed, but it's the condition around the statement that I'm struggling with:
if #StuYear = 11 then
AND (#TeachingGroup = 'Select All')
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND (#Subgroup='Select All')
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like '[ABU]%' AND Result <> ''
else if #StuYear = 10 then
AND #TeachingGroup Not Like 'Select All'
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND ([TeachingGroup] = #TeachingGroup)
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like '[ABC]%' AND Result <> ''
end if
Simply replace if with where and else if with or and surround the other conditions in paranthesis:
...
where (#StuYear = 11
AND (#TeachingGroup = 'Select All')
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND (#Subgroup='Select All')
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like 'A*'
AND Result Not Like 'A'
AND Result Not Like 'B'
AND Result Not Like 'U'
AND Result Not Like '' )
OR (#StuYear = 10
AND #TeachingGroup Not Like 'Select All'
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND ([TeachingGroup] = #TeachingGroup)
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like 'A*'
AND Result Not Like 'A'
AND Result Not Like 'B'
AND Result Not Like 'C'
AND Result Not Like '')
Tim's answer helped point me in the right direction. However, I eventually resolved the issue by applying the year condition within the where clause as opposed to rapping it around them:
where
(#StuYear = [stuyear]
AND #TeachingGroup = 'Select All'
AND [DataCollection] = #DataCollection
AND [Name] = #SubjectName
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND ((Result Not Like '[ABU]%' and result not like '' and stuyear='11')
or(Result Not Like '[ABC]%' and result not like '' and stuyear='10')
or(Result Not Like '[ABCD]%' and stuyear='9'))
AND Result <> '')
OR
(#StuYear = [stuyear]
AND #TeachingGroup Not Like 'Select All'
AND [DataCollection] = #DataCollection
AND [Name] = #SubjectName
AND [TeachingGroup] = #TeachingGroup
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND ((Result Not Like '[ABU]%' and result not like '' and stuyear='11')
or(Result Not Like '[ABC]%' and result not like '' and stuyear='10')
or(Result Not Like '[ABCD]%' and stuyear='9'))
AND Result <> '')

Using CASE statement in SUM() In Stored Procedure

Here is my code:
BEGIN
SELECT SUM(
CASE WHEN first_name > '' THEN 1 ELSE 0 END AS a
CASE WHEN last_name > '' THEN 1 ELSE 0 END AS b,
CASE WHEN country > '' THEN 1 ELSE 0 END AS c,
CASE WHEN state > '' THEN 1 ELSE 0 END AS d,
CASE WHEN city > '' THEN 1 ELSE 0 END AS e)
AS total
FROM employee_profile
WHERE id = user_id_in;
END
This generates the following error:
you have an error in your SQL syntax check the manual that corresponds to your MySQL server version for the right syntax to use near xxx error for the first line.
Can anyone help with the correct syntax for this, please?
MySQL Server version: 5.5.8
SUM takes a single argument (/ column) which it sums across multiple rows, not multiple arguments (/ columns) for a single row, this is why it's not working.
This should work:
SELECT (CASE WHEN first_name > '' THEN 1 ELSE 0 END +
CASE WHEN last_name > '' THEN 1 ELSE 0 END +
CASE WHEN country > '' THEN 1 ELSE 0 END +
CASE WHEN state > '' THEN 1 ELSE 0 END +
CASE WHEN city > '' THEN 1 ELSE 0 END) AS total
FROM employee_profile
WHERE id = user_id_in;
Try this query this will suerly help you..
SELECT SUM(
CASE
WHEN first_name = '' THEN '1'
WHEN last_name = '' THEN '1'
WHEN country = '' THEN '1'
WHEN state = '' THEN '1'
ELSE '0'
END) AS total
FROM employee_profile
WHERE id = user_id_in;