I need to understand the following MS SQL statement ( SELECT FROM VALUES)

MrMee

I am pretty new to MS SQL but I am having to work with it a lot now. I need to understand what is going on here:

    BEGIN TRANSACTION loadHalfdayAbsences;
    INSERT INTO @halfDayAbsences
    ([AbsencePart], 
     [AbsenceId], 
     [DeleteDate], 
     [LastChangeDate]
    )
           SELECT CASE ap.AbsencePart
                      WHEN 1
                      THEN a.AbsenceStart
                      WHEN 3
                      THEN a.AbsenceEnd
                      ELSE CASE
                               WHEN a.AbsenceStartHalfDay = 1
                               THEN DATEADD(DAY, 1, a.AbsenceStart)
                               ELSE a.AbsenceStart
                           END
                  END AS newEnd,
                  CASE ap.AbsencePart
                      WHEN 1
                      THEN 0.50
                      WHEN 2
                      THEN 1.00
                      WHEN 3
                      THEN 0.50
                  END AS newDuration, 
                  [ap].[AbsencePart], 
                  [a].[AbsenceId], 
                  [a].[EmployeeId], 
           FROM
           (
               SELECT AbsencePart
               FROM(VALUES(1), (2), (3)) AS t(AbsencePart)
           ) AS ap
           INNER JOIN dwh.Absence AS a ON 1 = 1
           WHERE AbsenceType IN
           (
               SELECT AbsenceType
               FROM @AbsenceType4TimeTac
           )

Particularly:

           FROM
           (
               SELECT AbsencePart
               FROM(VALUES(1), (2), (3)) AS t(AbsencePart)
           ) AS ap
           INNER JOIN dwh.Absence AS a ON 1 = 1
           WHERE AbsenceType IN
           (
               SELECT AbsenceType
               FROM @AbsenceType4TimeTac
           )
  • What does this part do?
  • What do the VALUES do here for me?
  • What happens to the data?
  • Is it being selected, but only fields where there is either a 1, a 2 or a 3 in there? Or what is going on with VALUES?

Thanks for any input in advance :)

Tim Biegeleisen

The portion

FROM (VALUES(1), (2), (3)) AS t(AbsencePart)

just is an inline table consisting of three values, 1 through 3, in a column called AbsensePart. You could have also used the following syntax:

FROM
(
    SELECT 1 AS AbsencePart UNION ALL
    SELECT 2 UNION ALL
    SELECT 3
) t

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Need SQL select statement for the following

SQL: I need to format a Select Distinct Statement with a Case as one of the values

Need explanation to understand the following java import statement

How do I select the node values from the following XML using SQL?

Can I insert multiple values to a MS Access record using an SQL statement from VB 6.0?

Select Columns From Values of other sql statement

Concatenating values from SQL select statement

How do I fix this SQL SELECT statement for MS Access

I need to understand how the following code works. What is the step by step to get from that array to the object

I need to understand the Conditional ternary statement

How can I UPDATE SQL using values from multiple columns of a select statement

Join a MS SQL Server WITH statement into a Select statement

How to structure SELECT from VALUES on MS SQL Server 2000

I am trying to add 2 values in a SQL Select Statement

I need to send values from my select options

How to insert values into database table from select statement SQL

Is there any way to use values from a JSON object in a SQL Select statement?

MS Access - Assigning a variable to add fixed Values to VBA/SQL INSERT/SELECT statement

How to write a select statement in SQL that returns the following result from the set provided

I need to combine two results from a SQL select query

Need help writing an SQL query for a select statement

Need help on writing sql select statement

I need help in a select statement Query

How do I convert the following SQL Statement to an Update Statement

MS SQL Server select statement: display all combinations from two tables

SQL update statement from select

SQL - Select Values From A Table Where A Corresponding Value Matches The Results Of Another Select Statement

Need to understand specific LEFT OUTER JOIN behavior in SQL SELECT

How to Increment values dynamically in SQL select statement