Home Forums SQL Server 2005 T-SQL (SS2K5) T-SQL Function that works like the FIRST funcion in MS Access? RE: T-SQL Function that works like the FIRST funcion in MS Access?

  • jamesheslin123 (8/12/2013)


    Hello,

    I need to convert a query in MS Access to a view in SQL Server 2005. The view will draw from two tables: Employee and Spaces. I want the view to be grouped by the space_number. Also, if there are two employees for one space_number, I need the two employees' names to be listed in the same row of the view.

    Here is the Access code of the query, which uses the FIRST function to do what I need:

    SELECT Spaces.Space_Number, First(Employee.First) AS First1, First(Employee.Last) AS Last1, Last(Employee.First) AS First2, Last(Employee.Last) AS Last2, First(Employee.CC) AS FirstOfCC, First(Employee.[Employee Type]) AS [FirstOfEmployee Type], Spaces.[Space Type]

    FROM Spaces LEFT JOIN Employee ON Spaces.Space_ID = Employee.Space_Number

    GROUP BY Spaces.Space_Number, Spaces.[Space Type];

    Below is the SQL Code that I have so far... :

    SELECT TOP (100) PERCENT dbo.Spaces.Space_Number, dbo.Spaces.Space_ID, dbo.Employee.First AS First1, dbo.Employee.Last AS Last1, dbo.Employee.First AS First2,

    dbo.Employee.Last AS Last2, dbo.Employee.CC, dbo.Employee.[Employee Type], dbo.Spaces.[Space Type]

    FROM dbo.Employee RIGHT OUTER JOIN

    dbo.Spaces ON dbo.Employee.Space_Number = dbo.Spaces.Space_ID

    ORDER BY dbo.Spaces.Space_Number

    You have a couple things going on here. The first challenge is the FIRST and LAST functions from Access. These functions make no sense in relation theory because tables by their nature have no concept of order. Access however decided to do this which is incredibly confusing for people and causes lots of issues. To get the "first" in sql server you must specify which one is first via an order by clause. The same concept holds true for LAST. This in itself wouldn't be so bad but you added one more little challenge to the puzzle.

    Also, if there are two employees for one space_number, I need the two employees' names to be listed in the same row of the view.

    What does that mean? Do you want all employee's listed in a comma seperated list in one column or do you want a dynamic number of columns based on the values found? Neither of these is trivial but are both certainly feasible.

    In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/