I have a problem when trying to make a query, I explain what I have:
I have a schedule of X device, to calculate the hours of operation against the hours that have failures, what I need is a query to totalize the hours of operation per month. I mean, I have a table that I have running hours Monday-Friday, Saturdays, and Sundays. For example: X appliance operates Monday through Friday from 7:00 AM to 8:00 PM, Saturdays from 8:00 AM to 8:00 PM and Sundays from 8:00 AM to 12:00 PM. So the idea is to take out how many hours should work per month, but I'm stuck in the query, I would like to be able to cover the whole month, that is, add up the hours worked per month.
The TIME tables is this:
CREATE TABLE [Horarios](
[COD_ATM] [int] NOT NULL,
[LV_INI] [datetime] NULL,
[LV_FIN] [datetime] NULL,
[SA_INI] [datetime] NULL,
[SA_FIN] [datetime] NULL,
[DO_INI] [datetime] NULL,
[DO_FIN] [datetime] NULL)
LV_INI = Monday To Friday Start to work LV_FIN = Monday to Friday Stop working (so do the others too) Some data:
COD_ATM LV_INI LV_FIN SA_INI
SA_FIN DO_INI DO_FIN
1 2014-06-09 07:00:00.000 2014-06-09 20:00:00.000 2014-06-09 07:00:00.000 2014-06-09 20:00:00.000 2014-06-09 07:00:00.000 2014-06-09 20:00:00.000
2 2014-06-09 08:00:00.000 2014-06-09 16:00:00.000 2014-06-09 08:00:00.000 2014-06-09 12:00:00.000 2014-06-09 00:00:00.000 2014-06-09 00:00:00.000
3 2014-06-09 00:00:00.000 2014-06-09 23:59:00.000 2014-06-09 00:00:00.000 2014-06-09 23:59:00.000 2014-06-09 00:00:00.000 2014-06-09 23:59:00.000
4 2014-06-09 07:00:00.000 2014-06-09 20:00:00.000 2014-06-09 07:00:00.000 2014-06-09 20:00:00.000 2014-06-09 07:00:00.000 2014-06-09 20:00:00.000
5 2014-06-09 08:00:00.000 2014-06-09 20:00:00.000 2014-06-09 08:00:00.000 2014-06-09 20:00:00.000 2014-06-09 08:00:00.000 2014-06-09 20:00:00.000
6 2014-06-09 07:00:00.000 2014-06-09 21:00:00.000 2014-06-09 07:00:00.000 2014-06-09 21:00:00.000 2014-06-09 07:00:00.000 2014-06-09 21:00:00.000
And the query I have is this:
/****** Promedio de horas ******/
SELECT COD_ATM,
sum(convert(int, DATEDIFF(HOUR, LV_INI, LV_FIN))*5) as LunesAViernes,
sum(convert(int, DATEDIFF(HOUR, SA_INI, SA_FIN))) as Sabado,
sum(convert(int, DATEDIFF(HOUR, DO_INI, DO_FIN))) as Domingo,
sum((convert(int, (DATEDIFF(HOUR, LV_INI, LV_FIN))*5) + convert(int, DATEDIFF(HOUR, SA_INI, SA_FIN)) + convert(int, DATEDIFF(HOUR, DO_INI, DO_FIN)))) AS TOTAL,
sum((convert(int, (DATEDIFF(HOUR, LV_INI, LV_FIN))*5) + convert(int, DATEDIFF(HOUR, SA_INI, SA_FIN)) + convert(int, DATEDIFF(HOUR, DO_INI, DO_FIN)))*4) AS TOTAL
FROM [ATMs].[dbo].[AT1203]
group by COD_ATM, LV_INI, LV_FIN
Then in this query I calculate them assuming that the month has 4 weeks. What I would like to do is add up according to the month I'm in.
And what I get is this: It approaches the desired data but it is not what I want
COD_ATM LunesAViernes Sabado Domingo TOTAL TOTAL
1 65 13 13 91 364
2 40 4 0 44 176
3 115 23 23 161 644
4 65 13 13 91 364
5 60 12 12 84 336
6 70 14 14 98 392
I hope you have given me to understand, any doubt is pending. I clarify that the date fields are not necessary, what I occupy of those fields is the start time and the end time respectively.
An expected example would be the following for ATM 1, September has 5 Saturdays, 4 Sundays, and 21 days a week. Then the ATM1 works from Monday to Friday from 7AM to 8PM, which means that it works 13 hours a day, if we multiply it by 21 we obtain the hours that work per week (Monday to Friday) which would be 273 hours a week and They are 4 Sundays (4 Sundays * 13) = 52 same on Saturday = 52
Entoces el total trabajado seria:
273 los dias de la semana
52 los sabados
52 los domingos
**377 Total**. Ese seria el resultado esperado, **y en la consulta que tengo solo me salen 364**