2
I’m developing a system and I’m bumping into an SQL query that consists of the user searching for Date and will have the option to choose if the query will be made adding Weekend to these 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 she doesn’t 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 already tried inside the Query, with Join, with Union, with Having, creating an auxiliary Bank to compare and nothing. Could someone explain to me how this could be done?
Below is the code of 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'
No Case, I made this test query to return the Datepart from days 03 to 05. Returned 6 for the occurrences of the day 03(Friday), 7 for the day 4(Saturday) and 1 for Sunday, the problem is that the final consultation will be an average of these occurrences and the total of hours she was out. And in that, the day of the week is not fitting.
Below is the final query that I need to fit the choice of Weekend.
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 fix this? A trial or some other way?
The value returned by DATEPART(weekday, ...) is not constant for the same day of the week, as it varies according to the DATEFIRST configuration. By the way, this is in the documentation: "When datepart is week (wk, ww) or weekday (dw), the Return value depends on the value that is set by using SET DATEFIRST".
– José Diz
@Josédiz yes, and if you read the question you will see what the configuration for his case. Again, I think that the reading of the
downvote
for you, but if you want to continue harming all the answers I give, including those of languages that you do not have mastery and competence to evaluate I will look for moderation.– Sorack
It worked. It worked perfectly
– Fernando Henrique Gonçalves