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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Select Datepart

Author  Topic 

rafaeljunio.ti
Starting Member

1 Post

Posted - 2012-07-22 : 16:10:26
Goodnight everyone,,

I have the following situation ... I have a table to count the hours of maintenance equipment in a day, month, year, etc. ..

Say I have a table with the following fields:

TMP_ID => id table

TMP_DT_INICIO => start of maintenance

TMP_DT_FIM => end of the maintenance

TMP_EQUIPAMENTO => Equipment manutenido

* A maintenance can never occur twice or less time if a product is on the table that he had retained (in manuntenção) from 10/07/2012 to 18/07/2012 there will be no other record of that equipment from that date. . the next maintenance should be in May as the date of liberation of the last manuntecaoq u did this equipment.

* But this equipment can enter and leave the ofcina N times or whether it may be trapped between 10/07 to 18/07 and then be held on 20/07 to 23/07 for example.

So I need eh build a query that tells me how many hours per day that equipment was held up day by day ... using the datediff diferecnas can pick up from the beginning and end of maintenance and it already phaco in a different situation but here need know how much he was detained hours each day in the month or atuas

Say a EQU_NOME was held three times in the current month (July)

Day 13/07 at 7:00 until 15/07 at 11:30

Day 16/07 10:00 until 18/07 at 11:00

I need it to return on 01/07 02/07 .... until 12/07 had already manuntecao 00:00 hours on 13/07 was 17:00, 14/07 00:00 and had here it goes.

ie, one column for each day of my current month .. always the current times the Maintenance equipment for that particular day .. if he stood once but was 5 days of maintenance necessary to show that this has had so many days and hours here it goes.

Always I appreciate the attention of the Forum.

thank you

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-07-22 : 17:15:25
Try the DATEDIFF function.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-22 : 20:19:32
See if what I am showing below is sort of what you are looking for. In the code below, the #MaintenanceTable is a sample table of your data that I made up. The #Calendar table is a calendar table that has one row for each day that is of interest to you.

-- CREATE A CALENDAR TABLE.
CREATE TABLE #Calendar(Dt DATE NOT NULL PRIMARY KEY CLUSTERED);

;WITH N(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM N WHERE n < 50)
INSERT INTO #Calendar SELECT DATEADD(dd,n-1,'20120701')
FROM N OPTION (MAXRECURSION 0);

-- CREATE AND POPULATE A SAMPLE MAINTENANCE TABLE
CREATE TABLE #MaintenanceTable(
TMP_EQUIPAMENTO INT,
TMP_DT_INICIO DATETIME,
TMP_DT_FIM DATETIME);
INSERT INTO #MaintenanceTable VALUES
(1, '2012-07-11 20:00:00.000', '2012-07-18 11:00:00.000'),
(2, '2012-07-21 20:00:00.000', '2012-07-21 23:00:00.000'),
(3, '2012-07-27 20:00:00.000', '2012-07-28 20:00:00.000')

-- QUERY
SELECT
Dt,
TMP_EQUIPAMENTO,
CASE
WHEN TMP_DT_INICIO < DATEADD(dd,1,dt) AND TMP_DT_FIM > Dt THEN
DATEDIFF
(
hh,
CASE WHEN TMP_DT_INICIO < Dt THEN Dt ELSE TMP_DT_INICIO END,
CASE WHEN TMP_DT_FIM > DATEADD(dd,1,dt) THEN DATEADD(dd,1,dt) ELSE TMP_DT_FIM END
)
ELSE 0
END AS Hours
FROM
#Calendar c
CROSS APPLY
(
SELECT TOP 1 TMP_EQUIPAMENTO,TMP_DT_INICIO ,TMP_DT_FIM
FROM #MaintenanceTable m
WHERE m.TMP_DT_INICIO < DATEADD(dd,1,c.Dt)
AND TMP_DT_FIM > c.Dt
ORDER BY TMP_DT_INICIO DESC
) m
ORDER BY TMP_EQUIPAMENTO, Dt;

DROP TABLE #Calendar
DROP TABLE #MaintenanceTable
If this is not what you are looking for, create code that someone can copy and run on their computer (just like you can copy and run the code I posted) which creates sample data and shows your expected output.
Go to Top of Page
   

- Advertisement -