I'm developing a system and am bumping into a SQL query that consists of the user searching by Date and will have the option to choose whether the query will be done by adding Weekend to those dates, excluding the weekend or just consulting the Weekend.
In this case, I have a View that will be used for this application. The problem is that it does not have a specific field for the day of the week, so I had to do a DatePart.
The problem is that when I use with other elements of the query, the error in it.
I have already tried within Query, with Join, with Union, Having, creating an auxiliary Bench to compare and nothing. Could someone explain to me how this could be done?
Below is the code for the two tables.
SELECT [DT_ABERT_OPER], DATEPART(weekday, [DT_ABERT_OPER]) AS
DIA_DA_SEMANA
FROM [dbo].[reavw001_ocorrencias_xxxxx] WHERE [DT_ABERT_OPER]
BETWEEN '2017-11-03' AND '2017-11-05'
In the Case, I made this test query to return the DatePart from days 03 to 05. It returned 6 for the occurrences of Day 03 (Friday), 7 for those of Day 4 (Saturday) and 1 for Sunday, the problem is that the final query will be an average of these occurrences and the total number of hours it has been out. And in this, the day of the week is not fitting.
Below is the final query that I need to fit into the Weekend Choice.
SELECT GITEC, SUM (DATEDIFF (minute,INICIO,DH_ULTIMA_CATEGORIZACAO))
as Soma_tempo,
AVG (DATEDIFF (minute,INICIO,DH_ULTIMA_CATEGORIZACAO)) as Media_tempo
FROM [readb004].[dbo].[reavw001_ocorrencias_xxxxxx] WHERE
([DT_ABERT_OPER]
BETWEEN '2017-11-03' AND '2017-11-06 23:59:59') GROUP BY GITEC order
by GITEC DESC
What can be done to solve this? Is there a procedure or is there any other way?