1

My SQL Query

SELECT emp_location, emp_system_name, emp_compName, tran_type_code, emp_morn_checkin, emp_ot_checkin, emp_ot_checkout, over_time, ontime, updated_by
FROM timesheet_tran
WHERE emp_id =  'TMSTEST'
AND tran_as_of_date =  '04/02/14'

Returns data as

SQL Structure

I want to find a way to combine and display data

Like

emp_location -- emp_system_name -- morn_check_OTM -- mor_check_DLY -- overtime_OTM --overtime_OT
AMK               ::1                8.45               8.38            1:15           6:30

My SQL knowledge , cant do this :(

3
  • What about tran_type_code which value do you want to select OTM or DLY or OT or any value?? Commented Feb 4, 2014 at 12:16
  • what are the rules to combine the entries? do calculations need to happen or only select based on tran_type_code ? Commented Feb 4, 2014 at 12:20
  • the vaules need to be in one row .. morn_check_OTM , mor_check_DLY Commented Feb 4, 2014 at 12:27

2 Answers 2

3

What you are looking for is pivot rows into columns, unfortunately MySQL doesn't have a native pivot table operator, but you can use the CASE expression to do so:

SELECT 
  emp_location, 
  emp_system_name,
  MAX(CASE WHEN tran_type_code = 'OTM' THEN emp_morn_checkin END) AS morn_check_OTM,
  MAX(CASE WHEN tran_type_code = 'DLY' THEN emp_morn_checkin END) AS morn_check_DLY,
  MAX(CASE WHEN tran_type_code = 'OT' THEN emp_morn_checkin END) AS morn_check_OT,
  MAX(CASE WHEN tran_type_code = 'OTM' THEN over_time END) AS over_time_OTM,
  MAX(CASE WHEN tran_type_code = 'DLY' THEN over_time END) AS over_time_DLY,
  MAX(CASE WHEN tran_type_code = 'OT' THEN over_time END) AS over_time_OT
FROM timesheet_tran
WHERE emp_id =  'TMSTEST'
AND tran_as_of_date =  '04/02/14'
GROUP BY emp_location, emp_system_name; 
Sign up to request clarification or add additional context in comments.

3 Comments

From where do we find People like U ..:) cheers Works like Charm
from were do you know all this stuff ?
@epynic From here, from stackoverflow :)
0

Try using concat(a, '---',b,c,d,) the way you want

2 Comments

concat(emp_location, '--', emp_system_name, '--' ,morn_check_OTM ,'--' ,mor_check_DLY, '--', overtime_OTM ,'--',overtime_OT)
yes but how can i combine the resultant to one single row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.