include weekends on sql query

Waelhi

I have a database that stores the log-in and log-out of the employees but we don't have work on weekends. My supervisor want the DTR report format(I'm using RDLC report) include the weekends. (see attached image)

enter image description here

The image above is the expected output format for DTR. I just want to know how to include Weekends though my data are on weekdays only. Is it possible to do this using SQL Query? If yes, should I use looping in sql here?

SQL Code:

select user_id,log_date,login_time,logout_time 
from table_DTR
where user_id = 'USER1'
AND log_date BETWEEN '11/21/2014' AND '12/09/2014'
Sandeep

Use common table expression and generate date range with from and to date and than use CTE as left join to actual table. I haven't used user_id filter in left join so apply it to your query:

 DECLARE @TMEP TABLE
    (
        [Date] DATE,
        [IN] VARCHAR(10),
        [OUT] VARCHAR(10)
    )

    INSERT INTO @TMEP VALUES ('2014-11-11','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-12','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-13','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-14','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-15','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-18','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-19','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-20','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-21','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-22','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-25','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-26','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-27','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-28','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-11-29','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-12-1','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-12-2','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-12-3','7:30','5:30')
    INSERT INTO @TMEP VALUES ('2014-12-4','7:30','5:30')

    DECLARE @FromDate DATE 
    SET @FromDate = '2014-11-11 06:00:00.000'
    DECLARE @ToDate DATE
    SET @ToDate = '2014-12-11 06:00:00.000'

    ;WITH CTE_TableDate ([CTEDate]) as
    (
        SELECT @FromDate
        UNION ALL
        SELECT  DATEADD(DAY,1,CTEDate) FROM CTE_TableDate WHERE [CTEDate] < @ToDate
    )
    SELECT 
        CTE_TableDate.CTEDate,
        CASE WHEN DATEPART(DW, CTE_TableDate.CTEDate) = 7 THEN 'SATURDAY'
    WHEN DATEPART(DW, CTE_TableDate.CTEDate) = 1 THEN 'SUNDAY' 
    ELSE TEMP.[In] END AS [IN],
    CASE WHEN DATEPART(DW, CTE_TableDate.CTEDate) = 7 THEN 'SATURDAY'
    WHEN DATEPART(DW, CTE_TableDate.CTEDate) = 1 THEN 'SUNDAY' 
    ELSE TEMP.[OUT] END AS [OUT]
    FROM CTE_TableDate
    LEFT JOIN
    (
        select 
            [Date],
            [IN],
            [OUT] 
        from 
            @TMEP) TEMP
    ON
        CTE_TableDate.CTEDate = TEMP.[Date]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I exclude weekends and holidays in SQL Server query

From Dev

sql query to include null

From Dev

to include a header into result of sQL query

From Dev

Include zeros in SQL count query?

From Dev

SQL Include zero rows in query

From Dev

to include a header into result of sQL query

From Dev

SQL query to DataTable to include headers

From Dev

How to include IF ELSE in SQL Query

From Dev

Exclude weekends from my query

From Dev

SQL Query to find the past 3rd business day excluding holidays and weekends

From Dev

Exclude / ignore weekends in Oracle SQL

From Dev

How to include variable in a SQL query in C#

From Dev

How to include only the last match in an SQL query

From Dev

Generalize SQL query to include data for all Users

From Dev

Generalize SQL query to include data for all Users

From Dev

Include Search Parameters Custom Paging SQL Query

From Dev

Modify an SQL query to include additional parameters

From Dev

How do I exclude weekends in SQL?

From Dev

or query with include

From Dev

SQL Server Query: Using JOIN to include NULL values

From Dev

Why does a SQL query with != 0 not include NULL values?

From Dev

pentaho cde include/exclude where clause in sql query

From Dev

How do I run a foreach against an array and include it in an SQL query?

From Dev

SQL Server Include user function result into select query

From Dev

SQL Server Query: Using JOIN to include NULL values

From Dev

Activerecord: how to query using find_by_sql and include with a variable

From Dev

MS Access SQL Query does not include specified expression as aggregated function

From Dev

pentaho cde include/exclude where clause in sql query

From Dev

SQL Server: how to include count in the select query with where clause?

Related Related

  1. 1

    How can I exclude weekends and holidays in SQL Server query

  2. 2

    sql query to include null

  3. 3

    to include a header into result of sQL query

  4. 4

    Include zeros in SQL count query?

  5. 5

    SQL Include zero rows in query

  6. 6

    to include a header into result of sQL query

  7. 7

    SQL query to DataTable to include headers

  8. 8

    How to include IF ELSE in SQL Query

  9. 9

    Exclude weekends from my query

  10. 10

    SQL Query to find the past 3rd business day excluding holidays and weekends

  11. 11

    Exclude / ignore weekends in Oracle SQL

  12. 12

    How to include variable in a SQL query in C#

  13. 13

    How to include only the last match in an SQL query

  14. 14

    Generalize SQL query to include data for all Users

  15. 15

    Generalize SQL query to include data for all Users

  16. 16

    Include Search Parameters Custom Paging SQL Query

  17. 17

    Modify an SQL query to include additional parameters

  18. 18

    How do I exclude weekends in SQL?

  19. 19

    or query with include

  20. 20

    SQL Server Query: Using JOIN to include NULL values

  21. 21

    Why does a SQL query with != 0 not include NULL values?

  22. 22

    pentaho cde include/exclude where clause in sql query

  23. 23

    How do I run a foreach against an array and include it in an SQL query?

  24. 24

    SQL Server Include user function result into select query

  25. 25

    SQL Server Query: Using JOIN to include NULL values

  26. 26

    Activerecord: how to query using find_by_sql and include with a variable

  27. 27

    MS Access SQL Query does not include specified expression as aggregated function

  28. 28

    pentaho cde include/exclude where clause in sql query

  29. 29

    SQL Server: how to include count in the select query with where clause?

HotTag

Archive