Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
navbingo20
Starting Member
12 Posts |
Posted - 2011-04-25 : 06:56:50
|
hi,i have attached my sql query with this mail SELECT Convert(varchar(10), hours) + ':' + Convert(varchar(10), mins) As total_time, total_minutes,machinename,description_name FROM ( SELECT total_minutes / 60 As hours , total_minutes % 60 As mins,total_minutes, machinename ,description_name FROM ( SELECT Sum(DateDiff(mi, 0, act_hour_as_datetime)) As total_minutes, machinename ,description_name FROM ( SELECT Convert(datetime, total_time) As act_hour_as_datetime, machinename ,description_name FROM [ven_fullreportmaster] with(nolock) INNER JOIN ven_descriptionmaster VDM ON VDM.description_id = ven_fullreportmaster.description_id inner join ven_machinemaster vm on vm.machine_id = ven_fullreportmaster.machine_id where entry_date = convert(varchar, getdate(), 105) and shift_type ='DAY_SHIFT' and is_task_completed ='Y' ) As derived_table group by machinename ,description_name ) As another_derived_table group by total_minutes, machinename ,description_name ) As yet_another_derived_table group by total_minutes, machinename,description_name,hours,mins the out put looks like this :total_time total_minutes machine_name description_name6:0 300 ABC IDLE4:0 240 DEF RUN1:15 75 GHI DOWNTIME2:00 120 ABC DOWNTIMEbut i want to frame the table like this :Machinename IDLE_TIME RUNTIME DOWN_TIMEABC 6:0 0:0 2:00DEF 0:0 4:0 0:0GHI 0:0 0:0 1:15could you plz help me out to solve this issuethnx navin |
|
Serrano88
Starting Member
4 Posts |
Posted - 2011-05-10 : 12:06:59
|
A format like the one I've written below might solve your problem, and make your code a bit more manageable. Of course I probably don't have your data sources correct, but I think you can update the queries to match your environment.Essentially, I find it easiest to create a table and insert my key fields first. In this case that would be the machine names and ids. Then I update each field using the keys I've inserted. Hope this makes sense:/* Create Production Table */CREATE TABLE MachineProductionTime ( MachineId varchar(10), Machine_Name varchar(3), Idle_Time datetime, Run_Time datetime, Down_Time datetime)/* Insert Machine Names */INSERT INTO MachineProductionTimeSELECT DISTINCT MachineId, Machine_NameFROM ven_machinemaster/* Update table with Idle Time data */UPDATE MachineProductionTimeSET Idle_Time = Convert(varchar,total_time, 108)FROM ven_fullreportmasterJOIN ven_descriptionmaster ON ven_fullreportmaster.description_id = ven_descriptionmaster.description_idWHERE MachineProductionTime.MachineId = ven_fullreportmaster.MachineIdAND ven_descriptionmaster.description_id = 'IDLE'/* Update table with Run Time data */UPDATE MachineProductionTimeSET Idle_Time = Convert(varchar,total_time, 108)FROM ven_fullreportmasterJOIN ven_descriptionmaster ON ven_fullreportmaster.description_id = ven_descriptionmaster.description_idWHERE MachineProductionTime.MachineId = ven_fullreportmaster.MachineIdAND ven_descriptionmaster.description_id = 'RUN'/* Update table with Down Time data */UPDATE MachineProductionTimeSET Idle_Time = Convert(varchar,total_time, 108)FROM ven_fullreportmasterJOIN ven_descriptionmaster ON ven_fullreportmaster.description_id = ven_descriptionmaster.description_idWHERE MachineProductionTime.MachineId = ven_fullreportmaster.MachineIdAND ven_descriptionmaster.description_id = 'DOWN'---------------------------------------------------------------------For SQL Contractor and Consulting Support:http://www.thedatasolutioncenter.com |
|
|
|
|
|
|
|